Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI forces me to define stages when using multiple include

I have a base file .gitlab-ci.yml:

include:
  - project: 'my-group/my-project'
    file: 'test1.yml'

test1.yml:

stages:
 -test_stage1

test_stage1:
 stage: test_stage1
 script: //some script

It works fine, test_stage1 runs successfully.

Now, if I want to include another file as well:

include:
  - project: 'my-group/my-project'
    file: 'test1.yml'
  - project: 'my-group/my-project'
    file: 'test2.yml'

test2.yml:

stages:
 -test_stage2

test_stage2:
 stage: test_stage2
 script: //some script

I get the following error:

This GitLab CI configuration is invalid: test_stage job: stage parameter should be test_stage2

So I have to add explicitly the stages:

    include:
      - project: 'my-group/my-project'
        file: 'test1.yml'
      - project: 'my-group/my-project'
        file: 'test2.yml'
   stages:
      -test_stage1
      -test_stage2

And it works.
Why is that?
Am I able to somehow include multiple files and go through all of their stages without declaring them?

like image 739
beatrice Avatar asked Aug 01 '26 05:08

beatrice


2 Answers

You need to define your stages so that GitLab CI knows the sequence of stages.

See the documentation.

Am I able to somehow just include multiple files and go through all of their stages without declaring them?

Not if you want to have multiple stages. You need to tell GitLab CI which stage comes first, second etc., so that all the jobs for each stage can be run before going on to the next stage. That gives us a pipeline.

The default stage is test, by the way. If you don't provide the stage attribute for a job, it'll be assigned to the test stage.

like image 191
Aleksey Tsalolikhin Avatar answered Aug 03 '26 07:08

Aleksey Tsalolikhin


I know this is an old question and an answer has already been accepted but there is a solution that I believe achieves what you want.

This issue you are experiencing is the stages declaration in test2.yml is overriding the stages declaration in test1.yml. And when you add a stages declaration to the .gitlab-ci.yml file, it's overriding both. I can't find any documentation to back this up, but in my experience you can only have one 'stages' declaration/directive.

The solution is to use a unique names for the jobs and you can pass an import to your includes to achieve this:

test1.yml:

spec:
  inputs:
    name:
      type: string
---

$[[ inputs.name ]]:test_stage
  stage: test_stage
  script: //some script

test2.yml:

spec:
  inputs:
    name:
      type: string
---

$[[ inputs.name ]]:test_stage
  stage: test_stage
  script: //some script

.gitlab-ci.yml:

include:
  - project: 'my-group/my-project'
    file: 'test1.yml'
    inputs:
      name: 'test1'
  - project: 'my-group/my-project'
    file: 'test2.yml'
    inputs:
      name: 'test2'

stages:
  test_stage
like image 27
Rooster242 Avatar answered Aug 03 '26 09:08

Rooster242