gitlab ci/cd管道运行/按顺序扩展其他作业
因此,我有一个由特定规则触发的作业 - 创建一个新的标签app-prod-1.0.0
或app-dev-1.0.0.0
。每当创建新标签时,我都会调用该作业,以回报扩展了其他作业,
image: node:lts-alpine
stages:
- install
- build
- deploy
.install-packages:
stage: install
script:
- echo "INSTALL-PACKAGES"
- yarn install --cache-folder .yarn-cache
artifacts:
paths:
- node_modules
cache:
- key:
files:
- yarn.lock
paths:
- .yarn-cache/
.build-project:
stage: build
script:
- echo "BUILD-PROJECT"
- echo $ENVIRONMENT
- yarn build
artifacts:
paths:
- build
.deploy-project:
stage: deploy
script:
- echo "DEPLOY-PROJECT"
- ls -la build
build_prod:
variables:
PACKAGE: '/app/prod'
ENVIRONMENT: 'prod'
extends:
- .install-packages
- .build-project
- .deploy-project
rules:
- if: '$CI_COMMIT_TAG =~ /^app-prod-[0-9]+\.[0-9]+\.[0-9]+$/'
build_dev:
variables:
PACKAGE: '/app/dev'
ENVIRONMENT: 'dev'
extends:
- .install-packages
- .build-project
- .deploy-project
rules:
- if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'
我的想法是将作业以我在作业中描述的顺序调用:.install-packages
,。构建项目
,.deploy-project
。但这没有发生,似乎它只是跳到最后一个作业.deploy-project
,而无需安装和构建,从而破坏了我的管道。 如何顺序运行/扩展作业?
So I have a job which is triggered with specific rules - creating a new tag app-prod-1.0.0
or app-dev-1.0.0
. Whenever new tag is created I call the job, which in return extends other jobs
image: node:lts-alpine
stages:
- install
- build
- deploy
.install-packages:
stage: install
script:
- echo "INSTALL-PACKAGES"
- yarn install --cache-folder .yarn-cache
artifacts:
paths:
- node_modules
cache:
- key:
files:
- yarn.lock
paths:
- .yarn-cache/
.build-project:
stage: build
script:
- echo "BUILD-PROJECT"
- echo $ENVIRONMENT
- yarn build
artifacts:
paths:
- build
.deploy-project:
stage: deploy
script:
- echo "DEPLOY-PROJECT"
- ls -la build
build_prod:
variables:
PACKAGE: '/app/prod'
ENVIRONMENT: 'prod'
extends:
- .install-packages
- .build-project
- .deploy-project
rules:
- if: '$CI_COMMIT_TAG =~ /^app-prod-[0-9]+\.[0-9]+\.[0-9]+$/'
build_dev:
variables:
PACKAGE: '/app/dev'
ENVIRONMENT: 'dev'
extends:
- .install-packages
- .build-project
- .deploy-project
rules:
- if: '$CI_COMMIT_TAG =~ /^app-dev-[0-9]+\.[0-9]+\.[0-9]+$/'
My thought was that jobs will be called in the order I have described inside the job: .install-packages
, .build-project
, .deploy-project
. But that's not happening it seems that it just jumps to the last job .deploy-project
, without installing and building and thus breaking my pipeline.
How to run/extend jobs in sequence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我在GitLab的工作中没有使用多个扩展的行为。
Gitlab,试图从父级合并代码。
现在,您的所有父作业都定义了脚本标签,并且在您的作业中,例如build_prod,在以下的顺序中进行的扩展
是从.Deploy-Project中的脚本代码覆盖另一个作业的脚本标签。
它在变量方面有所不同。如果使用相同的变量,它将合并所有变量并覆盖。
请参阅您自己的示例更新带有变量的示例。
现在,请注意如何用“/app/prod”的最终值覆盖包装变量,该值来自构建生产作业本身。同时,将来自单个家长作业的其他变量合并到下面的情况下,
我真的发现 view合并yaml 功能最好了解如何评估我的YML文件。
它可在CI/CD-&GT中使用。编辑
This is the behaviour for which I didn't use multiple extends so far in my work with GitLab.
GitLab, attempts to merge the code from parent job.
Now all your parent jobs defines the script tag and in your job for e.g. build_prod the extends happening in below order
the script code from .deploy-project is overwriting the other job's script tag.
It works differently for the variables. It will merge all the variables and overwrites if same variable is used.
See your own example updated with variables.
And now notice how PACKAGE variable is overwritten with the final value of '/app/prod' which comes from build-prod job itself. At the same time other variables from individual parent jobs are merged to look like below
I really found View merged YAML feature best to understand how my yml file will be evaluated.
Its available in CI/CD -> Editor
它实际上不是“跳到最后的作业”,而是仅执行您提供的单个作业 - 即
build_prod
或build> build_dev
,具体取决于提交标签。按 docs ,您基本上只是合并了您指定的所有模板作业中的所有内容,因此最后一个
stage
关键字来自.deploy-project
模板作业胜利。您应该在每个阶段分别指定每个作业,甚至可能将您的
规则
放入单独的模板作业,即您应该为
prod> prod
env创建类似的作业,定义模板作业。产品,并创建安装程序,构建 - 构建,部署生产作业It's not actually "jumps to the last job", but simply executes a single job you have provided - that is
build_prod
orbuild_dev
, depending on commit tag.As per docs when you are calling
extends
, you are basically just merging everything inside all the template jobs that you specified, so the laststage
keyword, which comes from.deploy-project
template job wins.You should specify each job separately for each stage, and maybe even put your
rules
to a separate template job, i.e.You should create similar jobs for
prod
env, define template job .prod, and create install-prod, build-prod, deploy-prod jobs