gitlab-ci父子include不工作



我试图包含另一个文件与作业(亲子),但它没有做任何事情:

.gitlab-ci。Yml看起来像这样:

workflow:
rules:
- if: $CI_COMMIT_REF_NAME == "master" && $CI_PIPELINE_SOURCE == "schedule"
variables:
CODENAME: all
- when: always
stages:
- stage1
- stage2
job1:
stage: stage1
rules: 
- if: $CI_COMMIT_REF_NAME == "master" && $CI_PIPELINE_SOURCE == "schedule"
- changes: 
- job1/*
variables:
CODENAME: all
trigger: 
include: 
- project: 'project-name'
file: 'job1/pipeline.yml'
job2:
stage: stage2
rules:
- if: $CI_COMMIT_REF_NAME == "master" && $CI_PIPELINE_SOURCE == "schedule"
- changes: 
- job2/*
variables:
CODENAME: bionic
trigger:
include:
- project: 'project-name'
file: 'job2/pipeline.yml'

job1/pipeline.yml:

include:
- project: 'project-name'
file: file.yml
.template: &template
extends: .kaniko-build # extend of the above include
job1-1:
<<: *template
rules: 
- if: '$CODENAME == "xenial" || $CODENAME == "all"'
when: always
- when: never
job1-2:
<<: *template
rules: 
- if: '$CODENAME == "bionic" || $CODENAME == "all"'
when: always
- when: never
job1-3:
<<: *template
rules: 
- if: '$CODENAME == "focal" || $CODENAME == "all"'
when: always
- when: never
job1-4:
<<: *template
rules: 
- if: '$CODENAME == "latest" || $CODENAME == "all"'
when: always
- when: never

job2/pipeline.yml:

include:
- project: 'project-name'
file: 'job1/pipeline.yml'
include:
- project: 'project-name'
file: file.yml

.template-custom-image1: &template-custom-image1
extends: .kaniko-build # extend of the above include
custom-image1-1:
<<: *template-custom-image1
rules: 
- if: '$CODENAME == "xenial" || $CODENAME == "all"'
when: always
- when: never
custom-image1-2:
<<: *template-custom-image1
rules: 
- if: '$CODENAME == "bionic" || $CODENAME == "all"'
when: always
- when: never
custom-image1-3:
<<: *template-custom-image1
rules: 
- if: '$CODENAME == "focal" || $CODENAME == "all"'
when: always
- when: never
custom-image1-4:
<<: *template-custom-image1
rules: 
- if: '$CODENAME == "latest" || $CODENAME == "all"'
when: always
- when: never

当前job2文件夹中的更改正在触发job2,这将触发job2/pipeline。嗯,一切都很好。但是现在我只看到一个job,意思是custom-image1-x. 我也希望看到job1/pipeline。我将job1-x包含在job2/pipeline.yml中:

include:
- project: 'project-name'
file: 'job1/pipeline.yml'

我还要求它们顺序运行,所以我不能使用另一个触发器,因为它们将并行运行。有什么线索或建议吗?也许我看问题的角度不对?

第二:job1/*中的更改应该只触发子管道中stage1的特定作业。但是,job2/*中的更改应该依次触发stage1和stage2(取决于),但要使用.gitlab-ci:

中定义的变量。
job2:
stage: stage2
rules:
- changes: 
- job2/*
variables:
CODENAME: bionic

很抱歉不得不把这个作为一个答案,所以它的格式是正确的:-/

所以我的主要问题是,当stage2发生变化时,我需要额外运行stage1。我已经修复了这个问题,添加了工作流规则,并更改了stage2 (job2/*):

之前我在我的。gitlab-ci中job2:

job2:
stage: stage2
rules:
- if: $CI_COMMIT_REF_NAME == "master" && $CI_PIPELINE_SOURCE == "schedule"
- changes: 
- job2/*
trigger:
include:
- project: 'project-name'
file: 'job2/pipeline.yml'

将触发job2/pipeline.yml:

include:
- project: 'project-name'
file: 'job1/pipeline.yml'
include:
- project: 'project-name'
file: file.yml

.template-custom-image1: &template-custom-image1
extends: .kaniko-build # extend of the above include
custom-image1-1:
<<: *template-custom-image1
custom-image1-2:
<<: *template-custom-image1
custom-image1-3:
<<: *template-custom-image1
custom-image1-4:
<<: *template-custom-image1

在上面你注意到包含了job1/pipeline.yml

include:
- project: 'project-name'
file: 'job1/pipeline.yml'

,我希望它能神奇地执行——没有扩展或任何东西,但它从来没有执行过。我在.gitlab-ci:

中修复了这个问题。
workflow:
rules:
- changes: 
- job2/*
variables: 
SET_STAGE: TWO
CODENAME: bionic
...
job1:
stage: stage1
rules:
- if: $SET_STAGE == "TWO"
- if: $CI_COMMIT_REF_NAME == "master" && $CI_PIPELINE_SOURCE == "schedule"
- changes: 
- job1/*
trigger:
include:
- project: 'project-name'
file: 'job1/pipeline.yml'
strategy: depend
job2:
stage: stage2
rules:
- if: $CI_COMMIT_REF_NAME == "master" && $CI_PIPELINE_SOURCE == "schedule"
- changes: 
- job2/*
trigger:
include:
- project: 'project-name'
file: 'job2/pipeline.yml'
strategy: depend

所以我所做的一切都是在我的。gitlab-ci我定义的工作流规则,将设置一个变量(SET_STAGE)在job2/*的变化,并在我的第一阶段使用该变量。如果设置好了,它也会运行这个阶段。当然,我可以这样解决它:

...
- changes: 
- job1/*
- changes: 
- job2/*
...

而不是:

...
- changes: 
- job1/*
- if: $SET_STAGE == "TWO"
...

但是我需要在某个地方设置一个整体的公共变量(CODENAME),无论如何我在工作流中做的,并将其移动到单独的文件中,从而使我的。gitlab-ci文件稍微苗条一些。

它看起来很简单,但我花了一些时间才弄明白。Simon在第一个帖子评论中提到的矩阵很有趣,但对我的用例来说并不是很有用。所以我保持了它原来的样子,这很好。我在这个线程中解决的另一个问题是"策略:依赖"。所以stage1和stage2不会并行执行。谢谢大家的帮助!: -)

相关内容

  • 没有找到相关文章

最新更新