Show current state of Jenkins build on GitHub repo
Ok, here's how you can set up Jenkins to set GitHub build statuses. This assumes you've already got Jenkins with the GitHub plugin configured to do builds on every push.
-
Go to GitHub, log in, go to Settings, Developer Settings, Personal access tokens and click on Generate new token.
-
Check repo:status (I'm not sure this is necessary, but I did it, and it worked for me).
-
Generate the token, copy it.
-
Make sure the GitHub user you're going to use is a repository collaborator (for private repos) or is a member of a team with push and pull access (for organization repos) to the repositories you want to build.
-
Go to your Jenkins server, log in.
-
Manage Jenkins → Configure System
-
Under GitHub Web Hook select Let Jenkins auto-manage hook URLs, then specify your GitHub username and the OAuth token you got in step 3.
-
Verify that it works with the Test Credential button. Save the settings.
-
Find the Jenkins job and add Set build status on GitHub commit to the post-build steps
That's it. Now do a test build and go to GitHub repository to see if it worked. Click on Branches in the main repository page to see build statuses.
You should see green checkmarks:
In the meanwhile the UI of Jenkins and GitHub has changed a bit and it took me a while to figure out how to configure Jenkins now correctly. The explanation here is based on Jenkins version 2.121.1.
I also assume that you have already configured your Jenkins Job be triggered by a webhook or by polling. Those are the steps that I have taken to get it working:
- Configure Github: Create Personal Access Token with OAuth Scope
repo:status
- Configure Jenkins:
Configure System
and add the OAuth Secret as a GitHub Server - useSecret Text
as an authentication method to put the OAuth Secret in there. - Configure your Jenkins Job: Add
Set GitHub commit status
as Post-build action. Set the Status Result toOne of the default messages and statuses
. - Check your result on GitHub: Check if you get the build status and build execution duration on your GitHub commit.
Configure Github
Configure Jenkins
Configure Jenkins Job
Result
You will now see the status for your commits and branches:
What I did is quite simple:
- Install the Hudson Post Task Plugin
- Create a Personal Access Token here : https://github.com/settings/tokens
-
Add a Post Task Plugin that always put success
curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"success\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build has succeeded!\" }"
-
Add a Post Task Plugin that will put failure if "marked build as failure"
curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"failure\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build has failed!\" }"
-
You can also add a call to pending at the beginning of tests
curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"pending\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build is pending!\" }"
This plugin should work: https://wiki.jenkins-ci.org/display/JENKINS/Embeddable+Build+Status+Plugin
You should be able to embed badges like this into your README.md
file:
The Commit Status API allows you to see the "Repo Statuses API".
And since April 26th 2013, you now can see the build status on your GitHub repo branch page:
That means it is another way, by visiting the GitHub project page, to see those statuses instead of having only Jenkins.
Starting April 30th, 2013, the API endpoint for commit statuses has been extended to allow branch and tag names, as well as commit SHAs.