1.開(kāi)啟設(shè)置gitlab服務(wù)器的CIC功能
2.下載安裝最新版本的gitlab-runner
盡量使用最新版本
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
調(diào)整權(quán)限
sudo chmod +x /usr/local/bin/gitlab-runner
3.配置參數(shù)
參考gitlab管理頁(yè)面的settings中的CICD部分,展開(kāi)runner的設(shè)置說(shuō)明膨处,并重點(diǎn)查看 Set up a specific Runner manually
- 客戶(hù)端配置
gitlab-runner register
#選擇shell作為runner撩炊,其他配置根據(jù)需要靈活調(diào)整
- 客戶(hù)端后配置
由于gitlab-runner每次執(zhí)行CICD的pipeline的時(shí)候都會(huì)clone以下最新的commit,有時(shí)候因?yàn)槟承┪募籨ocker之類(lèi)的工具生成了root才可以刪除的權(quán)限,
因此需要給CICD增加一個(gè)在clone之前清理本地cache文件的功能润绎,這在gitlab的服務(wù)器端是無(wú)法做到的(例如yaml中的before_script:),只能在客戶(hù)端做诞挨。
register之后莉撇,還需要手動(dòng)添加一項(xiàng)git clone的前置工作,在使用命令
gitlab-runner list
之后惶傻,會(huì)有一個(gè)配置文件
ConfigFile=/home/user/.gitlab-runner/config.toml
編輯該文件棍郎,并在[[runner]]的部分加入 pre_clone_script = " cd $CI_PROJECT_DIR/; YOUR_CLEAN_SCRIPT.sh"
編輯好之后大概是這個(gè)樣子
concurrent = 1
check_interval = 0
[[runners]]
name = "test"
url = "https://gitlab.address.work/"
token = "***************"
executor = "shell"
pre_clone_script = " cd $CI_PROJECT_DIR/production; ./clear_docker_cache.sh"
[runners.cache]
4.CICD的yaml配置設(shè)計(jì)
考慮到集成測(cè)試階段的任務(wù)阻塞银室,單元測(cè)試盡量不去卡住state往下運(yùn)行涂佃。 可以給定義明確的stage,并在單元測(cè)試的stage中蜈敢,增加手動(dòng)運(yùn)行的功能辜荠。并在下一階段設(shè)置不論上一步結(jié)果成功失敗,這個(gè)stage都繼續(xù)執(zhí)行抓狭,這樣可以使得單元測(cè)試和模塊測(cè)試不沖突伯病。并最終在模塊測(cè)試之后進(jìn)行deploy等。
when:allow_failure等CICD關(guān)鍵字的解釋
放一個(gè)例子: 其中when: manual 表示只能手動(dòng)觸發(fā)辐宾, when: always表示不論前面結(jié)果如何都繼續(xù)執(zhí)行
# 需要runner在更新之前使用docker清空本地的緩存,否則root的文件git無(wú)法刪除
#以下配置位于 命令:gitlab-runner list獲得的配置文件中
# pre_clone_script = " cd $CI_PROJECT_DIR/production/; ./clear_docker_cache.sh"
stages:
- build
- unittest_server
- unittest_client
- module_test
- deploy
before_script:
- echo 'make some public variables'
build-job:
stage: build
script:
- echo "Hello, $GITLAB_USER_LOGIN!"
dbc__uds_version:
stage: unittest_server
script:
- cd production
- python -m unittest remote_can/dbc_recognization.py
- python -m unittest remote_can/torquemap_uds_version.py
- cd ../
unittest-server:
stage: unittest_server
script:
- echo 'put server unit test here'
- cd production
- ./unittests_all.sh
- cd ../
when: manual
unittest-client:
stage: unittest_client
script:
- echo "pass, server not started"
when: manual
remote_can_server:
stage: module_test
script:
- echo "This job tests remote can server "
- cd production
- ./run_all.sh
- cd ../
when: always
remote_can_client:
stage: module_test
script:
- echo "This job tests something, but takes more time than test-job1."
- cd remote_can_client/
- python -m unittest
- cd ../
when: always
deploy-prod:
stage: deploy
script:
- echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
when: manual