groovy Jenkins Github插件无法设置状态

u3r8eeie  于 2022-11-01  发布在  Jenkins
关注(0)|答案(3)|浏览(261)

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

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

chhqkbe11#

如果您没有在全局Jenkins配置下设置“GitHub Server”配置,则可能会发生此问题:
管理Jenkins -〉配置系统-〉GitHub
您可以在GitHub插件文档的“自动模式”部分找到有关如何设置服务器配置的更多详细信息:
https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Plugin#GitHubPlugin-AutomaticMode%28Jenkinsmanageshooksforjobsbyitself%29

yh2wf1be

yh2wf1be2#

在经历了同样的问题和插件的痛苦之后,这里有一个修复程序,不是针对这个特定的插件,而是一个不需要插件但仍然可以解决问题的变通方案,使用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'
}

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

1tu0hz3e

1tu0hz3e3#

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

相关问题