Using Bitbucket for Automated Deployment of Multiple Repositories
Usually I work locally with MAMP, pushing updates to a staging server if I need to show the client or something. Before now, I did that with good old FTP, but I wondered if there might be a way to have my commits pushed to the server via Git.
Thanks to Bitbucket’s “services”, there is. I discovered Brandon Summers' awesome post and managed to get it working with a minor change. The only problem that remained was that Brandon's script only worked with one repo, so if you wanted to use it with multiple repos on the same server, you’d have to have multiple instances of the script available at different URLs.
So, I came up with a solution, which I’ve added to GitHub as a gist so you can fork it etc.
The script is essentially the same as Brandon’s so follow his instructions for the most part, then set up your root directory and repo list.
As an example, if you have repos at
/home/projects/another_site, you’d set it up like so:
/** * The root directory where the repos live. * * @var string */ $root_dir = '/home/projects/'; /** * A list of repo slugs that the script is allowed to know about, and their * locations relative to $root_dir. * * @var array */ $repos = array( 'example-repo-1' => 'example/htdocs' 'another-repo' => 'another_site' );
If Bitbucket tries to push to a repo that isn’t defined in
$repos, the script will fail gracefully and do nothing, so you don’t need to worry about accidentally updating the wrong repo. Just check your commits before you push them to Bitbucket!
If you use this, please let me know how you get on with it! Any forks or suggestions for improvements etc are very welcome - I will be using this daily so if I come up with anything myself I’ll add it to the gist.
Posted on Friday, 1st February 2013.
Sorry, feedback is now closed on this post, but please feel free to get in touch if you would like to talk about it!
Hey, thanks for putting together this awesome script. I’m having some problems implementing it and was wondering if you could point me in the right direction. I’ve replaced the variables with my credentials but every time i push a change to bitbucket nothing gets pushed to my server. The deployment.log doesn’t even get created. Any ideas?
Sorry for the delay replying, I must’ve missed the notification of the comment :(
Sounds like the script isn’t getting hit at all.. does it respond if you just try to access it directly? It should give a “no repo specified” error.
If that works, try hard-coding it so every time deploy.php gets accessed, it updates a single repo - you can do this by setting $slug to the name of a known repo on line 160. That way you can see if the deployment parts of the script are running.
If still no logs, check that the log directory is writable by the user that PHP runs under on the server as there should be some logs written every time the deploy functions run (but not if they don’t due to a repo not being specified or an invalid repo specified).
Give those a go and let me know how you get on - assuming you still need help that is, apologies again!