轉載請注明出處:https://www.cnblogs.com/shining5/p/8863063.html
部署GitLab-CI
簡介
GitLab_CI(gitlab continuous integration)是Gitlab提供的持續集成服務。主要功能是每一次push到gitlab的時候,觸發一次腳本執行,腳本內容包括編譯、測試、部署等自定義內容。

持續集成在自動化工作流程中扮演着十分重要的角色,GitLab內置持續集成,持續部署,持續交付以支持編譯,測試和部署應用程序
本文主要利用GitLab-CI實現自動部署
原理
自動部署涉及了若干個角色,主要介紹如下:
-
GitLab-CI
GitLab自帶的持續集成系統,你裝的GitLab的那台服務器上就有,無需自行安裝。GitLab-CI負責解析.gitlab-ci.yml
-
.gitlab-ci.yml
GitLab-CI使用YAML文件來管理項目配置,在git項目的根目錄下創建此文件,文件中包含一系列的階段和執行規則。GitLab-CI在push后解析它,根據里面的內容調用runner來執行。YAML配置語法
-
GitLab-Runner
這個是腳本執行的承載者, .gitlab-ci.yml的script部分就是由runner來負責的。GitLab-CI解析項目里的.gitlab-ci.yml文件之后,根據里面的規則,分配到各個Runner來運行相應的腳本script。

步驟
-
安裝GitLab-CI
GitLab自帶,無需單獨安裝
-
安裝GitLab-Runner
在centOS系統安裝gitlab-ci-multi-runner,在其它系統安裝runner,請參考安裝GitLab Runner
添加GitLab官方倉庫
# For RHEL/CentOS/Fedora
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
安裝最新版本的GitLab Runner
# For RHEL/CentOS/Fedora
sudo yum install gitlab-ci-multi-runner
-
向GitLab-CI注冊Runner,將Runner與git項目綁定起來
- 先介紹下如何獲取項目token,因為注冊Runner時會用到,git項目–settings–CI/CD–General pipelines settings–Runner token,如圖

- 注冊Runner
sudo gitlab-ci-multi-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://git.intra.weibo.com
Please enter the gitlab-ci token for this runner:
***(獲取git項目token值)
Please enter the gitlab-ci description for this runner:
Please enter the gitlab-ci tags for this runner (comma separated):
tag名字(很重要,在.gitlab-ci.yml文件里面指定tag,匹配使用哪個tag的runner)
Whether to run untagged builds [true/false]:
Whether to lock the Runner to current project [true/false]:
Registering runner... succeeded runner=m15ahKPr
Please enter the executor: docker-ssh, shell, ssh, virtualbox, kubernetes, docker, parallels, docker+machine, docker-ssh+machine:
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 status/gitlab-runner status查看runner運行狀態,也可以在git項目中查看或編輯runner狀態:Settiongs-CI/CD-Runner settings

-
編寫.gitlab-ci.yml
git項目根目錄下編寫.gitlab-ci.ymlstages: -deploy deploy: stage: deploy script: - deploy.sh only: - master tags: - runner26這里我們只有一個stage:deploy,only指定了只有在master分支push的時候才會被執行,tags是runner26,此tag對應剛才注冊runner時候的tags
最重要的script部分:deploy.sh 這里是一條shell命令,deploy.sh是runner服務器上編寫的一個腳本,runner執行時執行此腳本
.gitlab-ci.yml配置請參考官方文檔
添加徽章
步驟:Settings–>CI/CD–>General pipelines settings–>Pipeline status
將[](http://git.intra.weibo.com/dorylus_qa/CITest/commits/master)添加到README.md中
遇到問題及解決辦法
-
執行gitlab-ci時,有時會報
user gitlab-runner does not exist或者/home/gitlab-runner:permission denied-
解決辦法有兩個:
- user add gitlab-runner添加此用戶,chown -R gitlab-runner:gitlab-runner /home/gitlab-runner
-
修改gitlab-ci-multi-runner默認啟動參數,默認–working-directory /home/gitlab-runner,–user gitlab-runner,我們可以修改成自己的用戶名及工作目錄並賦予權限。
1. kill掉當前gitlab-ci-multi-runner服務 2. gitlab-ci-multi-runn uninstall 3. Gitlab-ci-multi-runner install —working-directory /data0/gitlab-runner --user tr1 4. chown -R tr1:tr1 data0/gitlab-runner 5. 重啟服務/usr/bin/gitlab-ci-multi-runner run --working-directory /data0/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-run --syslog --user tr1
-
-
項目執行ci時會自動將項目文件下載到runner所在的機器,那如何找到呢?
- 路徑為–working-directory指定的路徑,如/home/gitlab-runner/builds下,但下面會有一串字符命名的文件夾,那哪個是我們要找的目錄呢?
- 字符串命名的文件夾與runner中的字符串對應

- 字符串命名的文件夾與runner中的字符串對應
- 路徑為–working-directory指定的路徑,如/home/gitlab-runner/builds下,但下面會有一串字符命名的文件夾,那哪個是我們要找的目錄呢?
-
gitlab-runner如何並發執行任務
- 默認並發數為1,修改 /etc/gitlab-runner/config.toml中的concurrent的配置即可,然后重啟服務
-
runner執行以docker方式,那如何設置數據映射呢?
- 仍然是修改 /etc/gitlab-runner/config.toml中的配置,找到你注冊的runner,修改volumes即可
volumes = ["/data0/host:/data0/container:rw","/cache"]其中/data0/host為宿主機地址,/data0/container容器地址,rw設置讀寫權限
- 仍然是修改 /etc/gitlab-runner/config.toml中的配置,找到你注冊的runner,修改volumes即可
