Tutorial on pushing to Heroku via Jenkins

| | August 4, 2015

All anyone know any good articles on getting Jenkins to play nicely with Heroku?

What I want to do:
1) Want to setup a jenkins job to poll a private GitHub Repo when checkin’s are made to developer branch.
2) Build this branch and make sure everything is good!
3) Push private GitHub Repo codebase to Heroku Repo. So it does the build and deploy on Heroku.

I’ve seen bits and pieces of articles but can’t seem to get the complete flow to work. I’ve tried the GitHub plugin & Heroku plugin. I CAN get the github plugin to pull down and build but don’t understand how to then push to Heroku. Heroku plugin lets me deploy WAR file but that doesn’t bring up the app correctly. So I need to just push the codebase to the Heroku Repo so it does the compile and deploy.


3 Responses to “Tutorial on pushing to Heroku via Jenkins”

  1. First you should switch your project to use ssh-keys for authentication and add your public ssh-key to your heroku account:

    heroku git:remote --ssh-git
    heroku keys:add


    git remote -v
    heroku  git@heroku.com:your-project.git (fetch)
    heroku  git@heroku.com:your-project.git (push)

    you can figure out the URL to use as repository URL in your Jenkins configuration. Then follow the instructions given in the answer from @Andy Glover (see also http://thediscoblog.com/blog/2014/01/24/continuous-delivery-for-heroku-with-jenkins/) to configure your Jenkins.

    Finally to allow Jenkins to push to your git repository at Heroku, you should ask your Jenkins administrator to give you the public ssh key of the root/jenkins user executing your Jenkins job. Add this public key via your Heroku dashboard to your Heroku account. If your build job is successful, Jenkins will then be able to push to your Heroku account, resulting in a deployment.

  2. As an alternative to using the Heroku API as outlined above to deploy, you can simply push your code to a remote Git repository (i.e. the one Heroku defined for your app) as a Post-Build Action. Your job would therefore define two Git repositories — one being your Github repository and another being the Heroku one.

    enter image description here

    Give the Heroku repository a name, such as ‘heroku’ and in the Post-Build Actions section, use a Git publisher. Be sure to select the heroku name in the Target Remote name field.
    enter image description here

    Depending on how you’ve set up your Build Trigger on your Github project, when a build completes, Jenkins will push the resultant snapshot to the Heroku repository, resulting in a deployment.

  3. I use Jenkins to push to Heroku for our apps. I dont use the Heroku plugin, I like the control that ‘Execute Shell’ gives me. This is a rather detailed answer, if I missed anything be sure to comment.

    1) Polling a Private Repo :

    • Your job should be set up with the option ‘Build when a change is
      pushed to GitHub’ under the ‘Build Triggers’ section.
    • on GitHub go to your project page, and click on the Settings menu in the
      header (must have admin access). In the left sidebar of that page click ‘Service Hooks’.
      This will take you to a list of hooks you can choose. Select ‘Jenkins (GitHub plugin)’.
    • Add the callback URL of your jenkins server (leave that page open for later). Something like :


    • You can test the callback by adding a ‘Log Recorder’ from Jenkins, by going to Jenkins|Manage Jenkins|System Log. Click ‘Add New Log Recorder’
    • Enter ‘test hook’, Set Logger to ‘com.cloudbees.jenkins.GitHubWebHook’, Set Log Level to ‘All’
    • Go back to GitHub, and click ‘Test Hook’, you can then see the callback log to confirm your hook is working.

    2) Build the branch. Be sure you have all the GitHub configs set, as the callback will trigger the job only if these settings are done.

    • In the ‘Source Code Management’ section, Select ‘Git’ option and fill in the details of your repo, e.g. ‘git@github.com:…’
    • In the ‘Build Triggers’ section, Select ‘Build when a change is pushed to GitHub’

    3) Push to Heroku. A few things to consider here.

    • You need to ensure your job had the Heroku remote repo added. When creating a new job this is a ONE time action, and does not need to be done for every build. For example :
    heroku git:remote -a myApp
    git remote add heroku git@heroku.com:myApp.git

    create an Execute Shell script with just the above, for use only on your first build.

    • If you have Ping Targets (New Relic), disable them during deploy to avoid false notifications that your app is down.
    curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/disable -X POST -H "X-Api-Key: APIKEY"

    Dont forget to turn it back on after:

    curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/enable -X POST -H "X-Api-Key: APIKEY"
    • Do the same for Maintenance Mode on the App
    heroku maintenance:on --app myApp
    heroku maintenance:off --app myApp

    Putting this together, a typical deploy script on Jenkins may look like this :

    #one off to ensure heroku remote is added to repo
    heroku git:remote -a myApp
    git remote add heroku git@heroku.com:myApp.git
    curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/disable -X POST -H "X-Api-Key: APIKEY"
    heroku maintenance:on --app myApp
    #push to heroku
    git push --force heroku master
    heroku run rake db:migrate --app myApp
    curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/enable -X POST -H "X-Api-Key: APIKEY"
    heroku maintenance:off --app myApp

Leave a Reply