Continuous Delivery for Heroku With Jenkins
A continuous delivery pipeline that leverages Jenkins and targets Heroku is fairly simple to set up, provided you install the Jenkins Git plugin. With this pipeline, changes to a specific Git branch will result in a Heroku deployment.
For this deployment process to work nicely, you should use at least two Git branches, as you’ll want to have one branch targeted for auto-deploys and another that doesn’t (as it represents active development). For example, following the git-flow convention, those two branches could be named development
and master
, where changes to master
are deployed to Heroku and changes to development
aren’t. Thus, you will have at least two Jenkins jobs that monitor each of these branches.
Naturally, this pipeline process is language agnostic – Node, Ruby, Java – it doesn’t matter what you do during your build as this entire process is choreographed via Git.
When approaching Heroku auto-deployment from Jenkins, don’t bother with Heroku’s API because it’s much easier to use the Git publisher feature of Jenkins to push a branch from your repository to Heroku (which uses Git anyway).
At a high level, you’ll need to define a Jenkins job that monitors your master
Git branch; if there are changes, this job will run whatever your build needs to do and as a post-build step you can publish that branch to Heroku. It’s that easy.
To configure this pipeline, you will need the Git plugin. With the Git plugin installed, create a job and in the Source Code management section, add your source Git repository and then add another repository which is the Heroku remote repository.
Be sure to give the Heroku repository a name like heroku
. This is done by clicking the Advanced button under the Credentials section.
Second, in the Post-build Actions section, you’ll configure a Git Publisher.
In this case, the Git repository you are going to publish to will be the Heroku one defined earlier. Hit the Add Branch button and be sure to indicate the master
branch as the Branch to push and the Target remote name should be the name your gave to the remote Heroku repository in the Source Code Management section (i.e. heroku
).
Depending on how you’ve set up your Build Trigger on your job, when a build completes, Jenkins will push the resultant snapshot to the Heroku repository, resulting in a deployment! Now wasn’t that easy, man?