Jenkins 进阶04 Generic Webhook GitLab 触发器与提交流水线

网友投稿 1076 2022-11-30 11:50:20

Jenkins 进阶04 Generic Webhook GitLab 触发器与提交流水线

GitLab WebHook 配置

前面使用插件模拟了Jenkins如何触发流水线,现在使用Gitlab去触发Jenkins。

进入项目设置, gitlab webhook的配置页面:(为你的项目创建webhook)

trigger下面是很多事件,push events就是提交之后的构建 。(基于事件去触发)

事件:

Push 提交事件Tag Push 创建事件MergeRequest 合并事件Issue 问题创建更新事件

这里选择的是代码提交事件, 过滤的分支是以feature开头的所有分支

当事件触发了,就会去使用URL去触发Jenkins。

Jenkins WebHook配置

新建一个“Gitlab-Webhook-Trigger”项目, 开启Generic webhook。 配置触发token 为作业名称gitlab-test-pipeline。

现在在Jenkins启用触发器,并且填入token,token值为项目名字

生成的触发URL

WebHook 配置

进入项目设置, gitlab webhook的配置页面:

当修改代码,提交之后,基于所有分支。那么gitlab会向上面配置的url里面发送一个post请求,然后触发Jenkins。

出现此FAQ:Url is blocked: Requests to the local network are not allowed

解决方法:进入admin管理页面设置 > network

找到"Outbound requests"勾选允许请求webhooks和服务。(更改后,重启触发即可)

触发成功提示: Hook executed successfully: HTTP 200

触发之后,Jenkins拿到如下的数据,gitlab将数据发送给Jenkins了,后面需要什么信息在里面过滤就可以。

