Skip to content

Deployment with Webhooks

Chirag Gude edited this page Mar 7, 2015 · 5 revisions

Automagic Deployment from GitHub to Production server with a WebHook

Create a webhook file git-php-webhook-xxxxxxxxxxxxxx.php on the Production server in /home/larabase/public. Remember to set the variables in this file
<?php

// Set Variables
$LOCAL_ROOT         = "/home";
$LOCAL_REPO_NAME    = "larabase";
$LOCAL_REPO         = "{$LOCAL_ROOT}/{$LOCAL_REPO_NAME}";
$REMOTE_REPO        = "[email protected]:chiraggude/larabase.git";
$BRANCH             = "master";

$payload_github = file_get_contents('php://input');
$data = json_decode($payload_github);

if ( $data->ref === 'refs/heads/master' ) {

  // Only respond to POST requests from Github
  echo "Payload received from GitHub".PHP_EOL;
  
  if( file_exists($LOCAL_REPO) ) 
  {
    
    $whoami = shell_exec("whoami");
	echo "whoami: $whoami".PHP_EOL;
	
	// If there is already a repo, just run a git pull to grab the latest changes    	
	$git_pull = shell_exec("git pull 2>&1");
	echo "Git Pull: $git_pull".PHP_EOL;
	
	// $composer_install = shell_exec("cd $LOCAL_REPO && composer install 2>&1");
	// echo "Composer Install: $composer_install".PHP_EOL;
		
	$artisan_dump = shell_exec("cd $LOCAL_REPO && php artisan dump-autoload 2>&1");
	echo "PHP Artisan Dump-Autoload: $artisan_dump".PHP_EOL;

	$artisan_migrate = shell_exec("cd $LOCAL_REPO && php artisan migrate --env=production 2>&1");
	echo "PHP Artisan Migrate: $artisan_migrate".PHP_EOL;

    die("The End! " . mktime());	
  } 
  else 
  {
    
    // If the repo does not exist, then clone it into the parent directory
	
    shell_exec("cd {$LOCAL_ROOT} && git clone {$REMOTE_REPO} {$LOCAL_REPO_NAME}");
	echo "git clone: repo cloned successfully!".PHP_EOL;
	
	shell_exec("cd {$LOCAL_REPO} && composer install");
	echo "Executed: composer install".PHP_EOL;
    
    die("The End! " . mktime());
  }
} 

else {
	echo "Payload is not from GitHub. Nothing to see here!".PHP_EOL;
}
Give ownership of webhook file to NGINX
cd /home/larabase/public
chown nginx:nginx git-php-webhook-xxxxxxxxxxxxxx.php  

Note: File permission of the webhook file should be 664

Add the corresponding url to GitHub > Settings > WebHooks & Services

http://example.com/github-webhook-xxxxxxxxxxxxx.php

After a push event on GitHub, the webhook will execute the shell commands.

Note: In Production, migrate existing database after latest git pull with:

php artisan migrate --env=production

Webhooks for deploying Private Repos

  • Setup the webhook-projectname.php file in /home as shown in the script below
  • Give NGINX the permission to execute that file: # chown nginx:nginx webhook-projectname.php
  • Create SSH public key: # ssh-keygen -t rsa -C "[email protected]"
  • Enter file in which to save the key: # [press-enter]
  • Enter passphrase: # [press-enter]
  • Repeat passphrase: # [press-enter]
  • Public key is saved in the user's folder /root/.ssh
  • Copy the files in the root user's folder to var/cache/nginx/.ssh
  • Change directory: # cd /var/cache/nginx
  • Give NGINX user permissions to the public keys: # chown nginx:nginx .ssh
  • Ensure that all files in the .ssh folder 600 permissions
  • Open the id_rsa.pub file and add it as a deployment key in the remote Git repository (eg. GitHub, Gitlab)
  • Go to VPS, and clone the private repo: # git clone [email protected]:username/project-name.git