Auto-deploy your PHP app with Bitbucket Pipelines

I’ve got to admit – I’m old school!

Then we moved to subversion, woah, this is cool!

svn commit -m 'pushing this fix to the interwebs'

Then SSH to the relevant webserver, cd to the right dir, and calling svn up, that’s progress!

Then we introduced load-balancing and multiple app servers, so we wrote a bash script we can call from our mac’s that ssh’d to each app server and called the svn update command.

Now git and Pipelines.

We’ve moved our internal subversion code to bitbucket for a number of our newer projects and with it, we’re setting up some better practices.

These are all common practices, no wheel inventing here, and you’re probably doing this better than us already.
Let me know!


We’ve got three branches, our development all goes in the main branch, then we have a staging and production branch.

In the staging branch I’ve set up the following Pipeline:

    - step:
        name: Deploy to Staging
          - curl

Each push to our staging & production branch triggers the Pipeline build and web hook to deploy the changes across our stack.

The Webhook

You see the deployment pipeline is using curl to call a web hook.
This is a simple PHP script that does the following:

  • Checks the authentication token
  • Calls GIT PULL
  • Clears PHP OPCACHE
  • Clears an asset cache directory

Here is the script, the useful part for you is the git pull action.


	// Check auth token
	if($_GET['token'] != '123456789') die('Invalid Request');

	// deploys code
	exec('cd /var/www/site-name/&& git pull');

	// clears asset cache
	exec('cd /var/www/site-name/public/cache&& rm *.cache');

	// reload PHP for opcache refresh
	exec('sudo service php7.4-pfm reload');

	// Sends a SLACK WEB HOOK to out office channel
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL, '');
	curl_setopt($ch,CURLOPT_POST, 1);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch,CURLOPT_POSTFIELDS, 'payload={"channel": "#deployments", "username": "Git Deployed", "text": '. json_encode('Development sever code deployed at '. date('g:ia d/m')) .', "icon_emoji": ":fire:"}');
	$result = curl_exec($ch);

	echo "Deployed!\n";

Production Deployment

The above pipeline and PHP code is for our staging/development branch, our Production deployment is the same, and the code is the save, but in the pipeline, we have a curl request for each of the app servers running the code (4 app servers currently).

Leave a comment

Your email address will not be published.