Append the script on one stage .gitlab-ci.yml

Viewed 5090

How to append the script section in one stage in the .gitlab-ci.yml file?

e.g in this example

stages:
  - stage1_name

.a:
  script:
    - echo "String 1"

.b:
  script:
    - echo "String 2"


stage1_name:
  stage: stage1_name
  extends: .a
  extends: .b
  script:
    - echo "String 3"

how to get as output:

String 1
String 2
String 3

instead of:

String 3
5 Answers

My solution for this was:

stages:
  - stage1_name

.b:
  script:
    - echo "String 2"


stage1_name:
  stage: stage1_name
  before_script:
    - echo "String 1"
  extends: .b
  after_script:
    - echo "String 3"

To not overwrite the script section in stage_1_name I have use before_script and after_script.

You could use YAML anchors like this:

stages:
  - stage1_name

.a: &a
  - echo "String 1"

.b: &b
  - echo "String 2"

stage1_name:
  stage: stage1_name
  script:
    - *a
    - *b
    - echo "String 3"

Gitlab 13.9 introduced a !reference-tag which makes this possible;

.setup:
  script:
    - echo creating environment

test:
  script:
    - !reference [.setup, script]
    - echo running my own command

It is not possible, when you use extends you will overwrite the entire block.

You can use dependencies like the @user3106558 example

I'm not sure about extends usage, but I'm using usually dependencies for such cases.

stages:
   - stage1

script1:
  stage: stage1
  script:
       //doSomething

script2:
  stage: stage1
  dependencies:
    - script1
  script:
      //doSomething

script3:
  stage: stage1
  depencencies:
     - script2
  script:
      //doSomething

this way, script2 will be started only after finishing script1, and script3 - only after second.

Related