What is difference between extends and anchor tag (<<: *anchor) in yaml (Gitlab CICD)?
Asked Answered
T

3

5

When we use extends and when we use anchor tag ? Please refer below CI CD pipeline

  stages:
    - stage1
  .random-variables:
    variables:
      ABC: ${XYZ}
    
  .hidden-job: &hidden-job
    stage: stage1
    image: docker:latest
    services:
      - docker:dind
    script:
      #  My Scripts

  hidden-job:dev:
    extends:
      - .random-variables
    <<: *hidden-job
    only:
      - dev

Thanks in advance for clarifying my doubt.

As of now I understand how pipeline is working like anchor tag is use with <<: *alias to pull in the other block of code in current block.

Same extends is use to pull in variables in current block

Transnational answered 4/1, 2023 at 12:0 Comment(1)
One (anchors) is basic YAML syntax, the other (extends) is a specific feature of how GitLab interprets your input.Hooks
M
5

They are essentially the same except you can use a yaml anchor from another file using the extends field. For example:

include:
 - 'https://example.com/some-file.yaml'

# this will work
my_job:
  extends: .some-anchor-from-the-included-file

# this will fail
my_other_job:
  <<: *some-anchor-from-the-included-file

You can also use the !reference tag to pull yaml anchors from other files.

https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#reference-tags

Milliliter answered 22/6, 2023 at 0:26 Comment(1)
Do you mean "except you CANNOT use a yaml anchor from another file" ? Your comment doesn't make sense otherwise...Kami
V
5

One more piece of information could be the deep merge behaviour of the extends keyword.

Example based on this resolved issue: https://gitlab.com/gitlab-org/gitlab/-/issues/36372#note_706349820

.base:
  variables:
    VAR1: hello
  script: exit 0

job:
  extends: .base
  variables:
    VAR2: mello


#THE RESULT IS:
job:
  variables:
    VAR1: hello
    VAR2: mello
  script: exit 0

Please correct me if I am wrong since I didn't tested it personally, but based on my comprehension, with anchors, you would have this behaviour:

.base: &base
  variables:
    VAR1: hello
  script: exit 0

job:
  <<: *base
  variables:
    VAR2: mello


#THE RESULT IS:
job:
  variables:
    VAR2: mello
  script: exit 0

This post makes me confident in what I just write above: Merging Yaml arrarys nested

Veneering answered 18/7, 2023 at 9:52 Comment(1)
I just checked it, you are right! In this case extends keeps both variables and the yaml anchor overwrites the variables field as there is only one element.Etalon
E
1

Extending @Mav3656's correct answer:

If you try something like this:

.base: &base
  variables:
    VAR1: hello
    VAR2: TEST

job:
  extends: .base
  variables:
    VAR1: MELLO1
  script:
    - echo "{$VAR1}, {$VAR2}"

job2:
  <<: *base
  variables:
    VAR1: MELLO2
  script:
    - echo "{$VAR1}, {$VAR2}"

the first job will print {MELLO1}, {TEST} whereas the second job with the yaml anchor overwrites the whole variables field resulting in {MELLO2}, {}, discarding VAR2.

This is shows the differences in the merging strategies of yaml anchors and the extends keyword. The difference does NOT only lie in the fact that extends can be used across different files. The accepted answer is definitely lacking this!

And by the way: changing the values of a job that is imported and has been used via yaml anchors will not change anything. Yaml anchors are resolved on the file level:

File1:

include:
  - "/CICD/test.gitlab-ci.yml"

.wohoo:
  variables:
    VAR: "OVERWRITTEN"

File2:

.wohoo: &yaml
  variables:
    VAR: "HIDDEN"
  script:
    - echo "running template job {$VAR}"

job1:
  <<: *yaml
  variables:
    VAR: "VISIBLE"

In this case the configurations from File1 will be ignored, $VAR will carry the value VISIBLE. If you extends instead of yaml anchors in File2 instead:

File2_NEW:

.wohoo:
  variables:
    VAR: "HIDDEN"
  script:
    - echo "running template job {$VAR}"

job1:
  extends: .wohoo
  variables:
    VAR: "VISIBLE"

Your job1 would be overwritten and the variable would be OVERWRITTEN.

Etalon answered 26/1 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.