【发布时间】:2014-07-29 18:59:55
【问题描述】:
我按照tutorial 设置了一个 Jenkins 作业,以便在向 gitlab 存储库进行推送时运行。我测试了 webhook,我可以看到作业被触发了。但是,我在有效负载中看不到任何内容。
只是想知道,是否有人尝试过读取从 gitlab webhook 收到的有效负载?
【问题讨论】:
我按照tutorial 设置了一个 Jenkins 作业,以便在向 gitlab 存储库进行推送时运行。我测试了 webhook,我可以看到作业被触发了。但是,我在有效负载中看不到任何内容。
只是想知道,是否有人尝试过读取从 gitlab webhook 收到的有效负载?
【问题讨论】:
只要 Gitlab 存储库中发生任何事件,Jenkins Gitlab Plugin 就会向 Jenkins 发送 these POST parameters。
您可以在 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
【讨论】:
echo %gitlabAfter%, ...
您提到的tutorial 谈到了 GitHub webhook。 GitLab 和 GitHub 是两个独立的产品。因此,GitHub webhook 的文档或链接将不适用于 GitLab webhook。
GitLab 使用请求正文中的 JSON 有效负载调用 webhook URL,其中包含有关导致 webhook 调用的 GitLab 事件的大量信息。例如,GitLab webhook push event payload 中包含以下信息:
{
"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 plugin 使此 webhook 有效负载信息在 Jenkins Global Variable env 中可用。 available env variables如下:
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 Global Variable params 读取 Jenkins 作业参数一样,您可以从 Jenkins Global Variable 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 对话,以触发(或不触发)作业,并收集返回的状态。
我的出发点: How do I receive Github Webhooks in Python(最后一个答案,不是选择的那个)。
我在 2 天前开始开发它。已完成,但我仍在验证它。
【讨论】: