如何使用Jenkinsfile设置github提交状态而不使用拉请求生成器

如何使用Jenkinsfile设置github提交状态而不使用拉请求生成器,github,jenkins,jenkins-pipeline,jenkins-2,Github,Jenkins,Jenkins Pipeline,Jenkins 2,我们将Jenkins 2设置为构建每次推送到github的功能,并且我们不使用拉请求生成器(尽管作为拉请求一部分的提交显然也会被构建)。声明它只与pull请求生成器一起工作,所以这对我们不起作用 我也尝试过,但它似乎不适用于我们的案例(可能是因为回购协议是私有的和/或作为组织的一部分而不是个人用户拥有的)。我尝试过让它推断设置,以及手动指定凭证ID,账户,回购,当然还有状态参数,但都没有运气 下面是我的詹金斯档案目前的简略版本: pipeline { agent { label "cen

我们将Jenkins 2设置为构建每次推送到github的功能,并且我们不使用拉请求生成器(尽管作为拉请求一部分的提交显然也会被构建)。声明它只与pull请求生成器一起工作,所以这对我们不起作用

我也尝试过,但它似乎不适用于我们的案例(可能是因为回购协议是私有的和/或作为组织的一部分而不是个人用户拥有的)。我尝试过让它推断设置,以及手动指定
凭证ID
账户
回购
,当然还有
状态
参数,但都没有运气

下面是我的詹金斯档案目前的简略版本:

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的读/写权限。

首先,确保这些凭据是全局凭据,而不是文件夹凭据。
后者尚不受支持,并将生成类似的错误消息:请参阅(仍在审查中)


其次,如果这些凭证在浏览器中工作,但不是通过DSL配置文件,则可能是由于名称或密码中的特殊字符造成的:请查看您是否不必这样做。

我没有想到,
帐户
参数中的值必须与凭证中的用户不匹配。在
account
中,必须指定存储库所有者。在
credentialsId
中,您可以使用任何用户访问存储库:

credentialsId
:要使用的github凭据的id必须是
用户名和密码类型
。确保凭据具有写访问权限,因为:具有推送访问权限的用户可以为给定引用创建提交状态

帐户
:拥有存储库的帐户

根据:


没有多余的插件是必要的。只要安装并正确配置了GitHub插件,就不需要执行上述操作,它应该会自动执行。我们也不使用Pull请求生成器,而是使用Jenkins多分支管道。我们只是在PR中使用上面的代码片段来增加状态粒度

文档中的一个更好的例子:

def getRepoURL() {
  sh "git config --get remote.origin.url > .git/remote-url"
  return readFile(".git/remote-url").trim()
}

def getCommitSha() {
  sh "git rev-parse HEAD > .git/current-commit"
  return readFile(".git/current-commit").trim()
}

def updateGithubCommitStatus(build) {
  // workaround https://issues.jenkins-ci.org/browse/JENKINS-38674
  repoUrl = getRepoURL()
  commitSha = getCommitSha()

  step([
    $class: 'GitHubCommitStatusSetter',
    reposSource: [$class: "ManuallyEnteredRepositorySource", url: repoUrl],
    commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commitSha],
    errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
    statusResultSource: [
      $class: 'ConditionalStatusResultSource',
      results: [
        [$class: 'BetterThanOrEqualBuildResult', result: 'SUCCESS', state: 'SUCCESS', message: build.description],
        [$class: 'BetterThanOrEqualBuildResult', result: 'FAILURE', state: 'FAILURE', message: build.description],
        [$class: 'AnyBuildResult', state: 'FAILURE', message: 'Loophole']
      ]
    ]
  ])
}

如果您不想使用专门的插件,这里有一个使用
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\u API\u URL
通常是这样构造的,例如在
环境
指令中:

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

credentialsId
可以从
Jenkins->Credentials

创建和获取凭据。凭据是全局凭据。唯一的特殊字符是连字符——否则全是字母数字。我犯了同样的错误,所有这些都没有成功。我建议任何人按照James的回答使用Jenkins Github插件。你能通过@Jacob手动设置这些凭据的状态吗?@Jacob是的——凭据是正确的(用户名与密码匹配,等等)我仍然不清楚您是否能够通过API执行该操作。如果是的话,为什么不绕过插件,通过API自己调用GitHub呢?是的,我可以——我只是用Python的凭据登录了。我可以在Jenkins UI的配置系统部分中执行各种“测试凭据”操作;我无法让githubNotify插件正常工作,如这里所述。我尝试了很多方法让github notify插件正常工作,包括其他答案和评论中的所有建议。您的方法在大约5分钟内工作:-DHello@James,当pull请求开始生成时,您如何设置“挂起”状态?非常感谢。@Ricardo在运行状态为“待定”的管道时,您只需立即调用
setBuildStatus
。Google GHCommitState以查看您的选项。存储库的硬编码URL可以由自身替换为
env.GIT\u URL
@Flux它只会报告整个构建挂起/成功/失败。但是,通过使用上面的代码片段,您可以添加粒度。GitHub支持显示多个状态检查,只需为每个状态检查使用不同的上下文即可。请注意,文档会继续声明(我已经测试过)您的帐户密码或访问令牌可以在
用户名和密码
凭据中使用。
environment {
   GITHUB_API_URL='https://api.github.com/repos/organization_name/repo_name'
}