Jenkins集成GitLab
一、安装插件
二、关联Jenkins公钥至GitLab
- Jenkins服务器执行命名,生成公钥
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in y.
Your public key has been saved in y.pub.
The key fingerprint is:
SHA256:fKbEeDMHsO7wpL3YdOcN4thWYaYDFpa4zXTwvdpXjIw root@localhost.localdomain
The key's randomart image is:
- 将生成的公钥拷贝到GitLab中
[root@localhost ~]# cat y.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBLI5ebFfw0vlVNBUcy+CC0wdKwvYxEVvhQKhEKebie7/Lw3b5D/tH6ch02U2UFBLu0gZ83tQ3DUeOAB2J1J7zPA/DKuj2CfASSDQZ9sb8TmPXLKunAqrU/JNjljd
三、创建自由风格的项目
- 创建一个自由风格的任务
四、Jenkins任务中配置Jenkins私钥
- 配置Git仓库和私钥
在jenkins服务器上,查看生成的私钥,然后将其复制并粘贴到下图位置:
五、代码仓库域名解析
- 若jenkins服务器直接部署在linux系统中,当代码仓库无法连接,域名解析错误,在jenkins服务器上添加解析地址
[root@localhost ~]# echo "192.168.31.4 gitlab.xtank.com" >> /etc/hosts
- 若jenkins安装在docker容器中,此时无法直接修改/etc/hosts文件,但是可以在启动docker jenkins镜像时加上域名映射即可
[root@localhost ~]# docker run \
--add-host=gitlab.xtank.com:192.168.31.4 \
--name myjenkins \
-itd -p 9000:8080 \
-v /home/xyz/test/jenkins_home:/var/jenkins_home \
--env JAVA_OPTS="-Duser.timezone=Asia/Shanghai" \
jenkins/jenkins:latest
六、构建任务
进入docker宿主机,可以看到jenkins挂载目录中已经将克隆的代码挂载到了本地
[root@localhost ~]# cd /home/xyz/test/jenkins_home/workspace/
[root@localhost workspace]# ls
freestyle_mycode freestyle_mycode@tmp freestyle_myshell xyz_test
[root@localhost workspace]# cat freestyle_mycode
cat: freestyle_mycode: 是一个目录
[root@localhost workspace]# cat freestyle_mycode/README.md
这是一个开发项目
进入dockers容器中查看,发现代码已经克隆到容器中
[root@localhost ~]# docker exec -it myjenkins bash
jenkins@3f7698d57a9f:/$ cd /var/jenkins_home/workspace/freestyle_mycode
jenkins@3f7698d57a9f:~/workspace/freestyle_mycode$ ls
README.md a.py
jenkins@3f7698d57a9f:~/workspace/freestyle_mycode$ cat README.md
这是一个开发项目