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
.