Jenkins的Github插件没有设置状态。

5

我正在尝试从Jenkins工作中设置github状态。Jenkins返回一个

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

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

以下是Groovy代码:

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]] ]
        ]);
}
3个回答

2

你的代码库似乎没有被更新,这可能是因为代码库未正确设置。
插件仍然报告成功,因为它已经完成了它的运行,但是根据你的信息SUCCESS on repos [],代码库列表为空。

注:"Original Answer"可以翻译成"原始回答"或者"初始回答",具体要看上下文语境。

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 -> 凭据创建并获取。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接