此插件是git webhook的高階應用,安裝后會暴露出來一個公共API,GWT插件接收到 JSON 或 XML 的 HTTP POST 請求后,根據我們配置的規則決定觸發哪個Jenkins項目。
定義需要的變量
定義token,此token將用於gitlab的調用
正則匹配,只能匹配到設定的規則還會觸發構建,只有滿足 production與master分支,且git項目名滿足匹配字母或- 大於1個,且觸發者(代碼提交者)不能是yh,才能觸發構建。
在gitlab項目里添加webhook
https://jenkins.example.com/generic-webhook-trigger/invoke?token=xxxxx
點擊觸發測試
效果,可以成功觸發
gitlab傳遞到jenkins的數據在gitlab的webhook url的編輯里面 view detail,可自行獲取想要的變量值
{
"object_kind": "push",
"event_name": "push",
"before": "a2e3c8d96b30967da1fa9579096d52c2b3757d2a",
"after": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"ref": "refs/heads/master",
"checkout_sha": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"message": null,
"user_id": 324,
"user_name": "h_y",
"user_username": "h_y",
"user_email": "",
"user_avatar": null,
"project_id": 3199,
"project": {
"id": 3199,
"name": "hello",
"description": "",
"web_url": "http://gitlab.example.com/h_y/hello",
"avatar_url": null,
"git_ssh_url": "git@gitlab.example.com:h_y/hello.git",
"git_http_url": "http://gitlab.example.com/h_y/hello.git",
"namespace": "h_y",
"visibility_level": 0,
"path_with_namespace": "h_y/hello",
"default_branch": "master",
"ci_config_path": null,
"homepage": "http://gitlab.example.com/h_y/hello",
"url": "git@gitlab.example.com:h_y/hello.git",
"ssh_url": "git@gitlab.example.com:h_y/hello.git",
"http_url": "http://gitlab.example.com/h_y/hello.git"
},
"commits": [
{
"id": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"message": "type\n",
"title": "type",
"timestamp": "2020-05-28T15:09:37+08:00",
"url": "http://gitlab.example.com/h_y/hello/-/commit/9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"author": {
"name": "h_y",
"email": "h_y@example.com"
},
"added": [
],
"modified": [
"Jenkinsfile"
],
"removed": [
]
},
{
"id": "a49de07609ad97132c0c42aca35c75694ab80085",
"message": "type\n",
"title": "type",
"timestamp": "2020-05-28T15:08:47+08:00",
"url": "http://gitlab.example.com/h_y/hello/-/commit/a49de07609ad97132c0c42aca35c75694ab80085",
"author": {
"name": "h_y",
"email": "h_y@example.com"
},
"added": [
],
"modified": [
"Jenkinsfile"
],
"removed": [
]
},
{
"id": "a2e3c8d96b30967da1fa9579096d52c2b3757d2a",
"message": "type\n",
"title": "type",
"timestamp": "2020-05-28T15:07:58+08:00",
"url": "http://gitlab.example.com/h_y/hello/-/commit/a2e3c8d96b30967da1fa9579096d52c2b3757d2a",
"author": {
"name": "h_y",
"email": "h_y@example.com"
},
"added": [
],
"modified": [
"Jenkinsfile"
],
"removed": [
]
}
],
"total_commits_count": 3,
"push_options": {
},
"repository": {
"name": "hello",
"url": "git@gitlab.example.com:h_y/hello.git",
"description": "",
"homepage": "http://gitlab.example.com/h_y/hello",
"git_http_url": "http://gitlab.example.com/h_y/hello.git",
"git_ssh_url": "git@gitlab.example.com:h_y/hello.git",
"visibility_level": 0
}
}
靜默期配置
最近發現webhook觸發的構建會有幾秒的靜默期,如下圖。
現需將靜默期縮短,在Generic Webhook Trigger插件中勾選 Override Quiet Period
以上配置的webhook url為 https://jenkins.example.com/generic-webhook-trigger/invoke?token=testyh&jobQuietPeriod=0
配置靜默期為0后的靜默期時間明顯縮短