0

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

The Jenkins job that I have, is a pipeline job that has the below enabled: Build Trigger: GitHub hook trigger for GITScm polling

With the above configuration, I see that in response to an event ie; push/PR in GitHub, the jenkins job gets triggered successfully. In GitHub, under Recent Deliveries for the web hook, I see the details of the payload and a successful Response of 200.

I am trying to get the payload in Jenkins Pipeline for further processing. I need some details eg: PR URL/PR number, refs type, branch name etc for conditional processing in the Jenkins Pipeline.

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 ?

SoDa
  • 71
  • 2
  • 8

2 Answers2

1

You need to select Content type: application/json in your webhook in GitHub. Then you would be able to access any variable from the payload GitHub sends as follows: $. pull_request.url for pr url, for example.

  • @bad_coder - thanks for the edit. I am new to this platform, still learning the nitty-gritties. –  Sep 06 '21 at 10:06
0

Unsure if this is possible.

With the GitHub plugin we use (Pipeline Github), PR number is stored in the variable CHANGE_ID. PR URL is pretty easy to generate given the PR number. Branch name is stored in the variable BRANCH_NAME. In case of pull requests, the global variable pullRequest is populated with lots of data.

Missing information can be obtained from Github by using their API. Here's an example of checking if PR is "behind", you can modify that to your specific requirements:

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.")
        }
    }
}
MaratC
  • 6,418
  • 2
  • 20
  • 27