目錄
一、gitlab-runner 簡介
1.1 要求
1.2 特點(diǎn)
二、GitLab Runner 安裝
2.1 使用 GItLab 官方倉庫安裝
2.2 使用 deb/rpm 軟件包
2.3 在容器中運(yùn)行 GitLab Runner
三、GitLab Runner 注冊(cè)
3.1 GitLabRunner 類型
3.2 獲取 runner token
獲取?shared?類型 runner token ?
?獲取?group?類型的 runner token ?
?獲取?specific?類型的 runner token ?
3.3 進(jìn)行注冊(cè)
四、GitLab Runner 命令
4.1 啟動(dòng)命令
4.2 注冊(cè)命令
4.3 服務(wù)管理
五、運(yùn)行流水線任務(wù)(效果演示)
5.1 編寫一個(gè)簡單的 yml 文件
5.2 測(cè)試流水線
一、gitlab-runner 簡介
????????GitLab Runner 是一個(gè)開源項(xiàng)目,用于運(yùn)行您的作業(yè)并將結(jié)果發(fā)送回GitLab。它與 Gitlab CI?結(jié)合使用, Gitlab CI?是 Gitlab 隨附的用于協(xié)調(diào)作業(yè)的開源持續(xù)集成服務(wù)。
1.1 要求
-
GitLab Runner 是用?Go?編寫的,可以作為一個(gè)二進(jìn)制文件運(yùn)行,不需要特定于語言的要求。它旨在在 GNU/Linux,macOS 和 Windows 操作系統(tǒng)上運(yùn)行。只要您可以在其他操作系統(tǒng)上編譯 Go 二進(jìn)制文件,其他操作系統(tǒng)就可能會(huì)運(yùn)行。
-
如果要使用 Docker,請(qǐng)安裝最新版本。GitLab Runner 需要最少的 Docker
v1.13.0
。 -
GitLab Runner 版本應(yīng)與 GitLab 版本同步。盡管較舊的 Runner 仍可以使用較新的 GitLab 版本,反之亦然,但在某些情況下,如果版本存在差異,則功能可能不可用或無法正常工作。在次要版本更新之間可以保證向后兼容性,但是請(qǐng)注意,GitLab 的次要版本更新會(huì)引入新功能,這些新功能將要求 Runner 在同一次要版本上使用。
1.2 特點(diǎn)
-
允許運(yùn)行:
-
同時(shí)執(zhí)行多個(gè)作業(yè)。
-
對(duì)多個(gè)服務(wù)器(甚至每個(gè)項(xiàng)目)使用多個(gè)令牌。
-
限制每個(gè)令牌的并行作業(yè)數(shù)。
-
-
可以運(yùn)行作業(yè):
-
在本地。
-
使用 Docker 容器。
-
使用 Docker 容器并通過 SSH 執(zhí)行作業(yè)。
-
使用 Docker 容器在不同的云和虛擬化管理程序上自動(dòng)縮放。
-
連接到遠(yuǎn)程 SSH 服務(wù)器。
-
-
用 Go 編寫并以單個(gè)二進(jìn)制文件的形式分發(fā),而沒有其他要求。
-
支持 Bash,Windows Batch 和 Windows PowerShell。
-
在 GNU / Linux,macOS 和 Windows(幾乎可以在任何可以運(yùn)行 Docker 的地方)上運(yùn)行。
-
允許自定義作業(yè)運(yùn)行環(huán)境。
-
自動(dòng)重新加載配置,無需重啟。
-
易于使用的設(shè)置,并支持 Docker,Docker-SSH,Parallels 或 SSH 運(yùn)行環(huán)境。
-
啟用 Docker?容器的緩存。
-
易于安裝,可作為 GNU / Linux,macOS 和 Windows 的服務(wù)。
-
嵌入式 Prometheus 指標(biāo) HTTP 服務(wù)器。
-
裁判工作者監(jiān)視 Prometheus 度量標(biāo)準(zhǔn)和其他特定于工作的數(shù)據(jù)并將其傳遞給 GitLab。
二、GitLab Runner 安裝
2.1 使用 GItLab 官方倉庫安裝
參考官方文檔:使用官方極狐GitLab 倉庫安裝極狐GitLab Runner | 極狐GitLab?
2.2 使用 deb/rpm 軟件包
清華源下載地址:Index of /gitlab-runner/yum/el7/ | 清華大學(xué)開源軟件鏡像站 | Tsinghua Open Source Mirror
?
# 安裝
rpm -ivh gitlab-runner-15.3.3-1.x86_64.rpm
# 啟動(dòng)服務(wù)
systemctl start gitlab-runner
2.3 在容器中運(yùn)行 GitLab Runner
參考官方文檔:在容器中運(yùn)行極狐GitLab Runner | 極狐GitLab
三、GitLab Runner 注冊(cè)
官方參考文檔:注冊(cè) Runner | 極狐GitLab?
大概過程:獲取 runner token -> 進(jìn)行注冊(cè)?
3.1 GitLabRunner 類型
-
shared:運(yùn)行整個(gè)平臺(tái)項(xiàng)目的作業(yè)(gitlab)
-
group:運(yùn)行特定 group 下的所有項(xiàng)目的作業(yè)(group)
-
specific: 運(yùn)行指定的項(xiàng)目作業(yè)(project)
-
locked:無法運(yùn)行項(xiàng)目作業(yè)
-
paused:不會(huì)運(yùn)行作業(yè)
3.2 獲取 runner token
獲取?shared?
類型 runner token ?
需要管理員訪問極狐 GitLab 管理中心并點(diǎn)擊?概覽 > Runner。?
?
?獲取?group?
類型的 runner token ?
訪問?設(shè)置 > CI/CD?并展開?Runner
??
?
?獲取?specific?
類型的 runner token ?
進(jìn)入具體的項(xiàng)目 -> Settings -> CI/CD -> Runners -> Specific Runners ?
??
?
?
3.3 進(jìn)行注冊(cè)
方式一:交互式注冊(cè)
官方步驟:注冊(cè) Runner | 極狐GitLab?
[root@run01 ~]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=3293 revision=f767c145 version=15.3.3
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.170.133/
Enter the registration token:
GR1348941sUxNyye1qD4HcTSW-TMw
Enter a description for the runner:
[run01]: test
Enter tags for the runner (comma-separated):
build
Enter optional maintenance note for the runner:
this is d test
Registering runner... succeeded runner=GR1348941sUxNyye1
Enter an executor: custom, parallels, shell, docker-ssh+machine, docker, docker-ssh, ssh, virtualbox, docker+machine, kubernetes:
shell
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"
方式二:直接注冊(cè)
參考官方文檔:注冊(cè) Runner | 極狐GitLab
效果:注冊(cè)了一個(gè)指定的 runner 并且是鎖定狀態(tài)的
?
四、GitLab Runner 命令
GitLab Runner 包含一組命令,可用于注冊(cè),管理和運(yùn)行構(gòu)建。
4.1 啟動(dòng)命令
gitlab-runner --debug <command> # 調(diào)試模式排查錯(cuò)誤特別有用。
gitlab-runner <command> --help # 獲取幫助信息
gitlab-runner run # 普通用戶模式 配置文件位置 ~/.gitlab-runner/config.toml
sudo gitlab-runner run # 超級(jí)用戶模式 配置文件位置 /etc/gitlab-runner/config.toml
4.2 注冊(cè)命令
gitlab-runner register # 默認(rèn)交互模式下使用,非交互模式添加 --non-interactive
gitlab-runner list # 此命令列出了保存在配置文件中的所有運(yùn)行程序
gitlab-runner verify # 此命令檢查注冊(cè)的 runner 是否可以連接,但不驗(yàn)證 GitLab 服務(wù)是否正在使用 runner。 --delete 刪除
gitlab-runner unregister # 該命令使用 GitLab 取消已注冊(cè)的 runner。
# 使用令牌注銷
gitlab-runner unregister --url http://xxx/ --token t0kxx
# 使用名稱注銷(同名刪除第一個(gè))
gitlab-runner unregister --name test-runner
# 注銷所有
gitlab-runner unregister --all-runners
4.3 服務(wù)管理
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# --user 指定將用于執(zhí)行構(gòu)建的用戶
# --working-directory 指定將使用 Shell executor 運(yùn)行構(gòu)建時(shí)所有數(shù)據(jù)將存儲(chǔ)在其中的根目錄
gitlab-runner uninstall # 該命令停止運(yùn)行并從服務(wù)中卸載 GitLab Runner。
gitlab-runner start # 該命令啟動(dòng) GitLab Runner 服務(wù)。
gitlab-runner stop # 該命令停止 GitLab Runner 服務(wù)。
gitlab-runner restart # 該命令將停止,然后啟動(dòng) GitLab Runner 服務(wù)。
gitlab-runner status # 此命令顯示 GitLab Runner 服務(wù)的狀態(tài)。當(dāng)服務(wù)正在運(yùn)行時(shí),退出代碼為零;而當(dāng)服務(wù)未運(yùn)行時(shí),退出代碼為非零。
# 也可以是使用 systemctl 管理 runner
五、運(yùn)行流水線任務(wù)(效果演示)
5.1 編寫一個(gè)簡單的 yml 文件
在 gitlab 倉庫中項(xiàng)目根目錄添加一個(gè) .gitlab-ci.yml 文件,文件內(nèi)容如下:
stages:
- build
- deploy
build:
stage: build
tags:
- build
only:
- master
script:
- echo "mvn clean "
- echo "mvn install"
deploy:
stage: deploy
tags:
- deploy
only:
- master
script:
- echo "hello deploy"
這個(gè)流水線共包含兩個(gè) job,分別是?build 和 deploy
。
? build?
job 包含一個(gè) stage?build
。?build?
stage 配置了在具有?build?
標(biāo)簽的 runner 中運(yùn)行,限制為 master 分支提交,運(yùn)行構(gòu)建命令。
? deploy?
job 包含一個(gè) stage?deploy
。?deploy?
stage 配置了在具有?deploy?
標(biāo)簽的 runner 中運(yùn)行,限制為 master 分支提交,運(yùn)行發(fā)布命令。
5.2 測(cè)試流水線
編寫好 yml 文件之后,開始模擬在 master 分支提交代碼,此時(shí)會(huì)發(fā)現(xiàn)流水線正在運(yùn)行。?
上一篇文章:【基于 GitLab 的 CI/CD 實(shí)踐】01、GitLab CI/CD 基礎(chǔ)概念_Stars.Sky的博客-CSDN博客文章來源:http://www.zghlxwxcb.cn/news/detail-597368.html
下一篇文章:【基于 GitLab 的 CI/CD 實(shí)踐】03、GitLab Pipeline 實(shí)踐(上)_Stars.Sky的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-597368.html
到了這里,關(guān)于【基于 GitLab 的 CI/CD 實(shí)踐】02、gitlab-runner 實(shí)踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!