在前面的文章中,我們已經(jīng)介紹過了開源可私有化部署的代碼倉(cāng)庫(kù)Gitlab及其搭建教程,今天來介紹它的CICD功能。
一、Gitlab Runner簡(jiǎn)介
Gitlab實(shí)現(xiàn)CICD的方式有很多,比如通過Jenkins,通過Gitlab Runner等,今天主要介紹后者。Gitlab在安裝的時(shí)候,就默認(rèn)包含了Gitlab CI的能力,但是該能力只是用于協(xié)調(diào)作業(yè),并不能真的去執(zhí)行作業(yè),因此需要搭配Gitlab Runner來作為執(zhí)行器實(shí)現(xiàn)具體的CICD工作。Gitlab Runner可以被安裝在任意支持的系統(tǒng)上,比如Linux、Windows、Mac,甚至也可以運(yùn)行在Docker、Kubernetes集群上。
Gitlab Runner分為三種類型,在注冊(cè)Runner過程中使用不同類型的token就能區(qū)分不同的類型。
- Shared Runners,共享Runner,可以被倉(cāng)庫(kù)里面的任意項(xiàng)目使用;token獲取位置為全局管理中心-Runner-注冊(cè)令牌;
- Group Runners,組Runner,僅能被某個(gè)組中的任意項(xiàng)目使用;token獲取位置為項(xiàng)目群組-CICD-Runner-注冊(cè)令牌;
- Specific Runners,特定Runner,僅能被指定的項(xiàng)目使用;token獲取位置為項(xiàng)目-設(shè)置-CICD-Runner-注冊(cè)令牌;
如下是Gitlab的大致工作流程示意:
二、Gitlab Runner注冊(cè)
2.1 使用Docker Engine安裝
GitLab Runner在不同平臺(tái)的注冊(cè)可以在官網(wǎng)找到教程,本文就以Docker方式講解如何進(jìn)行注冊(cè),在開始之前,確保您的電腦上已經(jīng)安裝好了Docker Engine。
首先我們需要下載Gitlab Runner的鏡像,并運(yùn)行起來:
# 如果需要把配置文件掛載到宿主機(jī)的某個(gè)目錄,可以自行修改
docker run -d --name gitlab-runner-01 --restart always -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
然后執(zhí)行注冊(cè)命令:
# 進(jìn)入容器內(nèi)部并執(zhí)行注冊(cè)命令
PS C:\Windows\system32> docker exec -it gitlab-runner-01 gitlab-runner register
Runtime platform arch=amd64 os=linux pid=37 revision=bbcb5aba version=15.3.0
Running in system-mode.
# 填寫gitlab的地址,確保容器內(nèi)能夠順利訪問到該地址
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://10.96.12.149/
# 填寫需要注冊(cè)的runner token,此處選擇shared runner token
Enter the registration token:
BHRagrsztQAxdMPPhxGm
# 描述信息隨意填寫,也可不填
Enter a description for the runner:
[caa2879d6833]: gitlab-runner-01
# tag隨意填寫,此處不填
Enter tags for the runner (comma-separated):
# 隨意填寫,此處不填
Enter optional maintenance note for the runner:
Registering runner... succeeded runner=BHRagrsz
# 選擇執(zhí)行器的種類,建議選擇docker
Enter an executor: docker, shell, ssh, kubernetes, custom, docker-ssh, parallels, virtualbox, docker+machine, docker-ssh+machine:
docker
# 使用最新版本
Enter the default Docker image (for example, ruby:2.7):
docker:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
此時(shí),我們?cè)趃itlab的runner界面就能看到已經(jīng)注冊(cè)好的runner信息了:
2.2 使用windows安裝
首先在官網(wǎng)下載windows安裝包,放到某個(gè)文件夾下面,比如D:\gitlab-runner\gitlab-runner-windows-amd64.exe
,然后依次執(zhí)行如下注冊(cè)和啟動(dòng)命令:
PS C:\Windows\system32> cd D:\gitlab-runner
# 執(zhí)行注冊(cè)命令
PS D:\gitlab-runner> .\gitlab-runner-windows-amd64.exe register
Runtime platform arch=amd64 os=windows pid=15984 revision=bbcb5aba version=15.3.0
# 填寫gitlab的地址,確保容器內(nèi)能夠順利訪問到該地址
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://10.96.12.149/
# 填寫需要注冊(cè)的runner token,此處選擇shared runner token
Enter the registration token:
BHRagrsztQAxdMPPhxGm
Enter a description for the runner:
[GLIC-IT-ZX02]: gitlab-runner-02
Enter tags for the runner (comma-separated):
Enter optional maintenance note for the runner:
Registering runner... succeeded runner=BHRagrsz
Enter an executor: custom, docker, docker-windows, docker-ssh, parallels, shell, ssh, docker+machine, kubernetes, virtualbox, docker-ssh+machine:
docker
Enter the default Docker image (for example, ruby:2.7):
docker:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "D:\\gitlab-runner\\config.toml"
# 執(zhí)行安裝命令
PS D:\gitlab-runner> .\gitlab-runner-windows-amd64.exe install
Runtime platform arch=amd64 os=windows pid=12800 revision=bbcb5aba version=15.3.0
# 執(zhí)行啟動(dòng)命令
PS D:\gitlab-runner> .\gitlab-runner-windows-amd64.exe start
Runtime platform arch=amd64 os=windows pid=9932 revision=bbcb5aba version=15.3.0
此時(shí)才能看到,我們?cè)趙indows上安裝的runner已經(jīng)是就緒狀態(tài)了。
其余系統(tǒng)或者環(huán)境注冊(cè)runner的過程就不再贅述了,基本相同,詳情參考文檔。
三、Gitlab Runner運(yùn)行
我們首先準(zhǔn)備一個(gè)項(xiàng)目,這個(gè)項(xiàng)目?jī)?nèi)容很簡(jiǎn)單,只是作為runner演示使用,總共兩個(gè)文件:
-
index.html
,用來表示項(xiàng)目的主頁(yè);<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>CICD測(cè)試</title> </head> <body> <h1>Gitlab CICD 測(cè)試!!!</h1> </body> </html>
-
.gitlab-ci.yml
,定義CICD流程的文件;build-job: stage: build script: - echo "Hello, $GITLAB_USER_LOGIN!" test-job1: stage: test script: - echo "This job tests something" test-job2: stage: test script: - echo "This job tests something, but takes more time than test-job1." - echo "After the echo commands complete, it runs the sleep command for 20 seconds" - echo "which simulates a test that runs 20 seconds longer than test-job1" - sleep 20 deploy-prod: stage: deploy script: - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
大致的意思是,build階段啟動(dòng)一個(gè)作業(yè),輸出一段文本;test階段并行啟動(dòng)兩個(gè)作業(yè),也是分別輸出一段文本;deploy階段啟動(dòng)一個(gè)作業(yè),輸出一段文本;這三個(gè)階段分別模擬了構(gòu)建、測(cè)試、部署流程,具體的語法關(guān)鍵詞等內(nèi)容不屬于本文范疇,可以參考官網(wǎng)。
以上項(xiàng)目?jī)?nèi)容提交到gitlab后,顯示如下,僅有master分支:
此時(shí),任意的提交行為、合并行為都會(huì)觸發(fā)流水線的執(zhí)行,但是也可以手動(dòng)執(zhí)行,我們此時(shí)選擇項(xiàng)目中的CICD,流水線,并點(diǎn)擊運(yùn)行流水線。
如下是運(yùn)行中的流水線狀態(tài),可以查看各個(gè)階段的任務(wù),點(diǎn)擊任務(wù)圖標(biāo)還可以查看具體的任務(wù)執(zhí)行明細(xì)。
需要注意的是,流水線中包含多個(gè)任務(wù)時(shí),這些任務(wù)是會(huì)被Gitlab CI隨機(jī)調(diào)度到可以運(yùn)行的runner上運(yùn)行的,并非一直在某一個(gè)runner上,如下是運(yùn)行三次流水線,每個(gè)runner的任務(wù)執(zhí)行數(shù)量統(tǒng)計(jì):
至此,我們完成了兩個(gè)runner的搭建和注冊(cè),并運(yùn)行了示例項(xiàng)目中的流水線。后面文章我們?cè)僦v解工業(yè)級(jí)流水線的配置和高級(jí)的CICD內(nèi)容。
參考資料