简体   繁体   English

无法从詹金斯管道中的 GitHub 网络钩子触发器获取有效负载

[英]Unable to get the payload from GitHub web hook trigger in jenkins pipeline

I have configured a Github web hook with the below settings: Payload URL: https:///github-webhook/ Content Type: application/x-www-form-urlencoded Events : Pushes, Pull Requests我已经使用以下设置配置了一个 Github 网络钩子:有效负载 URL:https:///github-webhook/ 内容类型:application/x-www-form-urlencoded 事件:推送、拉取请求

The Jenkins job that I have, is a pipeline job that has the below enabled: Build Trigger: GitHub hook trigger for GITScm polling我拥有的 Jenkins 作业是一个管道作业,它启用了以下功能:构建触发器:用于 GITScm 轮询的 GitHub 钩子触发器

With the above configuration, I see that in response to an event ie;通过上述配置,我看到响应事件即; push/PR in GitHub, the jenkins job gets triggered successfully.在 GitHub 中 push/PR,jenkins 作业被成功触发。 In GitHub, under Recent Deliveries for the web hook, I see the details of the payload and a successful Response of 200.在 GitHub 中,在 Web hook 的“Recent Deliveries”下,我看到了有效负载的详细信息和 200 的成功响应。

I am trying to get the payload in Jenkins Pipeline for further processing.我正在尝试在 Jenkins Pipeline 中获取有效负载以进行进一步处理。 I need some details eg: PR URL/PR number, refs type, branch name etc for conditional processing in the Jenkins Pipeline.我需要一些详细信息,例如:PR URL/PR 编号、引用类型、分支名称等,以便在 Jenkins 管道中进行条件处理。

I tried accessing the "payload" variable (as mentioned in other stack overflow posts and the documentations available around) and printing it as part of the pipeline, but I have had no luck yet.我尝试访问“有效负载”变量(如其他堆栈溢出帖子和可用文档中所述)并将其作为管道的一部分打印,但我还没有运气。

So my question is, How can I get the payload from GitHub web hook trigger in my Jenkins Pipeline ?所以我的问题是,如何从 Jenkins 管道中的 GitHub 网络钩子触发器获取有效负载?

Unsure if this is possible.不确定这是否可能。

With the GitHub plugin we use (Pipeline Github), PR number is stored in the variable CHANGE_ID .对于我们使用的 GitHub 插件(Pipeline Github),PR 编号存储在变量CHANGE_ID PR URL is pretty easy to generate given the PR number.给定 PR 编号,PR URL 很容易生成。 Branch name is stored in the variable BRANCH_NAME .分支名称存储在变量BRANCH_NAME In case of pull requests, the global variable pullRequest is populatedwith lots of data .在拉取请求的情况下,全局变量pullRequest填充了大量数据

Missing information can be obtained from Github by using their API.可以使用他们的 API 从 Github 获取丢失的信息。 Here's an example of checking if PR is "behind", you can modify that to your specific requirements:这是检查 PR 是否“落后”的示例,您可以根据具体要求对其进行修改:

def checkPrIsNotBehind(String repo) {
    withCredentials([usernamePassword(credentialsId: "<...>", 
            passwordVariable: 'TOKEN', 
            usernameVariable: 'USER')]) {
        def headers = ' -H "Content-Type: application/json" -H "Authorization: token $TOKEN" '
        def url = "https://api.github.com/repos/<...>/<...>/pulls/${env.CHANGE_ID}"
        def head_sha = sh (label: "Check PR head SHA", 
                              returnStdout: true,
                              script: "curl -s ${url} ${headers} | jq -r .head.sha").trim().toUpperCase()
        println "PR head sha is ${head_sha}"
        
        headers = ' -H "Accept: application/vnd.github.v3+json" -H "Authorization: token $TOKEN" '
        url = "https://api.github.com/repos/<...>/${repo}/compare/${pullRequest.base}...${head_sha}"
        def behind_by = sh (label: "Check PR commits behind", 
                              returnStdout: true,
                              script: "curl -s ${url} ${headers} | jq -r .behind_by").trim().toUpperCase()
        
        if (behind_by != '0') {
            currentBuild.result = "ABORTED"
            currentBuild.displayName = "#${env.BUILD_NUMBER}-Out of date"
            error("The head ref is out of date. Please update your branch.")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM