我配置了以下
.gitlab-cci.yml
文件:
image: node:latest
stages:
- test
- node
- prepare
- build
- run
node_18:
stage: node
image: node:18.20.2
script:
- echo "Testing Node.js version 18.20.2"
- npm i
- cp config.example.json config.json
- rm config.json
node_20:
stage: node
image: node:20.13.1
script:
- echo "Testing Node.js version 20.13.1"
- npm i
- cp config.example.json config.json
- rm config.json
node_22:
stage: node
image: node:22.2.0
script:
- echo "Testing Node.js version 22.2.0"
- npm i
- cp config.example.json config.json
- rm config.json
prepare:
stage: prepare
script:
- echo "Removing node_modules..."
- rm -rf node_modules/
only:
- master
- merge_requests
compile:
stage: build
image: node:20
script:
- npm i
- cp config.example.json config.json
- node deploy-commands.js
- npm run pm2start
- npm run pm2stop
only:
- master
- merge_requests
testrun:
stage: run
script:
- npm run test
only:
- master
- merge_requests
include:
- template: Security/Secret-Detection.gitlab-ci.yml
现在,每当我对任何非主分支进行正常提交时,都会创建一个运行两个阶段(测试和节点)的管道。 然而,当我创建 PR/MR 时,它会创建两个管道,一个是我提到的这两个阶段,另一个是仅标记为在合并请求上运行并提交到 master 的其他阶段。 现在,这是一个问题,因为如果之前启动的管道失败,如果之后启动的管道全部成功,则 MR 将能够被合并(这是一个问题,因为我对所有提交进行秘密检测,所以如果有一个秘密,它将被可能成功的新管道覆盖)。 我怎样才能做到这一点,以便所有这些都在共同事件的一个管道内运行(合并请求并提交给主控)。
如果我让所有阶段始终运行,那么它们都在一个管道中运行,但是让它们像这样运行会将它们分成两个管道,这也会减慢一切,因为我只有两个运行器。
我已经使用示例配置更新了您的 CI 设置,您应该能够适应您的用例。请记住,最好使用
rules
而不是 only/except
,因为后者已被弃用。
image: node:latest
stages:
- test
- node
- prepare
- build
- run
node_18:
stage: node
image: node:18.20.2
rules:
- if $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- echo "Testing Node.js version 18.20.2"
- npm i
- cp config.example.json config.json
- rm config.json
node_20:
stage: node
image: node:20.13.1
rules: !reference [.node_18, rules]
script:
- echo "Testing Node.js version 20.13.1"
- npm i
- cp config.example.json config.json
- rm config.json
node_22:
stage: node
image: node:22.2.0
rules: !reference [.node_18, rules]
script:
- echo "Testing Node.js version 22.2.0"
- npm i
- cp config.example.json config.json
- rm config.json
prepare:
stage: prepare
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"
script:
- echo "Removing node_modules..."
- rm -rf node_modules/
compile:
stage: build
image: node:20
rules: !reference [prepare, rules]
script:
- npm i
- cp config.example.json config.json
- node deploy-commands.js
- npm run pm2start
- npm run pm2stop
testrun:
stage: run
rules: !reference [prepare, rules]
script:
- npm run test
include:
- template: Security/Secret-Detection.gitlab-ci.yml
此处的
reference
关键字是为了重用配置,这样您就不会在文件中复制粘贴配置,类似于 YAML 锚点的做法。
通过该设置,将出现两种情况: