GitLab-CI
GitLab-CI 是一套配合 GitLab 使用的持續(xù)集成系統(tǒng)恋腕。當(dāng)然均驶,還有其它的持續(xù)集成系統(tǒng)脏嚷,同樣可以配合 GitLab 使用赁炎,比如 Jenkins是尖。上一遍文章利用自定義 DSL 簡(jiǎn)化 jenkins 流水線(xiàn)意系,就是利用 Jenkins 配合 GitLab 實(shí)現(xiàn)持續(xù)集成。
與 jenkins pipeline 相比析砸,GitLab-CI 更輕昔字,更方便。它直接通過(guò)簡(jiǎn)單 yaml 文件定義 pipeline首繁,相比與 jenkins 復(fù)雜的 groovy 語(yǔ)法作郭,GitLab-CI 更簡(jiǎn)單。
GitLab-Runner
GitLab-Runner 是配合 GitLab-CI 進(jìn)行使用的弦疮。一般地夹攒,在 GitLab 里面定義一個(gè)屬于這個(gè)工程的軟件集成腳本,而 GitLab-Runner 是用來(lái)自動(dòng)化地完成一些軟件集成腳本工作胁塞,從而實(shí)現(xiàn)持續(xù)集成咏尝。
安裝GitLab-Runner(Mac)
1、GitLab-Runner 安裝
請(qǐng)參考官方文檔:
https://docs.gitlab.com/runner/install/osx.html
2啸罢、GitLab-Runner 注冊(cè)
To register a Runner under macOS:
-
Run the following command:
gitlab-runner register
-
Enter your GitLab instance URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com ) https://gitlab.com
-
Enter the token you obtained to register the Runner:
token 值按照如下流程獲缺嗉臁:
GitLab->Setting->CI/CD->Running->Set up a specific Runner manuallyPlease enter the gitlab-ci token for this runner xxx
-
Enter a description for the Runner, you can change this later in GitLab’s UI:
Please enter the gitlab-ci description for this runner [hostame] my-runner
-
Enter the tags associated with the Runner, you can change this later in GitLab’s UI:
Please enter the gitlab-ci tags for this runner (comma separated): my-tag,another-tag
-
Enter the Runner executor:
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell: docker
-
If you chose Docker as your executor, you’ll be asked for the default image to be used for projects that do not define one in
.gitlab-ci.yml
:Please enter the Docker image (eg. ruby:2.1): python:3.7
3、GitLab-Runner 啟動(dòng)
gitlab-runner install
gitlab-runner start
啟動(dòng)后可以用 gitlab-runner list 查看列表
新建 .gitlab-ci.yml, 觸發(fā) gitlab-ci(以自建項(xiàng)目 poc 為例)
1扰才、GitLab 新建 project 允懂,命名為 poc, 如下
2、新建文件 poc.py ,內(nèi)容如下:
"""
gitlab-ci poc
~~~~~~~~~
This module implements gitlab-ci poc.
:copyright: ? 2019 by the scott.lin..
"""
print("hello gitlab-ci!")
3衩匣、然后在新建文件 .gitlab-ci.yml蕾总,添加如下文件:
實(shí)現(xiàn) build stages, 在 build stages 實(shí)現(xiàn) codecheck job
stages:
- build
codecheck:
stage: build
script:
- pip install pylint
- pylint poc.py
tags:
- python3.7
4琅捏、添加完文件后生百,會(huì)觸發(fā) gitlab-ci,成功情況下輸出:
pipeline 包括 build stage柄延,多個(gè) stage 串行執(zhí)行
build 階段包括 codecheck job蚀浆,多個(gè) job 并行執(zhí)行
會(huì)遇到以下錯(cuò)誤,通過(guò)在 gitlab-runner 配置 extra_hosts 參數(shù),以添加 host 映射
具體操作如下:
1蜡坊、 gitlab-runner list 查看 gitlab-runner 配置文件杠输,如下:
2、config.toml 文件添加如下配置:
更多實(shí)踐錯(cuò)誤參考以下:
http://arder-note.blogspot.com/2017/01/dockergitlab.html
總結(jié)
- stages 是流水線(xiàn)階段秕衙,按照 yml 定義順序執(zhí)行
- 然后在 stages 定義 job蠢甲,比如上面例子中的 codecheck,job 是在同一個(gè) stages 中并行執(zhí)行的
- 更多 yml 定義請(qǐng)參考.gitlab-ci.yml
- 如何整合 kubernetes据忘,待后續(xù)研究鹦牛,kubernetes
- 還有 ChatOps,ChatOps