Gitlab-CI:Gitlab-runner自动部署GItlab项目
1.准备环境
(1)Gitlab服务器
[root@gitlab ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@gitlab ~]# free -m
total used free shared buff/cache available
Mem: 3791 3123 307 65 360 387
Swap: 999 26 973
[root@gitlab ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::a00:27ff:fe6c:3e95 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6c:3e:95 txqueuelen 1000 (Ethernet)
RX packets 3564 bytes 329896 (322.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2402 bytes 421392 (411.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.66.66 netmask 255.255.255.0 broadcast 192.168.66.255
inet6 fe80::a00:27ff:feff:c2b9 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:ff:c2:b9 txqueuelen 1000 (Ethernet)
RX packets 23284 bytes 6044105 (5.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 18336 bytes 19211297 (18.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 413196 bytes 2262371682 (2.1 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 413196 bytes 2262371682 (2.1 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
(2)GitLab-Runner服务器
[root@centos7 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@centos7 ~]# free -m
total used free shared buff/cache available
Mem: 3791 103 3512 8 175 3507
Swap: 999 0 999
[root@centos7 ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::a00:27ff:fe6c:3e95 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6c:3e:95 txqueuelen 1000 (Ethernet)
RX packets 5057 bytes 456738 (446.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2986 bytes 392198 (383.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.66.111 netmask 255.255.255.0 broadcast 192.168.66.255
inet6 fe80::a00:27ff:fe91:e49a prefixlen 64 scopeid 0x20<link>
ether 08:00:27:91:e4:9a txqueuelen 1000 (Ethernet)
RX packets 3164 bytes 758152 (740.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5514 bytes 1806268 (1.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 6 bytes 312 (312.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 312 (312.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.Gitlab服务器安装Gitlab(在此不赘述),安装成功后自带有GitLab-CI
3.(1) Centos7上安装 GitLab Runner
[root@centos7 ~]# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
[root@centos7 ~]# yum install gitlab-ci-multi-runner
(2)Gitlab-ci-multi-runner 注册
[root@centos7 ~]# gitlab-ci-multi-runner register
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.66.66:8001/ #根据下图 URL填写
Please enter the gitlab-ci token for this runner:
Lpz9jVjD-_FYkAnproXH #注册令牌根据下图填写
Please enter the gitlab-ci description for this runner:
[centos7]: luck #标识,随意命名
Please enter the gitlab-ci tags for this runner (comma separated):
hello #gitlab-ci 的标签
Whether to run untagged builds [true/false]:
[false]: #直接回车,默认false
Whether to lock Runner to current project [true/false]:
[false]: #直接回车,默认false
Registering runner... succeeded runner=Lpz9jVjD
Please enter the executor: kubernetes, docker-ssh, shell, virtualbox, docker+machine, docker-ssh+machine, docker, parallels, ssh:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
(3)注册完成后,在gitlab中相应的位置就可以看到注册信息如下
4.在Gitlab仓库的项目根目录下编写.gitlab-ci.yml,这样在push之后,Gitlab-ci就会自动识别来执行后续操作
项目根目录下的.gitlab-ci.yml 文件
stages:
- deploy
deploy:
stage: deploy
script:
- /home/gitlab-runner/deploy devops gitlab-runner-test
only:
- develop
tags:
- hello
1)stage只有一个是deploy;
2)only指定了只有在develop分支push的时候才会被执行;
3)tags是hello,必须对应刚才注册runner的时候的标签 [3.(2) gitlab-ci tags ]。
4)script部分/home/gitlab-runner/deploy devops gitlab-runner-test,这里是一条shell指令。/home/gitlab-runner/deploy是我在服务器上编写的一个shell脚本,建议写成绝对路径,传入参数是devops gitlab-runner-test,分别对应的是项目组名和项目名,如下图所示:
5.在gitlab-runner 的家目录(/home/gitlab-runner/)下新建deploy文件
[root@centos7 ~]# su gitlab-runner
[gitlab-runner@centos7 root]$ cd /home/gitlab-runner/
[gitlab-runner@centos7 ~]$ ll
总用量 4
drwxrwxr-x 4 gitlab-runner gitlab-runner 36 3月 1 20:23 builds
-rwxrwxr-x 1 gitlab-runner gitlab-runner 351 3月 1 13:14 deploy
[gitlab-runner@centos7 ~]$ cat deploy
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Arguments error!"
exit 1
else
deploy_path="/home/webroot/$1/$2"
if [ ! -d "$deploy_path" ]
then
project_path="[email protected]:"$1/$2".git"
git clone -b develop $project_path $deploy_path
else
cd $deploy_path
git pull
fi
fi
[gitlab-runner@centos7 ~]$ chmod +x deploy
注意: git clone -b develop $project_path $deploy_path 脚本会在服务器的/home/webroot/下自动创建$1/$2目录
6.在gitlab-runner 所在的服务器(centos7)上配置SSH免密登录。上述的deploy脚本是用ssh方式来和gitlab联系的,所以要给gitlab-runner这个用户配置一个gitlab上能ssh的用户。
注意:一定要切换至gitlab-runner账户下创建公钥(ssh-keygen直接回车),非常关键
[root@centos7 ~]# su gitlab-runner
[gitlab-runner@centos7 root]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/gitlab-runner/.ssh/id_rsa):
Created directory '/home/gitlab-runner/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/gitlab-runner/.ssh/id_rsa.
Your public key has been saved in /home/gitlab-runner/.ssh/id_rsa.pub.
The key fingerprint is:
17:27:b4:96:54:86:cc:75:25:99:cf:17:c9:85:94:79 gitlab-runner@centos7
The key's randomart image is:
+--[ RSA 2048]----+
| oo++.=B=|
| o++ .*=E|
| * . +.|
| . + +|
| S . .|
| . |
| |
| |
| |
+-----------------+
[gitlab-runner@centos7 root]$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLUs9oDafifmGC6hk7BlcPW3kUzbJErl6DV8tGIq9wl1BlNAYWo6uHhninM51upJrqWprtxZj+xBspBwsj9/Ia5iqYGguH7W9qpEMRlEqdJPcckaczbuYB8GZt9nODaLsAMxfEneEVGGUF4tFUD4783Z33CjFEc0WmeEPHSeyyUyVix25oS78cnYcTLuUrmNqTnITjv9G+H8qt4ZHERLBJRrekHAGOtjO02B/7a1+FU4GfkRtxmxSY5eCRZyWoRuj6eCDETG+zQX1bYdTEv+wYtq9XL+8LD7Pqs842x2ztG0RtlscCK9y3wYnmbNHp7hP3hghCcuz5sE9LZlDkdyXJ gitlab-runner@centos7
7.将gitlab-runner所在服务器centos7 的公钥内容复制下来, 粘贴到 gitlab部署密钥位置,如下图所示
8. 在gitlab 注册一个gitlab-runner用户,然后将该用户加入到需要自动部署的项目中
9.授权服务器项目部署目录(很重要)
[root@centos7 ~]# chown -hR gitlab-runner:gitlab-runner /home/webroot/devops/
10.测试验证
(1)在项目代码仓库的develop分支添加gitlab—runner-20200301测试文件
(2) 在gitlab-runner的服务器(centos7)上验证代码是否同步
(3)同时在gitlab控制台上可以得到验证
(4) 点击‘已通过’,呈现下图,也得到验证,大功告成!