Reactormonk
Reactormonk

Reputation: 21690

Jenkins Github plugin doesn't set status

I'm trying to set a github status from a Jenkins job. Jenkins returns a

[Set GitHub commit status (universal)] SUCCESS on repos [] (sha:9892fbd) with context:ci/jenkins/tests

... but the status isn't set when I query it with the REST API later.

There's the groovy code:

def getCommitHash() {
    sh(script: """
git rev-parse HEAD
""", returnStdout: true).trim()
}


def setCountTestLocation(String location) {
    url = "https://<internal github>/<org>/<repo>"
    commitHash = getCommitHash()
    print(url)
    print(commitHash)
    step([
            $class: "GitHubCommitStatusSetter",
            reposSource: [$class: "ManuallyEnteredRepositorySource", url: url],
            contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/tests"],
            statusBackrefSource: [$class: "ManuallyEnteredBackrefSource", backref: location],
            errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
            commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commitHash],
            statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: "Tests here!", state: "SUCCESS", location: location]] ]
        ]);
}

Upvotes: 5

Views: 1458

Answers (3)

foobar1101
foobar1101

Reputation: 11

This issue can occur if you have not set up a "GitHub Server" config under the global Jenkins configs:

Manage Jenkins -> Configure System -> GitHub

You can find more details on how to set up a server configuration under the "Automatic Mode" section of the GitHub Plugin documentation:

https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Plugin#GitHubPlugin-AutomaticMode%28Jenkinsmanageshooksforjobsbyitself%29

Upvotes: 1

Jozef
Jozef

Reputation: 2737

After much pain with the same issue and plugin, here is a fix not for this particular plugin, but rather a workaround that does not require a plugin and still solves the issue, using curl. You can add the following to your pipeline:

post {
  success {
    withCredentials([usernamePassword(credentialsId: 'your_credentials_id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
      sh 'curl -X POST --user $USERNAME:$PASSWORD --data  "{\\"state\\": \\"success\\"}" --url $GITHUB_API_URL/statuses/$GIT_COMMIT'
    }
  }
  failure {
    withCredentials([usernamePassword(credentialsId: 'your_credentials_id', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
      sh 'curl -X POST --user $USERNAME:$PASSWORD --data  "{\\"state\\": \\"failure\\"}" --url $GITHUB_API_URL/statuses/$GIT_COMMIT'
    }
  }
}

Where the GITHUB_API_URL is usually constructed like so, for example in the environment directive:

environment {
   GITHUB_API_URL='https://api.github.com/repos/organization_name/repo_name'
}

The credentialsId can be created and obtained from Jenkins -> Credentials

Upvotes: 0

Goran Brkic
Goran Brkic

Reputation: 21

You repository hasn't been updated as it seems that repos were not properly set.
Plugin still reports success as it properly completed its run, but repo list is empty as evident in your message SUCCESS on repos [].

Upvotes: 2

Related Questions