I. Overview
GitLab is a code repository used to manage code. Jenkins is an automated server, you can run a variety of automated build, test or deployment tasks. So these two together, we can achieve the developers submit code to GitLab, Jenkins to run automatically at a certain frequency test, build and deploy tasks more efficient integration and release the code to help set development team.
Here is what I recently installed a process GitLab + Jenkins environment, recorded for future reference.
In addition, although the article is very long, very trivial, but still failed to clearly describe the place, according to the article failed to build a successful, patient analysis, also available from the official document look for inspiration to continue total It will be successful.
surroundings
operating system | ip address | Roles | address |
ubuntu-16.04.4-server-amd64 | 192.168.10.122 | Jenkins | http://192.168.10.122:8080/ |
ubuntu-16.04.4-server-amd64 | 192.168.10.134 | GitLab | http://192.168.10.134/ |
Jenkins installation tutorial, please refer to the link:
https://www.cnblogs.com/xiao987334176/p/11323795.html
GitLab installation tutorial, please refer to the link:
https://www.cnblogs.com/xiao987334176/p/11329018.html
Second, create a project using GitLab
Click Create a project
Create a test project
Add README file
Typing, click OK
Results are as follows:
Create a user jenkins
Click the wrench button
Click User
Add jenkins user, click on the bottom of the Create user button.
Click Edit
Change Password, here is my definition of jenkins @ 1234
Set jenkins user ssh keys
Click Impersonate, pretending to represent user jenkins
Click Settings
Click ssh keys
Jenkins log on to the server, generate keys
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
查看公钥
cat /root/.ssh/id_rsa.pub
将公钥内容复制到网页,点击添加秘钥
创建Access Tokens
点击左侧的Access Tokens,输入用户 jenkins,勾选权限,点击创建
复制token,待会 jenkins 设置时,会用到。Zzis8ALsiYuytsg2GH-5
点击退出
重新登录root用户,点击test
点击设置-成员
添加jenkins 用户,这样就可以访问项目了
三、 jenkins 安装和Git,GitLab插件
由于 jenkins安装时,勾选了推荐安装的插件,因此包含了git插件。
现在只需要安装gitlab插件即可
点击 Manage Jenkins --> Manage Plugins -->可选插件,搜索gitlab
勾选gitlab,点击直接安装
等待十几分钟
提示安装完成
点击 Manage Jenkins --> Configure System -->配置 -->Gitlab
Connection Name ”随便填,“Git Host URL”填GitLab的访问地址,然后点“Add”——“jenkins”,如下所示:
选择gitlalb api,输入 jenkins 用户创建的token Zzis8ALsiYuytsg2GH-5
选择 gitlab api token,点击测试连接,出现 Success,表示成功。
点击最下面的保存
配置Git插件
点击 Manage Jenkins --> Configure System -->配置 -->Git plugin
设置Git插件的全局配置,然后点击最下面的保存。
四、创建一个Jenkins Job
在jenkins里,一个任务叫做一个job。一般我们的项目会有多个分支,比如开发分支和产品分支,我们可以对每一个分支都新建一个job,比如,我们对开发分支创建一个测试的job,每次有代码提交就自动运行一次测试,对产品分支创建一个打包的job,每次有代码提交就运行打包任务。
不过在这里,我们只是单纯的创建一个job,来演示jenkins自动运行任务的过程。
首先,点击 新建Item,item name”可以随便起,然后点击“构建一个自由风格的软件项目”,点击Ok
至此,创建一个Job成功了
由于 jenkins 服务器的ssh秘钥,已经添加到gitlab上面了。那么就可以进行下一步了!
配置Job的源码管理
选择“源码管理”,选择“Git”,然后去GitLab中复制项目地址,粘贴到“Repository URL”,然后点击“credentials”后面的“Add”按钮
类型,选择 SSH Username with private key
Username 填 root
PrivateKey 选择Enter directly,点击add
登录到jenkins 服务器,查看私钥
cat /root/.ssh/id_rsa
将内容复制到网页上面,点击添加
在“credentials”里选择我们刚刚创建的认证方式:root
如果没报错,说明成功了,点击页面底部的“apply”。如果出错了,会在“Repository URL”和“Credentials”之间显示红色的错误信息。
jenkins job默认对master分支进行构建,你也可以自定义分支。这要求你的Gitlab代码仓库中要存在这个分支,一般来说,就是要向代码仓库提交一次更改,请 自行完成(Gitlab项目刚创建时是空的,一个分支也没有,这样的话,自动构建时会出错)
配置Job的构建触发器
选择“构建触发器”,勾选“Pull SCM”,这个选项会每隔一段时间检查一下GitLab仓库中代码是否有更新,有的话就执行构建操作。日程表如何设置,在这个输入框下面有说明。
扩展阅读:
常见构建触发器、:
- Build after other projects are built 当另一个构建任务完成之后触发
- Build periodically 周期性的触发
- Build when a change is pushed to GitLab. GitLab CI Service URL: http://191.8.2.112:12000/project/test-go-dev 当代码有更新的时候触发,通过GitLab CI
- GitHub hook trigger for GITScm polling 通过Github钩子触发
- Poll SCM 定期检查代码有无更新,有更新时触发
这只是个人理解,具体怎么样大家可以试试,Poll SCM方式我是试过的。
配置Job的构建脚本
在build栏目里,选择“jenkins execute shell”,然后输入你项目的构建命令(这依赖于你的项目,如Maven的maven build,gulp的gulp xxx 等等)
这里我输入一段测试命令,表示将git项目的代码复制到/tmp/test
\cp -r $WORKSPACE /tmp/test
点击 可用的环境变量列表,就可以看到很多变量
其中 WORKSPACE 表示,下载git项目后的路径。
由于git项目为test,因此路径为:/var/lib/jenkins/workspace/test
点击保存
点击 Build Now,点击下面的构建过程
点击控制台输出,就可以看到完整的cp命令
登录到jenkins服务器,查看/tmp/test目录,是否有文件存在。
root@ubuntu:~# ll /tmp/test/ 总用量 16 drwxr-xr-x 3 jenkins jenkins 4096 8月 28 18:27 ./ drwxrwxrwt 14 root root 4096 8月 28 18:29 ../ drwxr-xr-x 8 jenkins jenkins 4096 8月 28 18:27 .git/ -rw-r--r-- 1 jenkins jenkins 16 8月 28 18:27 README.md