一、核心理解
构建 Webhook 触发器(钩子)本质是一个 HTTP 回调:开发人员向 GitLab 提交代码后,GitLab 主动推送事件到 Jenkins,触发自动构建。
常见场景:
只有在开发人员向develop分支提交代码的时候会自动触发代码构建和部署至测试环境,而向主分支提交的代码不会自动构建,需要运维人员手动部署代码到生产环境。
场景拆解:
| 分支 | 谁提交 | 触发行为 | 部署目标 |
|---|---|---|---|
develop | 开发人员 | 自动构建 + 部署 | 测试环境 |
main / master | 开发人员 | 不自动构建,运维手动点 | 生产环境 |
核心思路:webhook 对两个分支都会推送过来,靠 Jenkins 端的分支过滤规则区分——develop 放行自动跑,main 直接忽略。
可以使用多种方式实现 Webhook 触发构建 :
-
触发远程构建:此方式无需安装插件
-
Build when a change is pushed to GitLab. GitLab webhook URL:需要安装Gitlab插件
- Generic Webhook Trigger :需要安装 Generic Webhook Trigger Plugin 插件
二、配置步骤
2.1Jenkins配置
安装 GWT 插件
Jenkins → Manage Jenkins → Plugins → Available → 搜 Generic Webhook Trigger → 安装 → 重启
首页 → 新建Item → 名称填 freestyle-webhook-demo → 选 Freestyle project → 确定
#生成随机字符串
[root@jenkins ~]#openssl rand -hex 16
2fba1c66e5f5ab1ac17e0173a384dd07
- 源码管理(Git)
| 字段 | 值 |
|---|---|
| Git | 勾选 |
| Repository URL | git@gitlab.chenchengpei.cn:devops/spring-boot-helloworld.git |
| Credentials | gitlab-xiaoming-sshkey |
| Branches to build | */main |
-
Triggers
勾选 Generic Webhook Trigger
Token 填:生成的随机字符串
Print contributed variables:勾选(调试用,跑通后再关。)
关键信息: http://JENKINS_URL/generic-webhook-trigger/invoke?token=TOKEN_HERE JENKINS_URL:系统配置 → Jenkins URL:http://jenkins.chenchengpei.cn:8080/ 完整Webhook路径: http://jenkins.chenchengpei.cn:8080/generic-webhook-trigger/invoke?token=2fba1c66e5f5ab1ac17e0173a384dd07 -
Build Steps
Execute shell
true
保存
2.2测试
#域名解析
[root@gitlab ~]#cat /etc/hosts
10.0.0.101 gitlab.chenchengpei.cn
10.0.0.100 jenkins.chenchengpei.cn
[root@gitlab ~]#curl http://jenkins.chenchengpei.cn:8080/generic-webhook-trigger/invoke?token=2fba1c66e5f5ab1ac17e0173a384dd07 -I
HTTP/1.1 200 OK
[root@gitlab ~]#curl -X POST "http://jenkins.chenchengpei.cn:8080/generic-webhook-trigger/invoke?token=2fba1c66e5f5ab1ac17e0173a384dd07"
{"jobs":{"freestyle-webhook-demo":{"regexpFilterExpression":"","triggered":true,"resolvedVariables":{},"regexpFilterText":"","id":33,"url":"queue/item/33/"}},"message":"Triggered jobs."}[root@gitlab ~]#
"triggered":true + "message":"Triggered jobs." = GWT 成功触发了 freestyle-webhook-demo,构建已经进了队列(queue/item/33/)
#触发成功
2.3GitLab配置
管理员身份登录 → 点击管理员(右上角图标)→ 设置 → 网络 → 出站请求
允许来自 webhooks 和集成对本地网络的请求:勾选(核心开关)
允许系统钩子向本地网络发送请求:勾选
强制执行 DNS 重新绑定攻击保护:勾选
保存更改
chen用户登录(群组所有者) → 点击主页(左上角小狐狸图标)→ 群组 → devops → Spring Boot Helloworld
设置 → Webhooks → 添加新的 Webhooks →
URL:填写完整Webhook路径
名称L:jenkins
触发器:
标签推送事件:勾选 ,启用SSL验证:勾选
添加Webhooks
点击测试 → 标签推送事件
Hook executed successfully: HTTP 200
测试成功
2.4 看控制台输出
+ true
Finished: SUCCESS
#这说明从 GitLab curl → DNS解析 → Jenkins接收 → token验证 → 加入队列 → 执行构建 → 成功完成
三、改版本测试触发
[root@gitlab ~]#cd /data/
[root@gitlab data]#rm -rf spring-boot-helloworld/
[root@gitlab data]#git clone git@gitlab.chenchengpei.cn:devops/spring-boot-helloworld.git
[root@gitlab data]#cd spring-boot-helloworld/
[root@gitlab spring-boot-helloworld]#git tag
v0.1
v0.2
v0.3
v0.4
v0.5
v0.6
v1.0
v1.1
# 模拟开发改版本
[root@gitlab spring-boot-helloworld]#vim src/main/java/com/neo/controller/HelloWorldController.java
return "version v1.1\n";
改为 return "version v1.2\n";
[root@gitlab spring-boot-helloworld]#git commit -am "feat: bump version to v1.2 for GWT webhook trigger test"
[root@gitlab spring-boot-helloworld]#git push --all
[root@gitlab spring-boot-helloworld]#git tag v1.2
[root@gitlab spring-boot-helloworld]#git push --tags
3.1验证
#jenkins控制台输出
+ true
Finished: SUCCESS
四、写在最后
本博客持续分享Linux、云计算运维实战内容,整理大量环境部署、集群搭建、故障排查干货。
如果文章对你工作与学习有所帮助,欢迎点赞、收藏;后续持续更新运维实操内容,点个关注不迷路,一起交流进步。
198




被折叠的 条评论
为什么被折叠?



