gitlab与jenkins集成实现自动构建 - sysrj2010/CICD GitHub Wiki

一、版本说明

1、github:GitLab Community Edition 12.1.1

2、jenkins:2.187(war包方式运行) 下载地址:http://mirrors.jenkins.io/war/

3、JDK:1.8.222(jenkins依赖JDK运行)

二、参考文档:https://blog.csdn.net/wangyiyungw/article/details/81776972(因最新版本的工具有调整,所以直接使用该文无法实现自动构建)

三、主要操作步骤及问题处理:

1、获取gitlab的private token(新版本参考文档中的方法不能找到):通过setting-->access token 新建一个token,路径为:http://192.168.92.139/profile/personal_access_tokens

2、在 Jenkins 中安装gitlab及gitlab hook插件;

3、在 Jenkins 中,选择 系统管理 -> 系统设置,在系统设置中找到 GitLab 的部分:填写1中的 Private token 粘贴到 API token 后面的输入框中(测试时不通过Client error: HTTP 401 Unauthorized,但不影响使用);

4、在Jenkins中创建一个item:构建触发器 的部分,勾选 Build when a change is pushed to GitLab,填写gitlab上的用户名及对应项目的仓库URL地址;同时点击【高级】按钮,随后点击右下角的 Generate 按钮,生成 Secret token

--------------------至此hook触发构建完成------------------

5、在gitlab中操作:创建一个新的或选择一个已有的 GitLab 工程。然后选择 Settings -> Integrations,在 URL 一栏中输入前面保存的 GitLab CI Service URL,在 Secret Token 一栏中输入前面保存的 Secret token,然后选择需要 trigger Jenkins 工程执行构建的事件

-------------------hook钩子配置完成----------------

测试正常结果:Hook executed successfully: HTTP 200

错误结果:

1、Url is blocked: Requests to the local network are not allowed:参考https://www.wandouip.com/t5i99628/ 在gitlab中允许本地访问

2、403 anonymous is missing the Job/Build permission:参考 https://blog.csdn.net/u011662047/article/details/80525503 在jenkins中 系统管理 -> 系统设置 -> 去掉 Enable authentication for ‘/project’ end-point

如出现下一条再处理,一般不会出现:

3、Error 403 No valid crumb was included in the request:参考https://blog.csdn.net/weixin_37417954/article/details/83182962,在jenkins中取消jenkins地跨域访问限制;