Skip to content

Instantly share code, notes, and snippets.

@jollychang
Forked from rmcgibbo/github_comment_on_commit.py
Last active August 29, 2015 14:26
Show Gist options
  • Save jollychang/9e02509a1d65fe3b43e8 to your computer and use it in GitHub Desktop.
Save jollychang/9e02509a1d65fe3b43e8 to your computer and use it in GitHub Desktop.
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
// https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
// 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 = manager.build.getEnvironment(manager.listener)
if(manager.build.result == 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/github_comment_on_commit.py')
pb.environment().put('GITHUB_ACCESS_TOKEN', GITHUB_ACCESS_TOKEN)
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()
p.waitFor()
manager.listener.logger.println p.text
# /var/lib/jenkins/github_comment_on_commit.py
import sys, os
sys.stderr = sys.stdout # helpful for debugging groovy calling, so we dont have to check stderr
# using https://github.com/jacquev6/PyGithub, (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)
commit.create_comment(comment)
print 'python github comment script finished sucessfully'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment