HMK's blog

保持思考|00后|等待


  • Home
  • Archive
  • Tags
  •   

© 2026 Hekang

Theme Typography by Makito

Proudly published with Hexo

GitLab-Pipeline

Posted at 2026-08-11 devops 

GitLab-Pipeline

一、Pipeline开发工具

  • 流水线视图
  • 流水线编辑器
  • Job作业视图
  • Schedules 定时运行
  • 环境变量

1.1 流水线视图

1.2 Schedule

image-20260708111139221

1.3 环境变量

image-20260708111438670

二、CI/CD设置

  • 全局配置
    • 是否公开访问Pipeline信息;
    • 是否自动取消相同分支已经在构建的Pipeline;
    • 部署Job成功后,跳过其他还未运行的部署作业;
    • ci文件的本地或远程位置;
  • 为项目设置流水线状态标志

skip ci

image-20260708140835204

三、Pipeline语法

3.1 Stages

If stages is not defined in the .gitlab-ci.yml file, the default pipeline stages are:

  • .pre
  • build
  • test
  • deploy
  • .post

image-20260708151441327

设置settiing CI/CD General pipelines - CI/CD configure file

ci/stages.yml

pipeline editor: 编写新的pipeline

顺序运行

修改 gitlab-runner - config.toml 配置

concurrent = 10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
stages:
- .pre
- build
- test
- deploy
- .post

job0:
tags:
- go
stage: .pre
script:
- echo "init"


job1:
tags:
- go
stage: build
script:
- echo "build"

job2-1:
tags:
- go
stage: test
script:
- echo "test1"

job2-2:
tags:
- go
stage: test
script:
- echo "test2"

job3:
tags:
- go
stage: deploy
script:
- echo "deploy"

job4:
tags:
- go
stage: .post
script:
- echo "end"

image-20260708153325243

3.2 variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
variables:
GIT_DEPTH: 0 # 禁用浅克隆,完整拉取仓库
GIT_STRATEGY: clone # 强制完整克隆(而不是 fetch)
GIT_FETCH_EXTRA_FLAGS: "--no-tags" # 解决 fetch 兼容性问题

BUILD_TOOLS: "mvn"
RUNNER_TAG: "go"

stages:
- build

job1:
tags:
- ${RUNNER_TAG}
stage: build
variables:
BUILD_TOOLS: "gradle22255"
script:
- echo $BUILD_TOOLS

3.3 jobs

语法关键字 作用 备注
variables 定义作业中的环境变量
tags 根据标签选择运行作业的构建节点 - 多个标签匹配所有标签构建节点;
- 标签可以使用变量
stage 指定当前作业所属的阶段名称
before_script 作业在运行前执行的shell命令行
script 作业在运行中执行的shell命令行 每个作业至少要包含一个script
after_script 作业在运行后执行的命令行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
stages:
- build

variables:
RUNNER_TAG: "go"
before_script:
- echo "global pipeline before script"
after_script:
- echo "all done"

job1:
tags:
- $RUNNER_TAG
stage: build
before_script:
- echo "before script ready"
script:
- echo "mvn package"
after_script:
- echo "after sciprt done"

job2:
tags:
- $RUNNER_TAG
stage: build
script:
- echo 'job2'

image-20260715114904247

如果job没有 定义before ,after script,会使用全局的

3.3.1 job运行控制 allow_failure和when

关键字 作用 备注
allow_failure 控制作业状态,是否允许作业失败,默认值为false。启用后,如果作业运行失败,该作业将在用户界面中显示橙色警告。 管道认为作业成功/通过,不会别阻塞。假设所有其他作业均成功,则该作业的阶段及其管道将显示相同的橙色警告。但是,关联的提交将被标记为“通过”,而不会发出警告。
when 根据状态控制作业运行,当前面作业成功或者失败时运行。 on_success前面阶段成功时执行(默认值);
on_failure前面阶段失败时运行
always总是执行
manual手动执行
delayed延迟执行
never永不执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
stages:
- build

variables:
RUNNER_TAG: "go"
before_script:
- echo "global pipeline before script"
after_script:
- echo "all done"

