The below method is what I use for all of my status updates to commits, whether they are in PRs or Just commits to a branch like master.
//Pass Status back to GitHub based on Trigger Type.
def setBuildStatus(String message, String state, String repo_url, String job_name, String commit_sha) {
retry(3){
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: repo_url],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: job_name],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: state]],
commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commit_sha],
statusBackrefSource: [$class: "ManuallyEnteredBackrefSource", backref: "${BUILD_URL}display/redirect"],
statusResultSource: [$class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
So using this repo (and plugin) as an example, you would build a status like the following:
repo_url='https://github.com/KostyaSha/github-integration-plugin'
job_name="${JOB_BASE_NAME}"
job_state= can be FAILURE, SUCCESS, PENDING (check gihtub for more)
commit_sha-"${GITHUB_PR_HEAD_SHA}"
message="job completed"
and then call the method
sendBuildStatus(message, job_state, repo_url, job_name, commit_sha)
hope that helps.
Here's what we're using in our Groovy script. It's defined directly in our build job but I assume similar would work from Jenkinsfile.
pipeline {
agent any
stages {
stage("Get source code") {
steps {
checkout([
$class: "GitSCM",
branches: [[name: "origin-pull/pull/${GITHUB_PR_NUMBER}/merge"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [
credentialsId: "abc123",
name: "origin-pull",
refspec: "+refs/pull/${GITHUB_PR_NUMBER}/merge:refs/remotes/origin-pull/pull/${GITHUB_PR_NUMBER}/merge",
url: "[email protected]:yourorg/yourrepo.git"
post {
success {
githubPRComment comment: githubPRMessage("XXX from [build ${BUILD_NUMBER}](${BUILD_URL})."), statusVerifier: allowRunOnStatus("SUCCESS"), errorHandler: statusOnPublisherError("UNSTABLE")
The key is to make sure that githubPRComment is in the post part of the pipeline (else you will get errors as there is no build result object). See Pipeline syntax for post.
Hope that helps.