學(xué)習(xí)完整課程請(qǐng)移步 互聯(lián)網(wǎng) Java 全棧工程師
本節(jié)視頻
簡(jiǎn)介
理解了上面的基本概念之后,有沒有覺得少了些什么東西 —— 由誰來執(zhí)行這些構(gòu)建任務(wù)呢?
答案就是 GitLab Runner 了!
想問為什么不是 GitLab CI 來運(yùn)行那些構(gòu)建任務(wù)?
一般來說,構(gòu)建任務(wù)都會(huì)占用很多的系統(tǒng)資源 (譬如編譯代碼),而 GitLab CI 又是 GitLab 的一部分,如果由 GitLab CI 來運(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 來做拉!
因?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!
說明:
- 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 名稱
- 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:直接回車,不用輸入任何口令
- Please enter the executor:選擇 runner 類型,這里我們選擇的是 shell
CI 的地址和令牌,在 項(xiàng)目 --> 設(shè)置 --> CI/CD --> Runner 設(shè)置:
.gitlab-ci.yml
在項(xiàng)目工程下編寫 .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
# 運(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è)階段:
- 安裝依賴(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)說明:
- 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 賬戶,我們將它加進(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:
- 通過 ssh 的方式將代碼拉取到本地
測(cè)試集成效果
所有操作完成后 push 代碼到服務(wù)器,查看是否成功:
passed 表示執(zhí)行成功
其他命令
刪除注冊(cè)信息:
gitlab-ci-multi-runner unregister --name "名稱"
查看注冊(cè)列表:
gitlab-ci-multi-runner list