GitLab Runner

簡(jiǎn)介

理解了上面的基本概念之后,有沒(méi)有覺(jué)得少了些什么東西 —— 由誰(shuí)來(lái)執(zhí)行這些構(gòu)建任務(wù)呢?
答案就是 GitLab Runner 了!

想問(wèn)為什么不是 GitLab CI 來(lái)運(yùn)行那些構(gòu)建任務(wù)?

一般來(lái)說(shuō),構(gòu)建任務(wù)都會(huì)占用很多的系統(tǒng)資源 (譬如編譯代碼),而 GitLab CI 又是 GitLab 的一部分,如果由 GitLab CI 來(lái)運(yùn)行構(gòu)建任務(wù)的話,在執(zhí)行構(gòu)建任務(wù)的時(shí)候,GitLab 的性能會(huì)大幅下降。

GitLab CI 最大的作用是管理各個(gè)項(xiàng)目的構(gòu)建狀態(tài),因此,運(yùn)行構(gòu)建任務(wù)這種浪費(fèi)資源的事情就交給 GitLab Runner 來(lái)做拉!

因?yàn)?GitLab Runner 可以安裝到不同的機(jī)器上,所以在構(gòu)建任務(wù)運(yùn)行期間并不會(huì)影響到 GitLab 的性能

安裝

  • 在目標(biāo)主機(jī)上安裝 GitLab Runner,這里的目標(biāo)主機(jī)指你要部署的服務(wù)器
  • Ubuntu 安裝腳本:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
sudo apt-get update
sudo apt-get install gitlab-ci-multi-runner

注冊(cè) Runner

安裝好 GitLab Runner 之后,我們只要啟動(dòng) Runner 然后和 GitLab CI 綁定:

[root@iZbp1fmnx8oyubksjdk7leZ gitbook]# gitlab-ci-multi-runner register
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.75.146:8080/
Please enter the gitlab-ci token for this runner:
1Lxq_f1NRfCfeNbE5WRh
Please enter the gitlab-ci description for this runner:
[iZbp1fmnx8oyubksjdk7leZ]: deploy-gaming
Please enter the gitlab-ci tags for this runner (comma separated):
deploy
Whether to run untagged builds [true/false]:
[false]: true
Whether to lock Runner to current project [true/false]:
[false]: 
Registering runner... succeeded                     runner=P_zfkhTb
Please enter the executor: virtualbox, docker+machine, parallels, shell, ssh, docker-ssh+machine, kubernetes, docker, docker-ssh:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

說(shuō)明:

  • gitlab-ci-multi-runner register:執(zhí)行注冊(cè)命令
  • Please enter the gitlab-ci coordinator URL:輸入 ci 地址
  • Please enter the gitlab-ci token for this runner:輸入 ci token
  • Please enter the gitlab-ci description for this runner:輸入 runner 名稱(chēng)
  • Please enter the gitlab-ci tags for this runner:設(shè)置 tag
  • Whether to run untagged builds:這里選擇 true ,代碼上傳后會(huì)能夠直接執(zhí)行
  • Whether to lock Runner to current project:直接回車(chē),不用輸入任何口令
  • Please enter the executor:選擇 runner 類(lèi)型,這里我們選擇的是 shell

CI 的地址和令牌,在 項(xiàng)目 --> 設(shè)置 --> CI/CD --> Runner 設(shè)置:

image.png

.gitlab-ci.yml

在項(xiàng)目工程下編寫(xiě) .gitlab-ci.yml 配置文件:

stages:
  - install_deps
  - test
  - build
  - deploy_test
  - deploy_production

cache:
  key: ${CI_BUILD_REF_NAME}
  paths:
    - node_modules/
    - dist/

# 安裝依賴(lài)
install_deps:
  stage: install_deps
  only:
    - develop
    - master
  script:
    - npm install

# 運(yùn)行測(cè)試用例
test:
  stage: test
  only:
    - develop
    - master
  script:
    - npm run test

# 編譯
build:
  stage: build
  only:
    - develop
    - master
  script:
    - npm run clean
    - npm run build:client
    - npm run build:server

# 部署測(cè)試服務(wù)器
deploy_test:
  stage: deploy_test
  only:
    - develop
  script:
    - pm2 delete app || true
    - pm2 start app.js --name app

# 部署生產(chǎn)服務(wù)器
deploy_production:
  stage: deploy_production
  only:
    - master
  script:
    - bash scripts/deploy/deploy.sh

上面的配置把一次 Pipeline 分成五個(gè)階段:

  • 安裝依賴(lài)(install_deps)
  • 運(yùn)行測(cè)試(test)
  • 編譯(build)
  • 部署測(cè)試服務(wù)器(deploy_test)
  • 部署生產(chǎn)服務(wù)器(deploy_production)

設(shè)置 Job.only 后,只有當(dāng) develop 分支和 master 分支有提交的時(shí)候才會(huì)觸發(fā)相關(guān)的 Jobs。

節(jié)點(diǎn)說(shuō)明:

  • stages:定義構(gòu)建階段,這里只有一個(gè)階段 deploy
  • deploy:構(gòu)建階段 deploy 的詳細(xì)配置也就是任務(wù)配置
  • script:需要執(zhí)行的 shell 腳本
  • only:這里的 master 指在提交到 master 時(shí)執(zhí)行
  • tags:與注冊(cè) runner 時(shí)的 tag 匹配

其它配置

為保證能夠正常集成,我們還需要一些其它配置:

  • 安裝完 GitLab Runner 后系統(tǒng)會(huì)增加一個(gè) gitlab-runner 賬戶(hù),我們將它加進(jìn) root 組:
gpasswd -a gitlab-runner root
  • 配置需要操作目錄的權(quán)限,比如你的 runner 要在 gaming 目錄下操作:
chmod 775 gaming
  • 由于我們的 shell 腳本中有執(zhí)行 git pull 的命令,我們直接設(shè)置以 ssh 方式拉取代碼:
su gitlab-runner
ssh-keygen -t rsa -C "你在 GitLab 上的郵箱地址"
cd 
cd .ssh
cat id_rsa.pub
  • 復(fù)制 id_rsa.pub 中的秘鑰到 GitLab:
image.png
  • 通過(guò) ssh 的方式將代碼拉取到本地

測(cè)試集成效果

所有操作完成后 push 代碼到服務(wù)器,查看是否成功:


image.png

passed 表示執(zhí)行成功

其他命令

刪除注冊(cè)信息:

gitlab-ci-multi-runner unregister --name "名稱(chēng)"

查看注冊(cè)列表:

gitlab-ci-multi-runner list
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容