CI/CD: GitLab Runner安裝注冊配置管理
GitLab Runner是一個開源項目,用于運行您的作業(yè)并將結(jié)果發(fā)送回GitLab。它與GitLab CI結(jié)合使用,GitLab CI是GitLab隨附的用于協(xié)調(diào)作業(yè)的開源持續(xù)集成服務(wù)。
安裝要求
GitLab Runner是用Go編寫的,可以作為一個二進(jìn)制文件運行,不需要特定于語言的要求。它旨在在GNU / Linux,macOS和Windows操作系統(tǒng)上運行。只要您可以在其他操作系統(tǒng)上編譯Go二進(jìn)制文件,其他操作系統(tǒng)就可能會運行。
如果要使用Docker,請安裝最新版本。GitLab Runner需要最少的Docker v1.13.0。
GitLab Runner版本應(yīng)與GitLab版本同步。
可以在GNU / Linux,macOS,F(xiàn)reeBSD和Windows上安裝和使用GitLab Runner 。您可以使用Docker安裝它,手動下載二進(jìn)制文件,也可以使用GitLab提供的rpm / deb軟件包的存儲庫。
基于centos安裝
curl -LJO https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_<arch>.rpm
rpm -i gitlab-runner_<arch>.rpm
rpm -Uvh gitlab-runner_<arch>.rpm
基于macos系統(tǒng)安裝
sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/v12.6/binaries/gitlab-runner-darwin-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
gitlab-runner install
gitlab-runner start
基于Docker運行
mkdir ~/data/gitlab-runner/config
docker run --rm -t -id -v ~/data/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:v12.9.0
GitLab Runner注冊
- 類型
- shared :運行整個平臺項目的作業(yè)(gitlab)
- group:運行特定group下的所有項目的作業(yè)(group)
- specific: 運行指定的項目作業(yè)(project)
- 狀態(tài)
- locked:鎖定無法運行項目作業(yè)
- paused:暫停不會運行作業(yè)
獲取shared類型runnertoken
獲取group類型的runnertoken
進(jìn)入group -> Settings -> CI/CD -> Runners -> Group Runners
獲取specific類型的runnertoken
進(jìn)入具體的項目 -> Settings -> CI/CD -> Runners -> Specific Runners。
啟動容器交互式注冊
docker run --rm -t -i -v ~/data/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:v12.6.0 register
Runtime platform arch=amd64 os=linux pid=6 revisinotallow=ac8e767a versinotallow=12.6.0
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.1.105
Please enter the gitlab-ci token for this runner:
4tutaeWWL3srNEcmHs1s
Please enter the gitlab-ci description for this runner:
[00e4f023b5ae]: devops-service-runner
Please enter the gitlab-ci tags for this runner (comma separated):
build
Registering runner... succeeded runner=4tutaeWW
Please enter the executor: parallels, virtualbox, docker-ssh+machine, kubernetes, docker+machine, custom, docker, docker-ssh, shell, ssh:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
非交互式注冊
docker run -itd --rm -v ~/data/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:v12.6.0 register \
--non-interactive \
--executor "shell" \
--url "http://192.168.1.200:30088/" \
--registration-token "JRzzw2j1Ji6aBjwvkxAv" \
--description "devops-runner" \
--tag-list "build,deploy" \
--run-untagged="true" \
--locked="false" \
--access-level="not_protected"
效果:
常用命令
啟動命令:
gitlab-runner --debug <command> #調(diào)試模式排查錯誤特別有用。
gitlab-runner <command> --help #獲取幫助信息
gitlab-runner run #普通用戶模式 配置文件位置 ~/.gitlab-runner/config.toml
sudo gitlab-runner run # 超級用戶模式 配置文件位置/etc/gitlab-runner/config.toml
注冊命令:
gitlab-runner register #默認(rèn)交互模式下使用,非交互模式添加 --non-interactive
gitlab-runner list #此命令列出了保存在配置文件中的所有運行程序
gitlab-runner verify #此命令檢查注冊的runner是否可以連接,但不驗證GitLab服務(wù)是否正在使用runner。--delete 刪除
gitlab-runner unregister #該命令使用GitLab取消已注冊的runner。
#使用令牌注銷
gitlab-runner unregister --url http://gitlab.example.com/ --token t0k3n
#使用名稱注銷(同名刪除第一個)
gitlab-runner unregister --name test-runner
#注銷所有
gitlab-runner unregister --all-runners
服務(wù)管理:
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# --user指定將用于執(zhí)行構(gòu)建的用戶
#`--working-directory 指定將使用**Shell** executor 運行構(gòu)建時所有數(shù)據(jù)將存儲在其中的根目錄
gitlab-runner uninstall #該命令停止運行并從服務(wù)中卸載GitLab Runner。
gitlab-runner start #該命令啟動GitLab Runner服務(wù)。
gitlab-runner stop #該命令停止GitLab Runner服務(wù)。
gitlab-runner restart #該命令將停止,然后啟動GitLab Runner服務(wù)。
gitlab-runner status #此命令顯示GitLab Runner服務(wù)的狀態(tài)。當(dāng)服務(wù)正在運行時,退出代碼為零;而當(dāng)服務(wù)未運行時,退出代碼為非零。
運行Pipeline
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"
GitLabCI 有助于DevOps人員,例如敏捷開發(fā)中,開發(fā)與運維是同一個人,最便捷的開發(fā)方式。JenkinsCI適合在多角色團隊中,職責(zé)分明、配置與代碼分離、插件豐富。