How to merge sequences in .gitlab-ci.yml?

Viewed 9392

I am trying to remove some redundancy in setting up several tasks within GitLab CI. As a (greatly) simplified example, I have two jobs involving a call to apt-get update:

job1:
  before_script:
    - apt-get update
    - apt-get install foo

job2:
  before_script:
    - apt-get update
    - apt-get install bar

Not being a big fan of repetitions, I hoped that I would be able to "clean up" the yaml through the help of anchors as follows:

.update: &update
  before_script:
    - apt-get update

job1:
  <<: *update
  before_script:
    - apt-get install foo

job2:
  <<: *update
  before_script:
    - apt-get install bar

However, it appears that the before_script stanzas are not merged with *update. Instead, I find latter one being overwritten. This is in contrast to the GitLab CI docs as well as the example on Wikipedia, though.

Through some experimentation, I managed to get the following to run:

.update: &update apt-get update

job1:
  before_script: 
    - *update
    - apt-get install foo

job2:
  before_script: 
    - *update
    - apt-get install bar

Clearly a step forward. But given that I intend to go for more complex substitutions, this is hardly satisfactory. For reference: This is on GitLab v 8.12.

2 Answers

Gitlab 13.9 added !reference keyword:

.setup:
  before_script: apt-get update

job1:
  before_script: 
    - !reference [.setup, before_script]
    - apt-get install foo

job2:
  before_script: 
    - !reference [.setup, before_script]
    - apt-get install bar

The union of reference and extend is really good and will help you battle redundancy.

Related