Generic CauseRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline (hide)GenericWebhookEnvironmentContributor Received:{"object_kind":"push","event_name":"push","before":"0d9212b05a167d8c6e53f225402894456894ef1e","after":"236006b394466b614ca84fcd6a767329d3b9eea4","ref":"refs/heads/master","checkout_sha":"236006b394466b614ca84fcd6a767329d3b9eea4","message":null,"user_id":1,"user_name":"Administrator","user_username":"root","user_email":"","user_avatar":".gitlab-ci.yml file","title":"Update .gitlab-ci.yml file","timestamp":"2021-08-24T01:39:35+00:00","url":".gitlab-ci.yml file","title":"Update .gitlab-ci.yml file","timestamp":"2021-08-24T01:37:29+00:00","url":"new file","title":"Add new file","timestamp":"2021-08-24T01:36:04+00:00","url":"可以看到本次提交是否正常产生了webhook事件。

如果Jenkins触发成功了之后,我们可以在Jenkins的构建日志中查看效果。 这些数据就是gitlab post传递过来的。

到此:你基本上已经知道了Gitlab如何触发Jenkins的了。(多看几遍,多练习几遍)

补充

一个请求和响应的过程:

Request 用户通过浏览器向我们的服务端发起请求。Response服务端将客户端请求的资源数据进行响应。

调用接口的方法

请求类型:

GET 向指定的URL请求资源,可携带参数(明文)。POST 向指定的URL提交资源,表单数据提交,数据进行封装(比Get方法安全)。PUT 与POST类似,通常用于对资源数据的更新修改。DELETE 删除指定的资源。

def webHookData = readJSON text: "${allData}"String userName = webHookData.user_nameString userEmail = webHookData.user_emailpipeline { agent any stages { stage('Hello') { steps { println("${webHookData}") println(userName) println(userEmail) } } }}

[Pipeline] readJSON[Pipeline] nodeRunning on build-01 in /data/cicd/jenkinsagent/workspace/jenkins/webhook-01[Pipeline] {[Pipeline] stage[Pipeline] { (Hello)[Pipeline] echo[object_kind:push, event_name:push, before:efce8922637921394bd9509dc275a009901173aa, after:0a6725102ba2cf695565eed029f1939029147aa4, ref:refs/heads/master, checkout_sha:0a6725102ba2cf695565eed029f1939029147aa4, message:null, user_id:2, user_name:devop, user_username:devop, user_email:1239683670@qq.com, user_avatar:project_id:4, project:[id:4, name:devops-hello-service, description:, web_url:avatar_url:null, git_ssh_url:git@76c03cb0fea1:dev/devops-hello-service.git, git_namespace:dev, visibility_level:0, path_with_namespace:dev/devops-hello-service, default_branch:master, ci_config_path:null, homepage:url:git@76c03cb0fea1:dev/devops-hello-service.git, ssh_url:git@76c03cb0fea1:dev/devops-hello-service.git, commits:[[id:0a6725102ba2cf695565eed029f1939029147aa4, message:Update Jenkinsfile, title:Update Jenkinsfile, timestamp:2021-08-30T00:37:27+00:00, url:author:[name:devop, email:1239683670@qq.com], added:[], modified:[Jenkinsfile], removed:[]], [id:0cbf76185c4b87558dbfcaa836c5ac8eb1e180f3, message:Update Jenkinsfile, title:Update Jenkinsfile, timestamp:2021-08-28T07:38:26+00:00, url:author:[name:devop, email:1239683670@qq.com], added:[], modified:[Jenkinsfile], removed:[]], [id:efce8922637921394bd9509dc275a009901173aa, message:Update Jenkinsfile, title:Update Jenkinsfile, timestamp:2021-08-28T06:35:33+00:00, url:author:[name:devop, email:1239683670@qq.com], added:[], modified:[Jenkinsfile], removed:[]]], total_commits_count:3, push_options:[:], repository:[name:devops-hello-service, url:git@76c03cb0fea1:dev/devops-hello-service.git, description:, homepage:git_git_ssh_url:git@76c03cb0fea1:dev/devops-hello-service.git, visibility_level:0]][Pipeline] echodevop[Pipeline] echo1239683670@qq.com[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

Request body:

{ "object_kind": "push", "event_name": "push", "before": "efce8922637921394bd9509dc275a009901173aa", "after": "0a6725102ba2cf695565eed029f1939029147aa4", "ref": "refs/heads/master", "checkout_sha": "0a6725102ba2cf695565eed029f1939029147aa4", "message": null, "user_id": 2, "user_name": "devop", "user_username": "devop", "user_email": "1239683670@qq.com", "user_avatar": " "project_id": 4, "project": { "id": 4, "name": "devops-hello-service", "description": "", "web_url": " "avatar_url": null, "git_ssh_url": "git@76c03cb0fea1:dev/devops-hello-service.git", "git_" "namespace": "dev", "visibility_level": 0, "path_with_namespace": "dev/devops-hello-service", "default_branch": "master", "ci_config_path": null, "homepage": " "url": "git@76c03cb0fea1:dev/devops-hello-service.git", "ssh_url": "git@76c03cb0fea1:dev/devops-hello-service.git", "" }, "commits": [ { "id": "0a6725102ba2cf695565eed029f1939029147aa4", "message": "Update Jenkinsfile", "title": "Update Jenkinsfile", "timestamp": "2021-08-30T00:37:27+00:00", "url": " "author": { "name": "devop", "email": "1239683670@qq.com" }, "added": [ ], "modified": [ "Jenkinsfile" ], "removed": [ ] }, { "id": "0cbf76185c4b87558dbfcaa836c5ac8eb1e180f3", "message": "Update Jenkinsfile", "title": "Update Jenkinsfile", "timestamp": "2021-08-28T07:38:26+00:00", "url": " "author": { "name": "devop", "email": "1239683670@qq.com" }, "added": [ ], "modified": [ "Jenkinsfile" ], "removed": [ ] }, { "id": "efce8922637921394bd9509dc275a009901173aa", "message": "Update Jenkinsfile", "title": "Update Jenkinsfile", "timestamp": "2021-08-28T06:35:33+00:00", "url": " "author": { "name": "devop", "email": "1239683670@qq.com" }, "added": [ ], "modified": [ "Jenkinsfile" ], "removed": [ ] } ], "total_commits_count": 3, "push_options": { }, "repository": { "name": "devops-hello-service", "url": "git@76c03cb0fea1:dev/devops-hello-service.git", "description": "", "homepage": " "git_" "git_ssh_url": "git@76c03cb0fea1:dev/devops-hello-service.git", "visibility_level": 0 }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Kubernetes 环境部署 docker离线包准备
下一篇:Jenkins 进阶06 Generic Webhook 实践优化 GitLab提交流水线优化
相关文章