我们有Jenkins 2设置构建每次推送到github,我们不使用Pull Request构建器(虽然提交是拉请求的一部分,显然也会构建).在GitHub的集成插件说,它只有与拉请求生成器的工作原理,所以这不会为我们工作.
我也尝试了github-notify插件,但它似乎不适用于我们的情况(可能是因为repo是私有的和/或作为Organizaiton的一部分而不是个人用户拥有).我试图让它推断设置以及手动指定credentialsId
,account
,repo
,当然status
参数,都没有运气.
这是我Jenkinsfile的缩写版本:
pipeline { agent { label "centos7" } stages { stage("github => pending") { steps { githubNotify status: "PENDING", credentialsId: "my-credentials-id", account: "my-account", repo: "my-repo" } } stage("build") { ... } } post { success { githubNotify status: "SUCCESS", credentialsId: "my-credentials-id", account: "my-account", repo: "my-repo" } failure { githubNotify status: "FAILURE", credentialsId: "my-credentials-id", account: "my-account", repo: "my-repo" } } }
当我运行构建时,我得到以下内容:
java.lang.IllegalArgumentException: The suplied credentials are invalid to login at org.jenkinsci.plugins.pipeline.githubstatusnotification.GitHubStatusNotificationStep.getGitHubIfValid(GitHubStatusNotificationStep.java:234) at org.jenkinsci.plugins.pipeline.githubstatusnotification.GitHubStatusNotificationStep.getRepoIfValid(GitHubStatusNotificationStep.java:239) at org.jenkinsci.plugins.pipeline.githubstatusnotification.GitHubStatusNotificationStep.access$100(GitHubStatusNotificationStep.java:75) at org.jenkinsci.plugins.pipeline.githubstatusnotification.GitHubStatusNotificationStep$Execution.run(GitHubStatusNotificationStep.java:344) at org.jenkinsci.plugins.pipeline.githubstatusnotification.GitHubStatusNotificationStep$Execution.run(GitHubStatusNotificationStep.java:326) at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47) at hudson.security.ACL.impersonate(ACL.java:221) at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
我已经通过Jenkins(在配置系统区域中)和在浏览器中手动测试了凭据 - 用户名和密码是正确的,并且对相关的repo具有读/写访问权限.
根据Jenkins GitHub插件自己的例子:
void setBuildStatus(String message, String state) { step([ $class: "GitHubCommitStatusSetter", reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/my-org/my-repo"], contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"], errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]], statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ] ]); } ... pipeline { stages { ... } post { success { setBuildStatus("Build succeeded", "SUCCESS"); } failure { setBuildStatus("Build failed", "FAILURE"); } } }
不需要多余的插件.只要您安装并正确配置了GitHub插件,您甚至不需要执行上述操作,它应该自动执行.我们没有使用Pull Request构建器,而是使用Jenkins Multibranch Pipeline.我们只是在我们的PR中使用上面的代码片段来获得额外的状态粒度.
首先,确保这些凭据是全局凭证,而不是文件夹凭据.
后者尚不支持,并会生成类似的错误消息:请参阅JENKINS-42955
(仍在审核中)
其次,如果这些凭证在浏览器中工作但不通过DSL配置文件,则可能是jenkins文件,这可能是由于名称或密码中的特殊字符造成的:查看您是否需要对保留字符进行百分比编码.