簡體   English   中英

無法從詹金斯管道中的 GitHub 網絡鈎子觸發器獲取有效負載

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

我已經使用以下設置配置了一個 Github 網絡鈎子:有效負載 URL:https:///github-webhook/ 內容類型:application/x-www-form-urlencoded 事件:推送、拉取請求

我擁有的 Jenkins 作業是一個管道作業,它啟用了以下功能:構建觸發器:用於 GITScm 輪詢的 GitHub 鈎子觸發器

通過上述配置,我看到響應事件即; 在 GitHub 中 push/PR,jenkins 作業被成功觸發。 在 GitHub 中,在 Web hook 的“Recent Deliveries”下,我看到了有效負載的詳細信息和 200 的成功響應。

我正在嘗試在 Jenkins Pipeline 中獲取有效負載以進行進一步處理。 我需要一些詳細信息,例如:PR URL/PR 編號、引用類型、分支名稱等,以便在 Jenkins 管道中進行條件處理。

我嘗試訪問“有效負載”變量(如其他堆棧溢出帖子和可用文檔中所述)並將其作為管道的一部分打印,但我還沒有運氣。

所以我的問題是,如何從 Jenkins 管道中的 GitHub 網絡鈎子觸發器獲取有效負載?

不確定這是否可能。

對於我們使用的 GitHub 插件(Pipeline Github),PR 編號存儲在變量CHANGE_ID 給定 PR 編號,PR URL 很容易生成。 分支名稱存儲在變量BRANCH_NAME 在拉取請求的情況下,全局變量pullRequest填充了大量數據

可以使用他們的 API 從 Github 獲取丟失的信息。 這是檢查 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