centos 安裝 gitlab runner
- 安裝方法 1 :centos yum 安裝方式 版本為 9.5.1
# 添加yum源
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
# 安裝
yum install gitlab-runner
# 查看版本
gitlab-runner -v
- 安裝方法 2: 官網(wǎng)提供的安裝方式逛万,當(dāng)前版本為12.1.0
官網(wǎng):https://docs.gitlab.com/runner/install/linux-manually.html
# 我當(dāng)前centos 7 為 Linux x86-64
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# 設(shè)置安裝目錄可執(zhí)行
sudo chmod +x /usr/local/bin/gitlab-runner
# 直接使用 root 用戶權(quán)限運(yùn)行 gitlab-runner (或者其他用戶啤贩,看需求)
sudo gitlab-runner install --user=root --working-directory=/home/gitlab-runner
#啟動
sudo gitlab-runner start
# 如果提示命令 command not found 需要配置環(huán)境
# 添加軟鏈接
ln -s -f /usr/local/bin/gitlab-runner /usr/bin/gitlab-runner
# 查看版本
gitlab-runner -v
注冊 gitlab-runner
通過管理員登錄 gitlab ---- 管理中心---- 概況 ---- Runner 查看需要注冊的 URL與 Token(令牌)
- 配置注冊信息
gitlab-runner register
[root@localhost ~]# gitlab-runner register
Running in system-mode.
# 引導(dǎo)會讓你輸入gitlab的url陨闹,輸入自己的url久窟,例如http://gitlab.example.com/
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://xxx.xxx.xxx:xxx/
# 引導(dǎo)會讓你輸入token革砸,去相應(yīng)的項(xiàng)目下找到token蔼两,例如xrjc3tWcdQpLcEwoYzkU
Please enter the gitlab-ci token for this runner:
xrjc3tWcdQpLcEwoYzkU
# 輸入描述
Please enter the gitlab-ci description for this runner:
[localhost.localdomain]: develop
# 引導(dǎo)會讓你輸入tag酝枢,一個項(xiàng)目可能有多個runner匈仗,是根據(jù)tag來區(qū)別runner的瓢剿,輸入若干個就好了,比如web,hook,deploy,develop
Please enter the gitlab-ci tags for this runner (comma separated):
develop
# 是否運(yùn)行未標(biāo)記的版本
Whether to run untagged builds [true/false]:
[false]: false
# 是否將運(yùn)行程序鎖定到當(dāng)前項(xiàng)目
Whether to lock Runner to current project [true/false]:
[false]: true
Registering runner... succeeded runner=xrjc3tWc
# 引導(dǎo)會讓你輸入executor悠轩,這個是要用什么方式來執(zhí)行腳本间狂,圖方便輸入shell就好了
Please enter the executor: shell, ssh, docker+machine, docker, docker-ssh, parallels, virtualbox, docker-ssh+machine, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
注冊好后,在gitlab中相應(yīng)的位置就可以看到你注冊好的runner信息火架。
自定義構(gòu)建目錄
對應(yīng)的配置文件在 /etc/gitlab-runner/config.toml
修改配置文件鉴象,允許自定義git clone 的目錄
此功能要求GIT_CLONE_PATH在其中定義的路徑內(nèi)runners.builds_dir。為了便于使用的builds_dir所述 $CI_BUILDS_DIR變量可被使用何鸡。
#配置構(gòu)建的根目錄
builds_dir = "/www/wwwroot"
#允許用戶自定義構(gòu)建目錄 gitbab-ci.yml 變量 GIT_CLONE_PATH
[runners.custom_build_dir]
enabled = true
gitlab-runner 配置說明:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section
對應(yīng)執(zhí)行命令
# 運(yùn)行
gitlab-runner run
# 啟動
gitlab-runner start
# 重啟
gitlab-runner restart
# 通過name 取消注冊
gitlab-runner unregister --name develop
# 刪除所有注冊runner
gitlab-runner unregister --all-runners
Runner 狀態(tài)說明
每個Runner可以處于以下狀態(tài)中的其中一種:
- shared - Runner將運(yùn)行所有未指定的項(xiàng)目的作業(yè)
- group - Runner將運(yùn)行群組中所有未指定項(xiàng)目的作業(yè)
- specific - Runner將運(yùn)行指定項(xiàng)目的作業(yè)
- locked - 無法將Runner分配給其他項(xiàng)目
- paused - Runner不會接受新的作業(yè)
什么情況下需要注冊Shared Runner纺弊?
比如,GitLab上面所有的工程都有可能需要在公司的服務(wù)器上進(jìn)行編譯骡男、測試淆游、部署等工作,這個時(shí)候注冊一個Shared Runner供所有工程使用就很合適。
什么情況下需要注冊Specific Runner稽犁?
比如焰望,我可能需要在我個人的電腦或者服務(wù)器上自動構(gòu)建我參與的某個工程,這個時(shí)候注冊一個Specific Runner就很合適已亥。
什么情況下需要在同一臺機(jī)器上注冊多個Runner熊赖?
比如,我是GitLab的普通用戶虑椎,沒有管理員權(quán)限震鹉,我同時(shí)參與多個項(xiàng)目,那我就需要為我的所有項(xiàng)目都注冊一個Specific Runner捆姜,這個時(shí)候就需要在同一臺機(jī)器上注冊多個Runner传趾。
項(xiàng)目應(yīng)用 gitlab-ci.yml
更多的 .gitlab-ci.yml 配置規(guī)則 詳見
參考:
http://www.reibang.com/p/0ab8bbe05d27
https://blog.51cto.com/vnimos/2122951?source=dra
gitlab-ci.yml 文件編寫主要內(nèi)容
cache
定義需要被緩存的文件、文件夾列表泥技,只適用于項(xiàng)目目錄中的文件和文件夾浆兰。
stages
定義流水線階段(pipeline),如果沒有stages被定義.gitlab-ci.yml珊豹,那么build簸呈, test和deploy允許被用作默認(rèn)作業(yè)的階段。
stage
申明當(dāng)前的階段店茶,在 stages 中使用蜕便。如果作業(yè)未指定階段,則默認(rèn)作業(yè)test
variables
用于定義變量
before_script
覆蓋在作業(yè)之前執(zhí)行的腳本或命令
after_script
覆蓋在作業(yè)之后執(zhí)行的腳本或命令
script
定義由Runner執(zhí)行的shell腳本或命令
changes
指定 stage 觸發(fā)條件
refs
指定 stage 觸發(fā)的分支
tags
指定執(zhí)行作業(yè)的runner
執(zhí)行流程
- 首先贩幻,所有工作build都是并行執(zhí)行的轿腺。
- 如果所有作業(yè)都build成功,則test作業(yè)將并行執(zhí)行丛楚。
- 如果所有作業(yè)都test成功族壳,則deploy作業(yè)將并行執(zhí)行。
- 如果所有作業(yè)都deploy成功趣些,則提交標(biāo)記為passed决侈。
- 如果任何先前的作業(yè)失敗,則將提交標(biāo)記為喧务,failed并且不執(zhí)行其他階段的作業(yè)赖歌。
gitlab-ci.yml 示例
實(shí)現(xiàn)需求如下:
- 在服務(wù)器上 拉去指定git項(xiàng)目到指定目錄中
- 在開發(fā)服務(wù)器上每次推送都自動更新develop分支的代碼
- 在正式服務(wù)器上,每次master分支更新時(shí)都會自動更新代碼
- 如果服務(wù)器上的git代碼發(fā)生改變功茴,先本地儲藏修改的內(nèi)容庐冯。
由于當(dāng)前暫時(shí)沒有解決gitlab runner 保留服務(wù)器上生成的文件,所以使用了 GIT_STRATEGY:none
的方式坎穿,就是不直接用runner 本身的拉去方式展父。而用shell的方式 clone 代碼返劲。所以要先配置服務(wù)器上ssh拉去代碼的方式。
git ssh 拉去代碼配置方式如下:
http://www.reibang.com/p/9c8c5a7ea634
執(zhí)行流程:
1栖茉、在開發(fā)服務(wù)器與正式環(huán)境服務(wù)器上安裝 gitlab runner篮绿,并指定項(xiàng)目執(zhí)行。
2吕漂、在對應(yīng)服務(wù)器上配置 git ssh秘鑰拉去方式亲配。詳情
3、在gitlab 項(xiàng)目根目錄下創(chuàng)建 gitlab-ci.yml
文件惶凝。請根據(jù)各自修改更改內(nèi)如吼虎。
4、提交代碼到gitlab苍鲜,查看執(zhí)行效果思灰。運(yùn)行狀態(tài)通過則配置成功。
gitlab-ci.yml 內(nèi)如如下:
#定義變量
variables:
#定義克隆的路徑 $CI_BUILDS_DIR 為 runners.builds_dir 設(shè)置的路徑
GIT_CLONE_PATH: $CI_BUILDS_DIR/server/link
#git ssh 地址
GITLAB_SSH: ssh://git@192.168.10.10:50022/server/link.git
# 指定git獲取代碼的方式(clone,fetch,none)
GIT_STRATEGY: none
#在作業(yè)之前執(zhí)行的腳本或命令
before_script:
- echo "環(huán)境部署操作"
# 創(chuàng)建對應(yīng)目錄
- mkdir -p $GIT_CLONE_PATH
- cd $GIT_CLONE_PATH
#在作業(yè)之后執(zhí)行的腳本或命令
after_script:
#配置目錄的用戶權(quán)限
- chown -R www:www $GIT_CLONE_PATH
# 全局定義流水線階段(pipeline)
stages:
- build
develop_build:
stage: build
#需要執(zhí)行的shell腳本
script:
- echo "開發(fā)服務(wù)器環(huán)境配置"
- if [ ! -d ".git" ]; then
- git clone -b develop $GITLAB_SSH $GIT_CLONE_PATH --depth 1
- else
- git stash
- fi
- git pull
only:
# 指定分支
- develop
tags:
# 指定執(zhí)行作業(yè)的runner
- developRunner
master_build:
stage: build
script:
- echo "正式服務(wù)器環(huán)境配置"
- if [ ! -d ".git" ]; then
- git clone -b master $GITLAB_SSH $GIT_CLONE_PATH --depth 1
- else
- git stash
- fi
- git pull
only:
- master
tags:
- masterRunner
** 常見問題 **
驗(yàn)證 gitlab-ci.yml
文件格式是否正確混滔,在gitlab CI/CD的流水線或作業(yè)中有CI配置檢查 (CI Lint)洒疚。復(fù)制寫好的 gitlab-ci.yml
文件內(nèi)如到 CI Lint 驗(yàn)證。
提交代碼后查看運(yùn)行狀態(tài)