Search code examples
gitlabyamlgitlab-cigitlab-ci.yml

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


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


Solution

  • 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