job1:
tags:
- $RUNNER_TAG
stage: build
allow_failure: true
before_script:
- echo "before script ready"
script:
- echo "mvn package"
- mvs
after_script:
- echo "after sciprt done"

job2:
tags:
- $RUNNER_TAG
stage: build
script:
- echo 'job2'

image-20260715134727209

image-20260715134816850

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
stages:
- build
- test
variables:
RUNNER_TAG: "go"
before_script:
- echo "global pipeline before script"
after_script:
- echo "all done"

job1:
tags:
- $RUNNER_TAG
stage: build
allow_failure: false
before_script:
- echo "before script ready"
script:
- echo "mvn package"
- mvs
after_script:
- echo "after sciprt done"

job2:
tags:
- $RUNNER_TAG
stage: test
when: on_success
script:
- echo 'job2'

job3:
tags:
- $RUNNER_TAG
stage: test
when: on_failure
script:
- echo 'job3 is running job2 skipped'

image-20260715140135437

manual 必须前面所有的阶段都是success

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
stages:
- build
- test
- deploy
variables:
RUNNER_TAG: "go"
before_script:
- echo "global pipeline before script"
after_script:
- echo "all done"

job1:
tags:
- $RUNNER_TAG
stage: build
allow_failure: true
before_script:
- echo "before script ready"
script:
- echo "mvn package"
- mvs
after_script:
- echo "after sciprt done"

job2:
tags:
- $RUNNER_TAG
stage: test
when: on_success
script:
- echo 'job2 is running'

job3:
tags:
- $RUNNER_TAG
stage: test
when: on_failure
script:
- echo 'job2 is running job3 skipped'
job4:
tags:
- $RUNNER_TAG
stage: deploy
when: manual
script:
- echo 'manual run'

image-20260715141357638

3.3.2 job运行控制retry和timeout

关键字 作用 备注
retry 作业重新运行,遇到错误重新运行的次数 值为整数
等于或大于0,但小于等于2
异常分类
timeout 作业运行超时时间
rules 根据特定的变量或文件变更来控制作业运行; if
changes
exists
needs 作业依赖控制 needs:[“作业名称”]
parallel 生成多个作业,并行运行 parallel:5
值2-50之间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
stages: 
- build
- test
- deploy

variables:
RUNNER_TAG: "go"

job1:
tags:
- $RUNNER_TAG
stage: build
retry: 2
script:
- echo "build task begin"
- mvs

job2:
tags:
- $RUNNER_TAG
stage: test
script:
- echo "test task begin"

image-20260715143130493


配合条件使用,job2 不会retry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
stages: 
- build
- test
- deploy

variables:
RUNNER_TAG: "go"

job1:
tags:
- $RUNNER_TAG
stage: build
retry: 2
script:
- echo "build task begin"
- mvs

job2:
tags:
- $RUNNER_TAG
stage: test
when: on_failure
script:
- echo "test task begin"
- mvs
retry:
max: 2
when: api_failure
# when: script_failure job2 会重试

3.4 rules

GitLab CI 的 rules 是从上到下匹配,匹配到第一个条件就停止,不再继续往后判断。

job1 不会运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
stages:
- build

variables:
RUNNER_TAG: "go"
SKIP_BUILD: "false"

job1:
tags:
- $RUNNER_TAG
stage: build
rules:
- if: $SKIP_BUILD != true
when: never
- when: always
script:
- echo "mvn packages"

job2:
tags:
- $RUNNER_TAG
stage: build
script:
- echo "job2"

运行job1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
stages:
- build

variables:
RUNNER_TAG: "go"
SKIP_BUILD: "false"

job1:
tags:
- $RUNNER_TAG
stage: build
rules:
- if: $SKIP_BUILD == "true"
when: never
- when: always
script:
- echo "mvn packages"

job2:
tags:
- $RUNNER_TAG
stage: build
script:
- echo "job2"

changes 文件更改了触发job

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
stages:
- build
- test

variables:
RUNNER_TAG: "go"
SKIP_BUILD: "false"

job1:
tags:
- $RUNNER_TAG
stage: build
rules:
- if: $SKIP_BUILD != "true"
when: never
- when: always
script:
- echo "mvn packages"

job2:
tags:
- $RUNNER_TAG
stage: test
rules:
- changes:
- README.md #文件更改了触发job
when: always
when: never
script:
- echo "job2"

job3:
tags:
- $RUNNER_TAG
stage: test
script:
- echo "job3"

变更文件触发

image-20260715153833346

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
stages:
- build
- test

variables:
RUNNER_TAG: "go"
SKIP_BUILD: "false"

job1:
tags:
- $RUNNER_TAG
stage: build
rules:
- if: $SKIP_BUILD != "true"
when: never
- when: always
script:
- echo "mvn packages"

job2:
tags:
- $RUNNER_TAG
stage: test
rules:
- changes:
- README.md #文件更改了触发job
when: always
- when: never
script:
- echo "job2"

job3:
tags:
- $RUNNER_TAG
stage: test
rules:
- exists:
- Dockerfile
when: manual
- when: never
script:
- echo "job3"

job4:
tags:
- $RUNNER_TAG
stage: test
script:
- echo "job4"
# 只有job4运行

增加Dockerfile

手动运行job3

image-20260715155130366

3.5 needs

job2 执行完 执行job3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
stages:
- build
- test
- deploy

variables:
RUNNER_TAG: "go"

job1:
tags:
- $RUNNER_TAG
stage: build
rules:
script:
- echo "mvn packages2"
- sleep 10s

job2:
tags:
- $RUNNER_TAG
stage: build

script:
- echo "build "

job3:
tags:
- $RUNNER_TAG
stage: test
script:
- echo "test1"

job4:
tags:
- $RUNNER_TAG
stage: test
needs: ["job2"]
script:
- echo "test234"

并行执行某一个job

1
2
3
4
5
6
7
job2:
tags:
- $RUNNER_TAG
stage: build
parallel: 5
script:
- echo "build "

image-20260809131557243

3.6 Pipeline运行控制

workflow 控制流水线

job1 未运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
variables:
SKIP_RUN: 'true'
RUNNER_TAG: 'go'

workflow:
rules:
- if: $SKIP_RUN == "true"
when: never
- when: always
stages:
- build

job1:
tags:
- $RUNNER_TAG
stage: build
script:
- echo "build 1224"

提交跳过Pipeline触发 [skip ci]

trigger触发Pipeline运行

1
2
3
4
5
6
trigger:
project: mygroup/myproject


trigger:
branch: dev
1
2
3
4
5
6
7
stages:
- deploy
trigger-cd-pipeline:
stage: deploy
trigger:
include: ci/needs.yml

1
2
3
4
5
6
7
8
9
10
stages:
- deploy

trigger-project-pipeline:
stage: deploy
trigger:
include:
- project: Administrator/demotest
ref: "main"
file: "ci/needs.yml"

image-20260809135725711

API触发Pipeline

​ Pipeline trigger tokens

​ Add new pipeline trigger token

1
glptt-xauekxkMkLxx5Asf75Gy
Use cURL
1
2
3
4
5
curl -X POST \
--fail \
-F token=glptt-xauekxkMkLxx5Asf75Gy \
-F ref=main \
http://192.168.11.200/api/v4/projects/1/trigger/pipeline
Use .gitlab-ci.yml
1
2
script:
- "curl -X POST --fail -F token=TOKEN -F ref=REF_NAME http://192.168.11.200/api/v4/projects/1/trigger/pipeline"
Use webhook (POST requests only)
1
http://192.168.11.200/api/v4/projects/1/ref/REF_NAME/trigger/pipeline?token=TOKEN
Pass job variables

To pass variables to the triggered pipeline, add variables[VARIABLE]=VALUE to the API request.

cURL:

1
2
3
4
5
6
curl -X POST \
--fail \
-F token=TOKEN \
-F "ref=REF_NAME" \
-F "variables[RUN_NIGHTLY_BUILD]=true" \
http://192.168.11.200/api/v4/projects/1/trigger/pipeline

Webhook (POST requests only):

