Hi! I'm currently reworking my site, so although this content is still online things may change or look odd. Please get in touch to find out how I can help you!

Using Bitbucket for Automated Deployment of Multiple Repositories

I’ve recently started using Bitbucket as a kind of off-site backup for my Git repos - it works beautifully with SourceTree as you can have all commits pushed to the “origin” repo by default. Lovely.

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.

Usage

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/example/htdocs and /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.

Photo: Colorful buckets 2 by ivanmarn

Tags: Bitbucket Development Git PHP

Feedback

Sorry, feedback is now closed on this post, but please feel free to get in touch if you would like to talk about it!

  1. Jonathan on

    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?

    Thanks!

  2. Kris on

    Hi Jonathan

    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!

Or find posts by tag →

Friends & Influences

  1. Aching Brain
  2. Andy Budd
  3. Anthony Killeen
  4. Ben Everard
  5. Cameron Moll
  6. Dan Cederholm
  7. Dan Mall
  8. Dave Shea
  9. Elliot Jay Stocks
  10. Jamie Knight
  11. Jamie Rumbelow
  12. Jason Santa Maria
  13. Jeff Croft
  14. Jeffrey Zeldman
  15. Jeremy Keith
  16. Jon Hicks
  17. Khoi Vinh
  18. Mark Boulton
  19. Matt Croucher
  20. Nocturnal Monkey
  21. Sarah Parmenter
  22. Shaun Inman
  23. Simon Collison
  24. Tim Van Damme
  25. Toby Howarth