jenkins构建 Webhook 触发器

目的是要让gitlab上面的项目更新了版本,Jenkins上面自动去执行新版本

项目地址

不用插件实现标签触发任务

Jenkins上面创建任务

这里面需要更改下

#网址


http://jenkins.wang.org:8080/job/webhook-demo/configure

生成个随机密码,测试用123456也可以
[root@rocky8 ginweb]#openssl rand -base64 21
WLB14/f1J2DJBFpMCGvKva12y/ax

JENKINS_URL/job/webhook-demo/build?token=TOKEN_NAME 或者 /buildWithParameters?token=TOKEN_NAME
Optionally append &cause=Cause+Text 需要替换TOKEN

两种格式都可以
http://jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/ax

http://jenkins.wang.org:8080/job/webhook-demo/buildWithParameters?token=WLB14/f1J2DJBFpMCGvKva12y/ax

网址需要注意格式

只添加了编译选项,为了测试

先在gitlab上面通过curl命令测试下,发现需要验证

响应码403,通常是用户身份验证失败

去Jenkins上面创建用户,系统配置-用户管理

创建用户,账号密码随意

可以看到添加用户后已经成功
[root@gitlab ~]#curl -u wang:123456 http://jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/ax -I
HTTP/1.1 201 Created
Date: Sun, 08 Sep 2024 09:03:07 GMT
X-Content-Type-Options: nosniff
Location: http://jenkins.wang.org:8080/queue/item/26/
Content-Length: 0
Server: Jetty(10.0.20)

jenkins上面切换用户生成Token---不切换生成不出来

这个有两种一个是项目token  另外一个是用户的token

gitlab上面测试令牌

[root@gitlab ~]#curl -u wang:117ef87ab2a7977f0a9d04a04c52bf9334 http://jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/ax -I
HTTP/1.1 201 Created
Date: Sun, 08 Sep 2024 09:15:50 GMT
X-Content-Type-Options: nosniff
Location: http://jenkins.wang.org:8080/queue/item/27/
Content-Length: 0
Server: Jetty(10.0.20)

[root@gitlab ~]#curl -u wang:117ef87ab2a7977f0a9d04a04c52bf9334 http://jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/ax

可以看到已经触发了操作,用密码去触发不安全,最好使用令牌方式Gitlab配置 Webhook

打开外发请求,必须先打开

地址需要注意--ssl验证取消

http://wang:123456@jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/ax

测试出现错误403----新版不再支持 http 不允许用账号密码

用新版方法
http://wang:117ef87ab2a7977f0a9d04a04c52bf9334@jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/ax

测试已经成功

找台机器开始试验

[root@rocky8 ~]#git clone git@gitlab.wang.org:devops/spring-boot-helloworld.git
Cloning into 'spring-boot-helloworld'...
remote: Enumerating objects: 128, done.
remote: Counting objects: 100% (48/48), done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 128 (delta 26), reused 28 (delta 12), pack-reused 80 (from 1)
Receiving objects: 100% (128/128), 26.63 KiB | 13.31 MiB/s, done.
Resolving deltas: 100% (36/36), done.
[root@rocky8 ~]#ls
anaconda-ks.cfg  ginweb  ll  RuoYi  spring-boot-helloworld  wheel

[root@rocky8 spring-boot-helloworld]#vim README.md 

[root@rocky8 spring-boot-helloworld]#git commit -am "测试"
[main d56edc0] 测试
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@rocky8 spring-boot-helloworld]#git tag v5.0 d56edc0
[root@rocky8 spring-boot-helloworld]#git tag
v0.1
v0.2
v0.3
v5.0
vceshi2.0
vceshi3.0
[root@rocky8 spring-boot-helloworld]#git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.wang.org:devops/spring-boot-helloworld.git
   ebee160..d56edc0  main -> main
[root@rocky8 spring-boot-helloworld]#git push --tags 
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To gitlab.wang.org:devops/spring-boot-helloworld.git
 * [new tag]         v5.0 -> v5.0
[root@rocky8 spring-boot-helloworld]#

已经实现了标签推送,然后触发操作


利用插件实现标签触发操作

系统管理 - 管理插件 - 可选插件 -Gitlab Gitlab Hook 两个插件
触发条件取消,生成随机的token
e448c47e290cae9da94a342410b65624
URL: http://jenkins.wang.org:8080/project/webhook-demo

去gitlab上面设置标签推送规则,注意出站请求需要提前配置好

令牌填自动生成的token

测试已经成功

修改数据,推送标签,可以看到已经成功

[root@rocky8 spring-boot-helloworld]#vim README.md 
[root@rocky8 spring-boot-helloworld]#git commit -am "插件推送"
[main 93acd9e] 插件推送
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@rocky8 spring-boot-helloworld]#git tag v4.0 93acd9e
[root@rocky8 spring-boot-helloworld]#git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 289 bytes | 289.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.wang.org:devops/spring-boot-helloworld.git
   d56edc0..93acd9e  main -> main
[root@rocky8 spring-boot-helloworld]#git push --tags 
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To gitlab.wang.org:devops/spring-boot-helloworld.git
 * [new tag]         v4.0 -> v4.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值