Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorial on pushing to Heroku via Jenkins

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

What I want to do is:

1) Setup a Jenkins job to poll a private GitHub Repo when check-ins 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 I don't understand how to push to Heroku. Heroku plugin lets me deploy a 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.

like image 840
Dharmesh Patel Avatar asked May 30 '13 15:05

Dharmesh Patel


2 Answers

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.

like image 171
Andy Glover Avatar answered Oct 23 '22 12:10

Andy Glover


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 :

    http://jenkins.example.com/github-webhook/

  • 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. '[email protected]:...'
  • 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 [email protected]: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 [email protected]:myApp.git
#disbales
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
#enables
curl https://heroku.newrelic.com/accounts/ACCTID/applications/APPID/ping_targets/enable -X POST -H "X-Api-Key: APIKEY"
heroku maintenance:off --app myApp
like image 41
blotto Avatar answered Oct 23 '22 13:10

blotto