Specifying Deployment Targets for Different Git Branches

- By Charlie Crawford on February 03, 2015

So, you already have Shippable setup for all your CI needs, and you're itching to take the plunge into CD? Maybe the thing that's been holding you back is you aren't sure how you can easily deploy to your dev, staging, and prod machines, without accidently pushing to production prematurely? The good news is Shippable can easily be setup to accommodate such a work flow! You can specify different deployment procedures based on your git branch.

Let's say you want to use want Shippable to perform a build for every commit, but you only want to do a deployment if it's a commit to the master branch. We keep track of what branch was pushed to via the BRANCH environment variable; This environment  variable can be used to implement branch specific logic. Here is an example shippable.yml snippet:

env:
global:
- APP_NAME_PROD=shroudd-headland-1758

after_success:
- if [ "$BRANCH" == "master" ]; then git push -f git@heroku.com:$APP_NAME_PROD.git master; fi

This will push your app to Heroku, but will only do so for successful builds from the master branch. For those unfamiliar with using Shippable for continuous deployment to Heroku, please refer to our documentation.

This basic idea can be extended in many ways! For example, you could send commits from master to prod, and all other commits to dev:

env:
  global:
    - APP_NAME_DEV=whispering-canyon-9823
    - APP_NAME_PROD=shroudd-headland-1758

after_success:
 - if [ "$BRANCH" != "master" ]; then git push -f git@heroku.com:$APP_NAME_DEV.git master; fi
 - if [ "$BRANCH" == "master" ]; then git push -f git@heroku.com:$APP_NAME_PROD.git master; fi

Feel free to add as many deployment targets as you need for your work flow! With the BRANCH environment variable, you have all the information you need! Additionally, this process works with other deployment targets such as AWS, Digital Ocean, app engine, and others; simply swap out the logic inside the if conditions, and you're good to go!

Topics: continuous deployment (CD), how-to