0


jenkins构建 Webhook 触发器

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

项目地址

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

Jenkins上面创建任务

这里面需要更改下

  1. #网址
  2. http://jenkins.wang.org:8080/job/webhook-demo/configure
  3. 生成个随机密码,测试用123456也可以
  4. [root@rocky8 ginweb]#openssl rand -base64 21
  5. WLB14/f1J2DJBFpMCGvKva12y/ax
  6. JENKINS_URL/job/webhook-demo/build?token=TOKEN_NAME 或者 /buildWithParameters?token=TOKEN_NAME
  7. Optionally append &cause=Cause+Text 需要替换TOKEN
  8. 两种格式都可以
  9. http://jenkins.wang.org:8080/job/webhook-demo/build?token=WLB14/f1J2DJBFpMCGvKva12y/ax
  10. http://jenkins.wang.org:8080/job/webhook-demo/buildWithParameters?token=WLB14/f1J2DJBFpMCGvKva12y/ax

网址需要注意格式

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

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

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

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

创建用户,账号密码随意

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

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

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

gitlab上面测试令牌

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

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

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

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

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

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

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

测试已经成功

找台机器开始试验

  1. [root@rocky8 ~]#git clone git@gitlab.wang.org:devops/spring-boot-helloworld.git
  2. Cloning into 'spring-boot-helloworld'...
  3. remote: Enumerating objects: 128, done.
  4. remote: Counting objects: 100% (48/48), done.
  5. remote: Compressing objects: 100% (32/32), done.
  6. remote: Total 128 (delta 26), reused 28 (delta 12), pack-reused 80 (from 1)
  7. Receiving objects: 100% (128/128), 26.63 KiB | 13.31 MiB/s, done.
  8. Resolving deltas: 100% (36/36), done.
  9. [root@rocky8 ~]#ls
  10. anaconda-ks.cfg ginweb ll RuoYi spring-boot-helloworld wheel
  11. [root@rocky8 spring-boot-helloworld]#vim README.md
  12. [root@rocky8 spring-boot-helloworld]#git commit -am "测试"
  13. [main d56edc0] 测试
  14. 1 file changed, 1 insertion(+), 1 deletion(-)
  15. [root@rocky8 spring-boot-helloworld]#git tag v5.0 d56edc0
  16. [root@rocky8 spring-boot-helloworld]#git tag
  17. v0.1
  18. v0.2
  19. v0.3
  20. v5.0
  21. vceshi2.0
  22. vceshi3.0
  23. [root@rocky8 spring-boot-helloworld]#git push
  24. Enumerating objects: 5, done.
  25. Counting objects: 100% (5/5), done.
  26. Delta compression using up to 2 threads
  27. Compressing objects: 100% (3/3), done.
  28. Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.
  29. Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
  30. To gitlab.wang.org:devops/spring-boot-helloworld.git
  31. ebee160..d56edc0 main -> main
  32. [root@rocky8 spring-boot-helloworld]#git push --tags
  33. Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
  34. To gitlab.wang.org:devops/spring-boot-helloworld.git
  35. * [new tag] v5.0 -> v5.0
  36. [root@rocky8 spring-boot-helloworld]#

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


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

系统管理-管理插件-可选插件-Gitlab和Gitlab Hook 两个插件

触发条件取消,生成随机的token

e448c47e290cae9da94a342410b65624

URL: http://jenkins.wang.org:8080/project/webhook-demo

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

令牌填自动生成的token

测试已经成功

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

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

标签: jenkins 运维

本文转载自: https://blog.csdn.net/weixin_56547063/article/details/142028026
版权归原作者 重生之我在北京学Liunx 所有, 如有侵权,请联系我们删除。

“jenkins构建 Webhook 触发器”的评论:

还没有评论