Gitlab Runner安裝和簡單使用
1. 安裝和配置
-
GitLab-CI
GitLab-CI就是一套配合GitLab使用的持續集成系統(當然,還有其它的持續集成系統,同樣可以配合GitLab使用,比如Jenkins)。而且GitLab8.0以后的版本是默認集成了GitLab-CI並且默認啟用的。
-
GitLab-Runner
GitLab-Runner是配合GitLab-CI進行使用的。一般地,GitLab里面的每一個工程都會定義一個屬於這個工程的軟件集成腳本,用來自動化地完成一些軟件集成工作。當這個工程的倉庫代碼發生變動時,比如有人push了代碼,GitLab就會將這個變動通知GitLab-CI。這時GitLab-CI會找出與這個工程相關聯的Runner,並通知這些Runner把代碼更新到本地並執行預定義好的執行腳本。
所以,GitLab-Runner就是一個用來執行軟件集成腳本的東西。你可以想象一下:Runner就像一個個的工人,而GitLab-CI就是這些工人的一個管理中心,所有工人都要在GitLab-CI里面登記注冊,並且表明自己是為哪個工程服務的。當相應的工程發生變化時,GitLab-CI就會通知相應的工人執行軟件集成腳本。如下圖所示:
-
Runner類型
GitLab-Runner可以分類兩種類型:Shared Runner(共享型)和Specific Runner(指定型)。
Shared Runner:這種Runner是所有工程都能夠用的。只有系統管理員能夠創建Shared Runner。
Specific Runner:這種Runner只能為指定的工程服務。擁有該工程訪問權限的人都能夠為該工程創建Shared Runner。 -
CentOs中GitLab-Runner的安裝與使用
[root@middleware ~]# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7012 100 7012 0 0 4636 0 0:00:01 0:00:01 --:--:-- 4640
Detected operating system as centos/7.
Checking for curl...
Detected curl...
Downloading repository file: https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/config_file.repo?os=centos&dist=7&source=script
done.
Installing pygpgme to verify GPG signatures...
已加載插件:fastestmirror
Determining fastest mirrors
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
runner_gitlab-ci-multi-runner-source/signature | 862 B 00:00:00
從 https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey 檢索密鑰
導入 GPG key 0x51312F3F:
用戶ID : "GitLab B.V. (package repository signing key) <packages@gitlab.com>"
指紋 : f640 3f65 44a3 8863 daa0 b6e0 3f01 618a 5131 2f3f
來自 : https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
runner_gitlab-ci-multi-runner-source/signature | 951 B 00:00:00 !!!
updates | 2.9 kB 00:00:00
(1/3): extras/7/x86_64/primary_db | 236 kB 00:00:00
(2/3): docker-ce-stable/x86_64/primary_db | 60 kB 00:00:00
(3/3): updates/7/x86_64/primary_db | 8.0 MB 00:00:01
runner_gitlab-ci-multi-runner-source/primary | 175 B 00:00:02
軟件包 pygpgme-0.3-9.el7.x86_64 已安裝並且是最新版本
無須任何處理
Installing yum-utils...
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
軟件包 yum-utils-1.1.31-54.el7_8.noarch 已安裝並且是最新版本
無須任何處理
Generating yum cache for runner_gitlab-ci-multi-runner...
導入 GPG key 0x51312F3F:
用戶ID : "GitLab B.V. (package repository signing key) <packages@gitlab.com>"
指紋 : f640 3f65 44a3 8863 daa0 b6e0 3f01 618a 5131 2f3f
來自 : https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
https://packages.gitlab.com/runner/gitlab-ci-multi-runner/el/7/x86_64/repodata/42a81582ab8f41cfc311ccaae009dd552d2e372e-primary.xml.gz: [Errno 12] Timeout on https://d20rj4el6vkp4c.cloudfront.net/8/10/el/7/x86_64/repodata/42a81582ab8f41cfc311ccaae009dd552d2e372e-primary.xml.gz?t=1619768494_ee846eb88bd7cd72a2983cae76f24034b201ca4b: (28, 'Operation timed out after 30790 milliseconds with 0 out of 0 bytes received')
正在嘗試其它鏡像。
Generating yum cache for runner_gitlab-ci-multi-runner-source...
The repository is setup! You can now install packages.
[root@middleware ~]# yum install gitlab-ci-multi-runner
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 gitlab-ci-multi-runner.x86_64.0.9.5.1-1 將被 安裝
--> 解決依賴關系完成
依賴關系解決
==================================================================================================================================================================================================
Package 架構 版本 源 大小
==================================================================================================================================================================================================
正在安裝:
gitlab-ci-multi-runner x86_64 9.5.1-1 runner_gitlab-ci-multi-runner 24 M
事務概要
==================================================================================================================================================================================================
安裝 1 軟件包
總下載量:24 M
安裝大小:46 M
Is this ok [y/d/N]: y
Downloading packages:
gitlab-ci-multi-runner-9.5.1-1.x86_64.rpm | 24 MB 00:00:08
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : gitlab-ci-multi-runner-9.5.1-1.x86_64 1/1
GitLab Runner: creating gitlab-runner...
驗證中 : gitlab-ci-multi-runner-9.5.1-1.x86_64 1/1
已安裝:
gitlab-ci-multi-runner.x86_64 0:9.5.1-1
完畢!
[root@middleware ~]# gitlab-ci-multi-runner register
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.1.106:82/
Please enter the gitlab-ci token for this runner:
CdW2YBWzzDZseoybq89Y
Please enter the gitlab-ci description for this runner:
[middleware]: dev
Please enter the gitlab-ci tags for this runner (comma separated):
dev
Whether to run untagged builds [true/false]:
[false]: false
Whether to lock Runner to current project [true/false]:
[false]: true
Registering runner... succeeded runner=CdW2YBWz
Please enter the executor: docker, shell, ssh, kubernetes, docker-ssh, parallels, virtualbox, docker+machine, docker-ssh+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@middleware ~]# gitlab-runner list
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
dev Executor=shell Token=9RWCR_KAQs7wZAPA7uyp URL=http://192.168.1.106:82/
[root@middleware ~]# gitlab-runner list
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
dev Executor=shell Token=9RWCR_KAQs7wZAPA7uyp URL=http://192.168.1.106:82/
[root@middleware ~]#
其中有一個步驟要輸入項目的token。

配置完成后,runners里面會出現剛才配置的選項:

2. 使用
項目根目錄添加文件:.gitlab-ci.yml
內容如下:
stages:
- dev
dev-job:
stage: dev
tags:
- dev
only:
- master
script:
- echo 'hello runner'
提交后,會自動觸發部署腳本
部署日志如下:
