天天看點

使用 GitLab Runner

簡介

了解了上面的基本概念之後,有沒有覺得少了些什麼東西 —— 由誰來執行這些建構任務呢?

答案就是 GitLab Runner 了!

想問為什麼不是 GitLab CI 來運作那些建構任務?

一般來說,建構任務都會占用很多的系統資源 (譬如編譯代碼),而 GitLab CI 又是 GitLab 的一部分,如果由 GitLab CI 來運作建構任務的話,在執行建構任務的時候,GitLab 的性能會大幅下降。

GitLab CI 最大的作用是管理各個項目的建構狀态,是以,運作建構任務這種浪費資源的事情就交給 GitLab Runner 來做拉!

因為 GitLab Runner 可以安裝到不同的機器上,是以在建構任務運作期間并不會影響到 GitLab 的性能

安裝

  • 在目标主機上安裝 GitLab Runner,這裡的目标主機指你要部署的伺服器
  • 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           

複制

注冊 Runner

安裝好 GitLab Runner 之後,我們隻要啟動 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!            

複制

說明:

  • gitlab-ci-multi-runner register:執行注冊指令
  • 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 名稱
  • Please enter the gitlab-ci tags for this runner:設定 tag
  • Whether to run untagged builds:這裡選擇 true ,代碼上傳後會能夠直接執行
  • Whether to lock Runner to current project:直接回車,不用輸入任何密碼
  • Please enter the executor:選擇 runner 類型,這裡我們選擇的是 shell

CI 的位址和令牌,在 項目 --> 設定 --> CI/CD --> Runner 設定:

使用 GitLab Runner

.gitlab-ci.yml

在項目工程下編寫 .gitlab-ci.yml 配置檔案:

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

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

# 安裝依賴
install_deps:
  stage: install_deps
  only:
    - develop
    - master
  script:
    - npm install

# 運作測試用例
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

# 部署測試伺服器
deploy_test:
  stage: deploy_test
  only:
    - develop
  script:
    - pm2 delete app || true
    - pm2 start app.js --name app

# 部署生産伺服器
deploy_production:
  stage: deploy_production
  only:
    - master
  script:
    - bash scripts/deploy/deploy.sh           

複制

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

  • 安裝依賴(install_deps)
  • 運作測試(test)
  • 編譯(build)
  • 部署測試伺服器(deploy_test)
  • 部署生産伺服器(deploy_production)

設定 Job.only 後,隻有當 develop 分支和 master 分支有送出的時候才會觸發相關的 Jobs。

節點說明:

  • stages:定義建構階段,這裡隻有一個階段 deploy
  • deploy:建構階段 deploy 的詳細配置也就是任務配置
  • script:需要執行的 shell 腳本
  • only:這裡的 master 指在送出到 master 時執行
  • tags:與注冊 runner 時的 tag 比對

其它配置

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

  • 安裝完 GitLab Runner 後系統會增加一個 gitlab-runner 賬戶,我們将它加進 root 組:
gpasswd -a gitlab-runner root           

複制

  • 配置需要操作目錄的權限,比如你的 runner 要在 gaming 目錄下操作:
chmod 775 gaming           

複制

  • 由于我們的 shell 腳本中有執行 git pull 的指令,我們直接設定以 ssh 方式拉取代碼:
su gitlab-runner
ssh-keygen -t rsa -C "你在 GitLab 上的郵箱位址"
cd 
cd .ssh
cat id_rsa.pub           

複制

  • 複制 id_rsa.pub 中的秘鑰到 GitLab:
使用 GitLab Runner
  • 通過 ssh 的方式将代碼拉取到本地

測試內建效果

所有操作完成後 push 代碼到伺服器,檢視是否成功:

使用 GitLab Runner

passed 表示執行成功

其他指令

删除注冊資訊:

gitlab-ci-multi-runner unregister --name "名稱"           

複制

檢視注冊清單:

gitlab-ci-multi-runner list           

複制