一、前言
沒有使用或不熟悉gitlab的同學,對在gitlab上新建項目、添加yml文件(用於CI/CD控制)這兩個操作會感到茫然,下面我將實現方法作以說明。
二、實現方法
1、在gitlab中新建項目(以空私有項目為例)
添加+ -》 New project -》填寫相關內容 Create Project


2、添加yml文件
選擇項目-》set up CI/CD -》 自己寫或者使用模板 -》commit changes


3、展示流水線(pipeline)的效果:編譯(build)、測試(test,包括test1,test2)、部署(deploy)的流水線
以模板的改寫為例
.gitlab-ci.yml中鍵入
# This file is a template, and might need editing before it works on your project.
# see https://docs.gitlab.com/ce/ci/yaml/README.html for all available options
# you can delete this line if you're not using Docker
image: busybox:latest
before_script:
- echo "Before script section"
- echo "For example you might run an update here or install a build dependency"
- echo "Or perhaps you might print out some debugging details"
- ping -c 5 127.0.0.1
after_script:
- echo "After script section"
- echo "For example you might do some cleanup here"
- ping -c 6 127.0.0.1
build1:
stage: build
script:
- echo "Do your build here"
- ping -c 10 127.0.0.1
test1:
stage: test
script:
- echo "Do a test here"
- echo "For example run a test suite"
- ping -c 11 127.0.0.1
test2:
stage: test
script:
- echo "Do another parallel test here"
- echo "For example run a lint test"
- ping -c 12 127.0.0.1
deploy1:
stage: deploy
script:
- echo "Do your deploy here"
- ping -c 20 127.0.0.1
yml中腳本內容的說明:
(1)一個yml文件,就是一個流水線(pipeline)
(2)該流水線中包含了3個階段(stage,包括build、test、deploy)
(3)每個階段包含了1到2個作業(job)。
build階段:包含build1作業
test階段:包含test1和test2作業(共2個作業)
deploy階段:包含deploy1作業
注意:當只有一個runner時,作業之間的相互關系是串行的,不是並行的。即前面的作為沒有執行完成、或者執行失敗,后面的作業就都不再執行。

(4)關鍵詞作業
before_script關鍵詞作業:會在階段中的每個自定義作業執行前,都重新執行一遍。
after_script關鍵詞作業:會在階段中的每個自定義作業執行后(不論成功還是失敗),都重新執行一遍。
