Read json payload from gitlab webhook in Jenkins
我按照本教程设置了一个Jenkins作业,只要将其推送到gitlab存储库即可运行。 我测试了Webhook,可以看到作业已触发。 但是,我在负载中看不到任何东西。
只是想知道,是否有人尝试读取从gitlab webhook接收到的有效负载?
每当Gitlab存储库中发生任何事件时,Jenkins Gitlab插件都会将这些POST参数发送给Jenkins。
您可以在Jenkins控制台中添加
例如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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推送事件有效负载中包含以下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | { "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":"[email protected]", "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":"[email protected]: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":"[email protected]:mike/diaspora.git", "ssh_url":"[email protected]:mike/diaspora.git", "http_url":"http://example.com/mike/diaspora.git" }, "repository":{ "name":"Diaspora", "url":"[email protected]:mike/diaspora.git", "description":"", "homepage":"http://example.com/mike/diaspora", "git_http_url":"http://example.com/mike/diaspora.git", "git_ssh_url":"[email protected]: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":"[email protected]" }, "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变量如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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负载字段:
1 2 | 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}。
Doc:https://github.com/elvanja/jenkins-gitlab-hook-plugin#parameterized-projects
但是请注意,如果使用/ gitlab / notifycommit,它将无法正常工作,因为触发Jenkins人与实际开始工作之间存在间隙(民意测验)。在这种情况下,所有有效负载数据均为空。
但是要小心使用/ gitlab / buildnow,因为您无法控制是否要构建,例如Maven提交一些文件时,并且不应触发构建。
我所做的是用Python编写了一个小工具来接收所有gitlab通知,然后该工具与GitLab和Jenkins进行对话,以触发(或不触发)工作并收集状态。
我的出发点:
如何使用Python接收Github Webhooks(最后一个答案,而不是所选答案)。
我两天前开始开发它。完成了,但我仍在验证。