Noa*_*fit 10 parameters webhooks jenkins jenkins-plugins gitlab
当我推进一个特定的分支时,我正在尝试使用Gitlab webhook在我的jenkins上建立一个工作.我唯一的问题是jenkins工作是参数化的.参数是分支名称.是否可以选择将分支名称(每次都是一个不同的分支)添加到webhook中,以便将其自动插入到jenkins作业参数的参数中?
谢谢
GitLab使用请求主体中的JSON负载调用Webhook URL,该请求中包含有关导致Webhook调用的GitLab事件的大量信息。GitLab 推送事件的JSON有效负载中确实包含分支名称信息-请参阅GitLab webhook推送事件有效负载中的“ ref ”字段:
{
"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
}
Run Code Online (Sandbox Code Playgroud)
您不能将webhook有效负载中的任何字段自动绑定到Jenkins作业参数。但是,仍然可以在Jenkins作业中访问Webhook有效负载信息。该詹金斯GitLab插件使得可用该网络挂接有效载荷信息詹金斯全局变量 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
Run Code Online (Sandbox Code Playgroud)
就像您在作业管道脚本中从Jenkins全局变量 参数中读取Jenkins作业参数一样,您可以从Jenkins全局变量 env中读取webhook有效负载字段:
params.MY_PARAM_NAME
env.gitlabBranch
Run Code Online (Sandbox Code Playgroud)
希望以上信息能帮助您解决问题。
您可以使用Generic Webhook Trigger 插件来做到这一点。在GitLab 违规评论页面中还有一个将该插件与 GitLab 一起使用的示例。
源分支通过以下 JSONPath 进行解析:
$.object_attributes.source_branch
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3850 次 |
| 最近记录: |