jenkins -- Generic Webhook Trigger插件
此插件是git webhook的高階應用,安裝後會暴露出來一個公共API,GWT插件接收到 JSON 或 XML 的 HTTP POST 請求後,根據我們配置的規則決定觸發哪個Jenkins項目。
定義需要的變量
此插件有兩種配置方式
1.圖形界面配置-建立流水線任務在觸發器中配置(本文不采用此法)
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL3QjM0IDNxADMy0yNyMjNwgDM5AjMwcDMxIDMy0yNwYTMyQjMvw1NwEjMwIzLcdDM2EjM0IzLcd2bsJ2Lc12bj5ycn9Gbi52YuAjMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.pipeline 腳本中配置-注意此方法需要手動觸發一次建構任務生成 Generic Webhook Trigger配置
jenkins配置
1.安裝插件
勾選Generic Webhook Trigger後,點選【Install without restart】安裝插件。
2.建立Jenkins任務
在Jenkins Dashboard中,點選【建立任務】
輸入任務名稱,選擇流水線類型後,點選确定建立任務。
點選剛才建立好的任務。
點選【配置】。
選擇【流水線】。
3.pipeline内容
pipeline {
agent any
riggers{
GenericTrigger(
genericVariables:[
[key:'event_name',value:'$.event_name'], //觸發動作 pubat or tag_pubat
[key:'user_email',value:'$.user_email'], //GitLab公共郵箱需要自行配置否則擷取不到
[key:'project_name',value:'$.project.name'], //項目名稱 DevOps_Test
[key:'git_url',value:'$.project.git_http_url'], //git_url http://xxx.xxx.xxx/devops/DevOps_Test.git
[key:'ref',value:'$.ref'], //分支或tag資訊
[key:'group_name',value:'$.project.namespace'], //GITLAB_GROUP
[key:'commits_id',value:'$.commits[0].id'] //gitlab commits id
],
token:"qazwsx", //gitlab webhook觸發token 多個任務配置同一個token會一起觸發
causeString:'Triggered on $ref',
printContributedVariables:true,
printPostContent:true
)
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
gitlab傳遞的post資料是json格式
{
"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": "[email protected]: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": "[email protected]:h_y/hello.git",
"ssh_url": "[email protected]: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": "[email protected]"
},
"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": "[email protected]"
},
"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": "[email protected]"
},
"added": [
],
"modified": [
"Jenkinsfile"
],
"removed": [
]
}
],
"total_commits_count": 3,
"push_options": {
},
"repository": {
"name": "hello",
"url": "[email protected]: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": "[email protected]:h_y/hello.git",
"visibility_level": 0
}
}
建立完成手動觸發一次建構生成插件配置檔案
gitlb配置
http://jenkinsserver:8080//generic-webhook-trigger/invoke?token=qazwsx