使用 Jenkins 做自动构建、部署,实现每当提交到 Gitlab 的代码就开始部署工程。关键点就是 Jenkins 和 Gitlab 之间的关联如何打通?如何配置?
解决的方式:Gitlab 中的 Webhooks(钩子)。
## Jenkins job编辑界面:构建触发器 -> Build when a change is pushed to GitLab
Build when a change is pushed to GitLab. GitLab webhook URL: http://192.168.98.21:8080/jenkins/project/http-demo
此功能需要安装 GitLab 插件
获取地址:http://192.168.98.21:8080/jenkins/project/http-demo
## Gitlab settings/Webhooks 钩子,自动 build
gitlab:project -> settings -> webhooks
URL: http://192.168.98.21:8080/project/http-demo
Add webhook
## 遇到问题 1:url is blocked. requests to the local network are not allowed
git界面:小扳手图标(admin area) -> settings -> network -> Outbound requests, 两个选项勾上
## 遇到问题 2:403 访问不通,也就是 Jenkins 和 GitLab 没有联通
Hook executed successfully but returned HTTP 403 <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Error 403 No valid crumb was included in the request</title> </head> <body><h2>HTTP ERROR 403 No valid crumb was included in the request</h2> <table> <tr><th>URI:</th><td>/jenkins/project/http-demo</td></tr> <tr><th>STATUS:</th><td>403</td></tr> <tr><th>MESSAGE:</th><td>No valid crumb was included in the request</td></tr> <tr><th>SERVLET:</th><td>Stapler</td></tr> </table> <hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.27.v20200227</a><hr/> </body> </html>
解决:(1) jenkins 系统配置 -> 配置 -> GitLab 选项取消勾掉
(2)问题在地址的url后缀,没有 jenkins 路径
复制过来的地址是这样的:http://192.168.98.21:8080/jenkins/project/http-demo
正确是:http://192.168.98.21:8080/project/http-demo
## 测试钩子是否成功
test -> push events
显示下面的 200 就是成功:Hook executed successfully: HTTP 200