簡體   English   中英

從 Jenkins 中的 gitlab webhook 讀取 json 負載

[英]Read json payload from gitlab webhook in Jenkins

我按照本教程設置了一個 Jenkins 作業,以便在推送到 gitlab 存儲庫時運行。 我測試了 webhook,我可以看到作業被觸發。 但是,我在有效負載中沒有看到任何內容。

只是想知道,是否有人曾嘗試讀取從 gitlab webhook 收到的有效負載?

每當 Gitlab 存儲庫中發生任何事件時, Jenkins Gitlab 插件都會將這些 POST 參數發送給 Jenkins。

您可以在 Jenkins 控制台中添加env以獲取所有 Gitlab 參數導出到 Jenkins 環境的內容。 然后您可以打印或使用所需的變量。

例如

echo $gitlabSourceRepoURL
echo $gitlabAfter
echo $gitlabTargetBranch
echo $gitlabSourceRepoHttpUrl
echo $gitlabMergeRequestLastCommit
echo $gitlabSourceRepoSshUrl
echo $gitlabSourceRepoHomepage
echo $gitlabBranch
echo $gitlabSourceBranch
echo $gitlabUserEmail
echo $gitlabBefore
echo $gitlabSourceRepoName
echo $gitlabSourceNamespace
echo $gitlabUserName

您提到的教程討論了 GitHub webhooks。 GitLab 和 GitHub 是兩個獨立的產品。 因此,GitHub webhooks 的文檔或鏈接不適用於 GitLab webhooks。

GitLab 使用請求正文中的 JSON 有效負載調用 Webhook URL,該有效負載包含有關導致 Webhook 調用的 GitLab 事件的大量信息。 例如, GitLab webhook 推送事件負載中攜帶以下信息:

{
  "object_kind": "push",
  "before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
  "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  "ref": "refs/heads/master",
  "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  "user_id": 4,
  "user_name": "John Smith",
  "user_username": "jsmith",
  "user_email": "john@example.com",
  "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
  "project_id": 15,
  "project":{
    "id": 15,
    "name":"Diaspora",
    "description":"",
    "web_url":"http://example.com/mike/diaspora",
    "avatar_url":null,
    "git_ssh_url":"git@example.com:mike/diaspora.git",
    "git_http_url":"http://example.com/mike/diaspora.git",
    "namespace":"Mike",
    "visibility_level":0,
    "path_with_namespace":"mike/diaspora",
    "default_branch":"master",
    "homepage":"http://example.com/mike/diaspora",
    "url":"git@example.com:mike/diaspora.git",
    "ssh_url":"git@example.com:mike/diaspora.git",
    "http_url":"http://example.com/mike/diaspora.git"
  },
  "repository":{
    "name": "Diaspora",
    "url": "git@example.com:mike/diaspora.git",
    "description": "",
    "homepage": "http://example.com/mike/diaspora",
    "git_http_url":"http://example.com/mike/diaspora.git",
    "git_ssh_url":"git@example.com:mike/diaspora.git",
    "visibility_level":0
  },
  "commits": [
    {
      "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "message": "Update Catalan translation to e38cb41.",
      "timestamp": "2011-12-12T14:27:31+02:00",
      "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "author": {
        "name": "Jordi Mallach",
        "email": "jordi@softcatala.org"
      },
      "added": ["CHANGELOG"],
      "modified": ["app/controller/application.rb"],
      "removed": []
    },
    {
      "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "message": "fixed readme",
      "timestamp": "2012-01-03T23:36:29+02:00",
      "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "author": {
        "name": "GitLab dev user",
        "email": "gitlabdev@dv6700.(none)"
      },
      "added": ["CHANGELOG"],
      "modified": ["app/controller/application.rb"],
      "removed": []
    }
  ],
  "total_commits_count": 4
}

Jenkins GitLab 插件使這個 webhook 有效負載信息在Jenkins 全局變量env 中可用。 可用的ENV變量如下:

gitlabBranch
gitlabSourceBranch
gitlabActionType
gitlabUserName
gitlabUserEmail
gitlabSourceRepoHomepage
gitlabSourceRepoName
gitlabSourceNamespace
gitlabSourceRepoURL
gitlabSourceRepoSshUrl
gitlabSourceRepoHttpUrl
gitlabMergeRequestTitle
gitlabMergeRequestDescription
gitlabMergeRequestId
gitlabMergeRequestIid
gitlabMergeRequestState
gitlabMergedByUser
gitlabMergeRequestAssignee
gitlabMergeRequestLastCommit
gitlabMergeRequestTargetProjectId
gitlabTargetBranch
gitlabTargetRepoName
gitlabTargetNamespace
gitlabTargetRepoSshUrl
gitlabTargetRepoHttpUrl
gitlabBefore
gitlabAfter
gitlabTriggerPhrase

就像您從作業管道腳本中的Jenkins 全局變量參數讀取 Jenkins 作業參數一樣,您可以從Jenkins 全局變量env讀取 webhook 有效負載字段:

echo "My Jenkins job parameter is ${params.MY_PARAM_NAME}"
echo "One of Jenkins job webhook payload field is ${env.gitlabTargetBranch}"

希望以上信息能幫助您解決問題。

我做到了。 它適用於某些場景。

如果您使用 /gitlab/buildnow,則可以訪問有效負載對象。 他們都。 但是您必須將它們命名為“此構建已參數化”。 然后您可以按名稱訪問它們,例如 ${AUTHOR_NAME}。

文檔: https : //github.com/elvanja/jenkins-gitlab-hook-plugin#parameterized-projects

但請注意,如果您使用 /gitlab/notifycommit,它將不起作用,因為在觸發 jenkins 和實際開始工作之間存在差距(輪詢)。 這種情況下的所有有效載荷數據都是空的。

但是要小心使用/gitlab/buildnow,因為您無法控制是否要構建,例如Maven何時提交回一些文件,並且不應觸發構建。

我所做的是用 Python 編寫一個小工具來接收所有 gitlab 通知,這個工具會與 GitLab 和 Jenkins 對話,以觸發(或不觸發)作業,並收集回狀態。

我的起點: 我如何在 Python 中接收 Github Webhooks (最后一個答案,而不是選擇的答案)。

我兩天前開始開發它。 它已經完成,但我仍在驗證它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM