Jenkins Github插件没有设置状态

时间:2018-01-10 14:19:54

标签: github jenkins groovy

我试图从Jenkins工作中设置github状态。詹金斯回归

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

...但是当我稍后使用REST API查询状态时,状态不会设置。

还有常规代码:

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]] ]
        ]);
}

2 个答案:

答案 0 :(得分:1)

您的存储库尚未更新,因为似乎未正确设置存储库。
插件在正确完成运行后仍报告成功,但是回购列表为空,如消息SUCCESS on repos []所示。

答案 1 :(得分:0)

在经历了相同的问题和插件的痛苦之后,这不是针对此特定插件的修复程序,而是一种解决方法,它不需要插件,仍然可以使用curl解决该问题。您可以将以下内容添加到管道中:

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'
    }
  }
}

GITHUB_API_URL的结构通常是这样的,例如在environment指令中:

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

可以从credentialsId创建和获取Jenkins -> Credentials