目的是要让gitlab上面的项目更新了版本,Jenkins上面自动去执行新版本
项目地址
不用插件实现标签触发任务
Jenkins上面创建任务
这里面需要更改下
#网址http://jenkins.wang.org:8080/job/webhook-demo/configure生成个随机密码,测试用123456也可以[root@rocky8 ginweb]#openssl rand -base64 21WLB14/f1J2DJBFpMCGvKva12y/axJENKINS_URL/job/webhook-demo/build?token=TOKEN_NAME 或者 /buildWithParameters?token=TOKEN_NAMEOptionally append &cause=Cause+Text 需要替换TOKEN两种格式都可以http://jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/axhttp://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 -IHTTP/1.1 201 CreatedDate: Sun, 08 Sep 2024 09:03:07 GMTX-Content-Type-Options: nosniffLocation: http://jenkins.wang.org:8080/queue/item/26/Content-Length: 0Server: 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 -IHTTP/1.1 201 CreatedDate: Sun, 08 Sep 2024 09:15:50 GMTX-Content-Type-Options: nosniffLocation: http://jenkins.wang.org:8080/queue/item/27/Content-Length: 0Server: 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.gitCloning 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 ~]#lsanaconda-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 tagv0.1v0.2v0.3v5.0vceshi2.0vceshi3.0[root@rocky8 spring-boot-helloworld]#git pushEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 2 threadsCompressing 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 0To 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 0To 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 pushEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 2 threadsCompressing 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 0To 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 0To gitlab.wang.org:devops/spring-boot-helloworld.git * [new tag] v4.0 -> v4.0