Deploy with Bitbucket + Webhook + PHP/Bash

17 Aug
Goran

Goran

web developer at test
Goran

Latest posts by Goran (see all)

Deploy what the ….

In short I have a bitbucket account (using it with git for some of the projects). For hosting I am using digitalocean which is by the way the best hosting ever for 5$. You get like VPS SSD hosting with 20GB of memory, 2TB bandwith, super simple interface and al that for 5$.. So let’s see  how to deploy code to digitalocean.

So I did some project with google polymer, which uses bower for dependency resolving. Polymer is using vulcanize as a build tool. I needed a deploy system which will do the following after my local push:

  1. code is now on bitbucket
  2. than code needs to be deployed to my domain
  3. project needs to be build
  4. I need some log if something goes wrong

Simple proces

After I created repository in bitbucket and cloned it to my PC, I did the same on the server. Why, because there is no need to copy code or similar, I can just do git pull on my server too and voila, code is there.

Nest step is to create bitbucket webhook (github has it also). That’s one cool option. It just triggers some URL via POST when some action occurs, for example push to repository. Bitbucket also sends ton of data, commits etc. I don’t need that for now.

Here is some example from bitbucket blog how it looks

Git and Bitbucket deploy with webhook

Bitbucket webhook preview

 

Very simple, it’s in repository settings. You can just check Repository push for start.In URL bar enter the script that you want to do your build process. Ok you’r done on bitbucket.

Next step is to secure your script so it can only trigger build for bitbucket IP addresses.
I whitelisted 104.192.143.193 && 104.192.143.208 . You can find more info on this bitbucket blogpost.

Next you create build.sh script which will be triggered by your PHP, Python or any other script that received the webhook request and is capable to trigger bash file.

Some security:
– plz put it in some protected folder so it’s not available on the web
– add www-data user to sudoers and limit access so it can only execute git pull and other commands you need for build.
www-data is used by apache so it’s good to also change the owner of your project files to be sure build will pass, you can do it with chown www-data:www-data *

Ok so my build.sh looks like this

you can see that I am putting my username and password in git  repo origin so git will properly login to bitbucket and pull my repository.

And this is simplified sample of php code that is triggering bash script on webhook request

on every build output is written to build.log so you can check if build failed or so.

I didn’t included all the small details and steps, just wanted to describe the flow..Linux users won’t have a problem setting this all up because you can potentially have problems with permissions or similar.

Basically that’s it. A very simple and basic steps for simple deploy with one branch.  I am using this method for some smaller projects. Probably Jenkins is the way to go and a standard flow. But if don’t have time to prepare all, custom approach can be useful also.