Yii Automating deployment without capistrano

Automating tasks and routines is very important. If you are doing deployments manually you really need to consider making an automated way of doing it. A good tool that is Ruby based is  capistrano. The one thing I noticed about capistrano though was the tool was kind of heavy. I wanted something a little more lightweight that I understood the entire process. So even though capistrano would have got the job done I didn’t need such a heavy tool. So I decided to create my own tool. This is just a single PHP script that you could store in your commands directory. Then you just execute it.


php commands/release_test.php

You could also pass in an environment variable if you don’t want to have a separate command for each environment. For me I wanted it as a separate command so I didn’t have to think about it on release. Here is the script in detail hopefully it’s useful to other Yii projects.


<?php
 // This is a simple script that can be used to release code to the test environment

&nbsp;

$server_location = ‘[234.343.343.343]’;
 $env = ‘[testing]’;
 $server_username = ‘[USERNAME]’;
 $production_location = ‘[PRODUCTION_LOCATION] i.e. /Users/whatever/location…’;
 $project_assets_location = ‘[ASSETS]’;

$git_upstream_name = ‘[upstream]’;
 $git_branch_name = ‘[branch_name]’;

// connect to the server
 $connection = ssh2_connect($server_location, 22);

echo “Enter password: “;
 $password = preg_replace(’/\r?\n$/’, ‘’, `stty -echo; head -n1 ; stty echo`);
 echo “\n”;

ssh2_auth_password($connection, $server_username, trim($password));

// update the remote repository
 $updateResponse = ssh2_exec($connection, ‘cd ‘.$production_location.’; git remote update’);
 stream_set_blocking($updateResponse, true);
 $updateResponseOutput = stream_get_contents($updateResponse);
 echo $updateResponseOutput.”\n”;

// pull in the latest code
 $pullResponse = ssh2_exec($connection, ‘cd ‘.$production_location.’; git pull ‘.$git_upstream_name.’ ‘.$git_branch_name);
 stream_set_blocking($pullResponse, true);
 $pullResponseOutput = stream_get_contents($pullResponse);
 echo $pullResponseOutput.”\n”;

// run the migrations
 $migrations = ssh2_exec($connection, ‘YII_ENVIRONMENT=’.$env.’ ‘.$production_location.’/yiic migrate —interactive=0’);
 stream_set_blocking($migrations, true);
 $migrationsOutput = stream_get_contents($migrations);
 echo $migrationsOutput.”\n”;// check to see if we want assets rebuild
 echo “Do you want to rebuild assets? Type ‘yes’ to rebuild assets: “;
 $handle = fopen (“php://stdin”, “r”);
 $line = fgets($handle);
 if (trim($line) == ‘yes’) { $cleanAssets = ssh2_exec($connection, ‘rm -r ‘.$project_assets_location.’/*’); stream_set_blocking($cleanAssets, true); $cleanAssetsOutput = stream_get_contents($cleanAssets); echo $cleanAssetsOutput.”\n”;
 }
 // close the connection

// exit the ssh2 connection
 ssh2_exec($connection, ‘exit’);

Leave a Reply

Your email address will not be published. Required fields are marked *