Gitlab觸發jenkins並獲取專案post引數
阿新 • • 發佈:2021-07-02
jenkins -- Generic Webhook Trigger外掛
此外掛是git webhook的高階應用,安裝後會暴露出來一個公共API,GWT外掛接收到 JSON 或 XML 的 HTTP POST 請求後,根據我們配置的規則決定觸發哪個Jenkins專案。
定義需要的變數
此外掛有兩種配置方式
1.圖形介面配置-建立流水線任務在觸發器中配置(本文不採用此法)
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