Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Jenkins setup to automatically comment on github commits with build status
// Using the Jenkins Groovy Post build plugin to execute the following after every build
// It would be nice not to have to specify these here... the repo name should be available within the hudson
// api somehow, but I didn't know how to get it. The access token should maybe be saved in a config file, and
// read in at runtime?
GITHUB_REPO_NAME = 'myusername/myreponame'
GITHUB_ACCESS_TOKEN = 'my_github_api_v3_access_token'
env =
if( == hudson.model.Result.SUCCESS) {
COMMENT = "This commit <a href=${env.BUILD_URL}>passes</a>"
} else {
COMMENT = "This commit <a href=${env.BUILD_URL}>fails</a>"
pb = new ProcessBuilder('python', '/var/lib/jenkins/')
pb.environment().put('GITHUB_REPO_NAME', GITHUB_REPO_NAME)
pb.environment().put('GIT_COMMIT', env.GIT_COMMIT)
pb.environment().put('COMMENT', COMMENT)
p = pb.start()
manager.listener.logger.println p.text
# /var/lib/jenkins/
import sys, os
sys.stderr = sys.stdout # helpful for debugging groovy calling, so we dont have to check stderr
# using, (pip install PyGithub)
from github import Github
access_token = os.environ['GITHUB_ACCESS_TOKEN']
repo_name = os.environ['GITHUB_REPO_NAME']
commit = os.environ['GIT_COMMIT']
comment = os.environ['COMMENT']
github_user, repo_short_name = repo_name.split('/')
g = Github(access_token)
user = g.get_user(github_user)
repo = user.get_repo(repo_short_name)
commit = repo.get_commit(commit)
print 'python github comment script finished sucessfully'

rmcgibbo commented Sep 4, 2012

And to generate the github access token,

$ curl -u vsp-jenkins-bot -d '{"scopes":["repo"]}'
where vsp-jenkins-bot is the github username I'm using for the comments to be listed under.

swis commented Jan 12, 2013

If one wants to repeat the original commit message, one can replace



commit.create_comment(comment + " " + commit.commit.message)

(Useful, if you mentioned an issue in the message and the build status should go into the issue, too)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment