Gitlab CI/CD之.gitlab-ci.yml常用配置说明

这里记录着我在配置.gitlab-ci.yml过程中碰到的常用的几个配置,对其进行分类解释说明,方便后续理解与查阅。

我的一个完整的.gitlab-ci.yml示例:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# default stages: .pre build test deploy .post
# my stages: pre test build deploy post
stages:
- pre # install
- test # lint, sonar_check, unit_test
- build # build, build_with_sentry
- deploy # deploy_testing, deploy_production (upload to cdn or server)
- post # clean if needed

variables:
BUILD_DIR_FOR_TESTING: pre
BUILD_DIR_FOR_PRODUCTION: dist
URL_FOR_TESTING: https://test-demo.example.com/
URL_FOR_PRODUCTION: https://demo.example.com/

cache: &global_cache
untracked: true
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules/
policy: pull-push

.common:
tags:
- gr

############ stage: pre ############

install:
extends: .common
stage: pre
script:
- npm install
cache:
<<: *global_cache
policy: push
rules:
- if: $CI_COMMIT_REF_NAME =~ /^dev$|^master$/
changes:
- package.json
- package-lock.json
- yarn.lock
- if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^dev$|^master$/
changes:
- package.json
- package-lock.json
- yarn.lock

############ stage: test ############

lint:
extends: .common
stage: test
cache:
<<: *global_cache
script:
- echo "==== 代码校验 ===="
- npm run lint
rules:
- if: $CI_COMMIT_REF_NAME =~ /^dev$|^master$/
- if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ /^dev$|^master$/

sonar_check:
extends: .common
stage: test
script:
- echo "==== SONAR ANALYSIS ===="
allow_failure: true
when: manual
only:
- dev
- master

############ stage: build ############

build_testing:
extends: .common
stage: build
cache:
<<: *global_cache
policy: pull
script:
- echo "==== 测试构建 ===="
- npm run pre
artifacts:
name: '$CI_PROJECT_NAME-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHORT_SHA-testing'
expire_in: 1 week
paths:
- $BUILD_DIR_FOR_TESTING/
only:
- dev

build_production:
extends: .common
stage: build
cache:
<<: *global_cache
policy: pull
script:
- echo "==== 生产构建 ===="
- npm run build
artifacts:
name: '$CI_PROJECT_NAME-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHORT_SHA-production'
expire_in: 1 week
paths:
- $BUILD_DIR_FOR_PRODUCTION/
only:
- master

############ stage: deploy ############

deploy_testing:
extends: .common
stage: deploy
script:
- echo "测试环境代码发布"
after_script:
- echo "发布成功:"$URL_FOR_TESTING
environment:
name: testing
url: $URL_FOR_TESTING
when: manual
only:
- dev

deploy_production:
extends: .common
stage: deploy
script:
- echo "生产环境代码发布"
after_script:
- echo "发布成功:"$URL_FOR_PRODUCTION
environment:
name: production
url: $URL_FOR_PRODUCTION
when: manual
only:
- master

Gitlab CI/CD之.gitlab-ci.yml复用类配置

  • Anchors
  • !reference
  • extend
  • include

条件类配置:

  • only
  • except
  • rules
  • when

脚本类配置:

  • before_script
  • script
  • after_script

缓存类配置:

  • cache
  • artifacts
Cleam Lee wechat
欢迎扫一扫订阅!