1
http://192.168.11.200/api/v4/projects/1/ref/REF_NAME/trigger/pipeline?token=TOKEN&variables[RUN_NIGHTLY_BUILD]=true
1
2
3
4
5
6
7
root@gitlab:~# curl -X POST \
--fail \
-F token=glptt-xauekxkMkLxx5Asf75Gy \
-F ref=main \
http://192.168.11.200/api/v4/projects/1/trigger/pipeline
{"id":72,"iid":42,"project_id":1,"sha":"9206191fc5700fd66c2b1c2adbe9c5f197a8311f","ref":"main","status":"created","source":"trigger","created_at":"2026-08-09T13:06:29.034Z","updated_at":"2026-08-09T13:06:29.034Z","web_url":"http://192.168.11.200/root/demotest/-/pipelines/72","before_sha":"0000000000000000000000000000000000000000","tag":false,"yaml_errors":null,"user":{"id":1,"username":"root","public_email":null,"name":"Administrator","state":"active","locked":false,"avatar_url":"https://www.gravatar.com/avatar/26e1cb973e52bebccd12e7a42a7c12bc36eaa71fb0dd2f3dd12e4642fac9bb5a?s=80\u0026d=identicon","web_url":"http://192.168.11.200/root"},"started_at":null,"finished_at":null,"committed_at":null,"duration":null,"queued_duration":null,"coverage":null,"detailed_status":{"icon":"status_created","text":"Created","label":"created","group":"created","tooltip":"created","has_details":true,"details_path":"/root/demotest/-/pipelines/72","illustration":null,"favicon":"/assets/ci_favicons/favicon_status_created-4b975aa976d24e5a3ea7cd9a5713e6ce2cd9afd08b910415e96675de35f64955.png"},"archived":false}root@gitlab:~#

3.8 Pipeline Template

  • extend
  • include
1
2
3
4
5
6
7
8
9
10
新建项目: devops/devops-ci-lib
ci.yaml
.build:
tags:
- go
stage: build
variables:
BUILD_TOOLS: "maven 3"
script:
- echo "$BUILD_TOOLS"

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
stages:
- build
- test

include:
- project: 'devops/devops-ci-lib'
ref: main
file: '/CI/ci.yaml'


build:
extends: .build
test1:
tags:
- "go"
stage: test
script:
- echo "test"

image-20260809214813706

3.9 配置提交阶段流水线

  • 提交代码后自动触发Pipeline运行

初始化一个spring 项目

image-20260810093623455

推送至仓库

1
2
3
4
5
6
cd /tmp
git init
git remote add origin http://192.168.11.101/devops/devops-maven-service.git
git add .
git commit -m "Initial commit"
git push -uf origin master
1
2
3
4
5
6
7
8
9
.build:
tags:
- go
stage: build
variables:
BUILD_TOOL: "maven 3"
script:
- $BUILD_SHELL

安装maven 安装过java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@jenkins gitlab-runner]# wget https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
# 解压
tar -zxvf apache-maven-3.6.3-bin.tar.gz

# 重命名(可选)
mv apache-maven-3.6.3 maven-3.6.3

# 创建软链接(便于版本管理)
ln -s /usr/local/maven-3.6.3 /usr/local/maven
# 解压
tar -zxvf apache-maven-3.6.3-bin.tar.gz

# 重命名(可选)
mv apache-maven-3.6.3 maven-3.6.3

# 创建软链接(便于版本管理)
ln -s /usr/local/maven-3.6.3 /usr/local/maven



wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
tar -zxvf jdk-21_linux-x64_bin.tar.gz
mv jdk-21.0.12/ jdk21

sudo tee /etc/profile.d/java21.sh <<'EOF'
export JAVA_HOME=/usr/local/jdk21
export PATH=$JAVA_HOME/bin:$PATH
EOF
source /etc/profile.d/java21.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
include:
- project: 'devops/devops-ci-lib'
ref: main
file: 'CI/ci.yaml'

stages:
- build
- test

variables:
GIT_CHECKOUT: "false"
RUNNER_TAG: "go"
# 下载代码
checkout:
tags:
- $RUNNER_TAG
stage: .pre
variables:
GIT_CHECKOUT: "true"
script:
echo "pull code"
# 构建
build:
tags:
- $RUNNER_TAG
extends: .build
variables:
BUILD_SHELL: "mvn clean package -DskipTests"

image-20260810143028477

image-20260810155557632

Share 

 Previous post: CephX 认证机制 Next post: Ceph数据冷热分离 

© 2026 Hekang

Theme Typography by Makito

Proudly published with Hexo