Friday, September 4, 2015

Deployment Using SSH2

Today we will going to learn deployment on remote server using SSH2. First ssh2 package needs to be installed.
Establish a connection:
connection = ssh2_connect('Server ip / domain name',
22,array('hostkey'=>'ssh-rsa'));
if(!$connection){    echo "connection is not established";}
Prefer using public and private keys to authenticate logins.
ssh2_auth_pubkey_file(    $connection,    'deploy',    
'/your path to/.ssh/id_rsa.pub',    '/your path to/.ssh/id_rsa');

Whether we use username/password or public/private key authentication ssh2_auth_pubkey_file() return a Boolean value indicating whether authentication was successful.

Now performing basic command to deploy

$stream = ssh2_exec($connection, 'cd /your document root path; 
git pull https://username:password@git repositrypath your branch name;');

$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);

stream_set_blocking($errorStream, true);

stream_set_blocking($stream, true);

echo   stream_get_contents($stream) . stream_get_contents($errorStream);

Last line to show message, if deployment is successfull or not.



No comments:

Post a Comment