概念
Gitlab上的CI基于事后模式,也就是說在代碼合入主干之后,進行編譯、測試。在這點上,和gerrit的模式完全不同。
實現(xiàn)
Gitlab自己已經(jīng)集成CI、Issue管理等功能了,所以不需要額外安裝其他服務(wù)。
Gitlab上的job配置,是以文件方式保存在項目的根目錄下面,文件名是.gitlab-ci.yml
runner就是執(zhí)行編譯或者測試的目標(biāo)機器,runner分兩種:
-
Shared runners:gilab上所有項目都可以用 -
Specific runners:按照項目配置,只用在一個特定的項目上。
步驟
- 創(chuàng)建一個代碼庫
- 編寫測試相關(guān)的文件
- 編寫文件
.gitlab-ci.yml
下面是一個簡單的例子:
- 編寫文件
job1:
tags:
- linsee
script: "./execute-script-for-job1"
這個測試任務(wù)名字是job1,在標(biāo)簽為linsee的機器上執(zhí)行,執(zhí)行時使用的腳本是:項目根目錄下,名字為execute-script-for-job1的腳本文件
- 在根目錄下創(chuàng)建
execute-script-for-job1腳本文件
g++ main.cpp
我們只做簡單的編譯測試
上傳這兩個文件,并且包括測試文件
main.cpp-
配置
Specific Runners- 在你的目標(biāo)機器上,下載文件
wget https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-ci-multi-runner-linux-amd64
- 配置
執(zhí)行
./gitlab-ci-multi-runner-linux-amd64 register
按提示輸入相應(yīng)信息(可以在右上角配置 -> Runner里面獲?。?/p>
...
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
https://這里是你的gitlab的url/ci
Please enter the gitlab-ci token for this runner:
在這里輸入你的token
Please enter the gitlab-ci description for this runner:
[xxxxx]:
Please enter the gitlab-ci tags for this runner (comma separated):
linsee
- 運行
./gitlab-ci-multi-runner-linux-amd64 run
這時候,在右上角配置 -> Runner -> Specific runners下面就可以看到你剛注冊上來的機器了。
上面有name和tag,如下:

Paste_Image.png
- 修改main.cpp,并推送到gitlab上,就會自動觸發(fā)CI了。
CI的執(zhí)行結(jié)果,可以在Pipelines里面看到,當(dāng)然在這里你也可以重新觸發(fā)(執(zhí)行不穩(wěn)定,或者調(diào)試環(huán)境時用)

Paste_Image.png