Automated deployments with git-flow, heroku_san & CircleCI
05 Dec 2013Here’s a simple workflow that I like to use to quickly get automated deployments working on any RoR project running on Heroku.
git-flow
First we need to establish a good git branching process and for that I always use git-flow.
If you are not familiar with it, I highly recommend reading A successful Git branching model which explains what it’s all about.
Basically it’s just a command line wrapper for several git commands. For example, this git-flow command git flow feature start cool_stuff will create a new branch called feature/cool_stuff, based on your develop branch and switch to it.
It’s super easy to use. It establishes best-practices out the door and helps you leverage the power of git.
heroku_san
Now once you have that setup, we can move on to setting up heroku_san. It makes maintaining multiple environments (staging, production, demo) a breeze by providing a simple set of rake tasks.
Install the gem
group :development do
gem 'heroku_san'
end
and run the rake command
rails generate heroku_san
which will generate a /config/heroku.yml. You can tweak this according to your needs. In our case:
production:
app: liveapp
stack: cedar
config:
RACK_ENV: production
RAILS_ENV: production
BUNDLE_WITHOUT: "development:test"
staging:
app: stagingapp
stack: cedar
config:
RACK_ENV: staging
RAILS_ENV: staging
BUNDLE_WITHOUT: "development:test"
Now you could run rake staging deploy at anytime to deploy your code to the stagingapp on Heroku but we want to automate that.
CircleCI
That brings us to the last part. Here you could swap CircleCI with any other CI server such as Jenkins, Codeship, Travis, Semaphore, etc they should all work similarly.
In this case, go ahead and sign-up for CircleCI and add your project to it. After that, follow this guide to deploy to Heroku.
Once you’ve set that up, create a circle.yml file at the root of your project so it knows what to do when you push your code:
## Customize deployment commands
deployment:
production:
branch: master
commands:
- bundle exec rake production deploy
staging:
branch: develop
commands:
- bundle exec rake staging deploy
We are basically telling it to run heroku_san deploy tasks once your test suite passes.
In Action
So now that we’ve got it all dialed in, we can either push to develop or master and the code will be deployed to the correct app on Heroku.
Pushing to staging:
git push origin develop
That’s all it takes and your app will automatically be deployed to your stagingapp on Heroku. It will recognize that we pushed to the develop branch based on our configuration in the circle.yml.
OR
Pushing to production:
git flow release start 1.0.0 git flow release finish 1.0.0 git push origin master develop --tags
In this case, git-flow will create a release branch/tag which will be merged into master and pushed to GitHub. CI will listen in on that and immediately trigger a production deploy since we pushed to master.
