gitlab-ci持續集成主要包括兩方面:gitlab、runner。gitlab負責代碼管理,runner負責版本管理、編譯、推送。公司已有gitlab服務,現在只需要創建runner與gitlab對接即可。
- 下載ubuntu Gitlab-Runner安裝包
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
- 安裝gitlab-ci-multi-runner
sudo apt-get install gitlab-ci-multi-runner
- 安裝完成後需要查看安裝是否成功,執行:
sudo gitlab-runner status
如果顯示gitlab-runner: Service is running!即爲安裝成功,此時會在/home目錄下生成gitlab-runner文件夾。這裏是最令人無語的地方,gitlab-runner創建了一個新用戶,gitlab-runner只是普通
sudo usermod -aG sudo gitlab-runner
- 註冊runner並和對應的工程關聯起來,我們需要用到工程的token值,所以在這裏我們先在gitLab上創建工程。在項目的settings下的CI/CD下找到token,然後在本地執行註冊
sudo gitlab-runner register
接下來需要填寫一些信息:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
URL
Please enter the gitlab-ci token for this runner:
token
Please enter the gitlab-ci description for this runner:
[DESKTOP-2P9GHDD]: gitbook publish
Please enter the gitlab-ci tags for this runner (comma separated):
master
Registering runner... succeeded runner=avuSXASJ
Please enter the executor: docker-ssh, parallels, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, docker:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
- 在本地倉庫中創建隱藏文件.gitlab-ci.yml,內容爲:
build:
stage: build
script:
- pwd
- tree
- gitbook init
- python3 summarybuilder.py
- gitbook install
- gitbook build
- ./scpf.sh
summarybuilder.py,./scpf.sh爲自己根據需要添加的腳本,在本地倉庫創建隱藏文件.gitignore,內容爲:
**/_book
**/node_modules
**/*.pyc
*.swp
-
添加代碼並push之後在jobs選項卡下就能看到編譯結果,點擊pass能看到控制檯編輯結果
-
啓動gitbook。在gitlab該項目的piplines中,我們找到gitbook的輸出目錄,即pwd命令所打印的目錄,如/home/gitlab-runner/builds/00bbd75f/0/book/overview。登錄centos後使用gitlab-runner用戶,切換到上述路徑後,執行:
nohup gitbook serve --port 15016 &
即完成了gitbook的啓動(端口自己指定,默認4000),而且當gitbook發生變動後,他會自動發佈變動。
參考
- https://www.cnblogs.com/fithon/p/6645020.html
- https://www.lefer.cn/posts/53574/
- https://www.lefer.cn/posts/25801/
- https://scarletsky.github.io/2016/07/29/use-gitlab-ci-for-continuous-integration/
- 實在不行就通過scp實現編譯文件的傳遞,https://www.cnblogs.com/wanzaiyimeng/p/6700138.html
- docker,https://www.zoulei.net/2017/12/25/GitLabCICD_quickstart/
- 摺疊目錄,http://gitbook.zhangjikai.com/plugins.html#expandable-chapters-small