Gitlab-CI:Gitlab-runner自動部署GItlab項目

                                                               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)  點擊‘已通過’,呈現下圖,也得到驗證,大功告成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章