Preserve YAML anchors across includes in CI configuration

Description

GitLab 10.5 added the include keyword for including external YAML files in .gitlab-ci.yml files. This can reduce repetition by allowing common jobs and other keywords to be shared across multiple repositories. However, it can be difficult to refactor jobs that have small customizations in different repositories.

In my repositories, I commonly have bundle jobs that run docker-compose config to produce a stable Compose file that will be used for various deployments. The bundle share a common base that is defined as a YAML anchor. He's a pared down example.

.bundle_job: &bundle_job
  image:
    name: docker/compose:1.19.0
    entrypoint: [""]
  services:
    - name: docker:stable-dind
  script:
    - docker-compose config --resolve-image-digests > $STACK_NAME.yml
  artifacts:
    name: ${STACK_NAME}-stack
    paths:
      - $STACK_NAME.yml

Concrete jobs (without a leading .) then alias bundle_job to set custom variables or artifacts. Here is an example.

bundle:onboard:
  <<: *bundle_job
  variables:
    EXTERNAL_TRAEFIK_NETWORK: 'true'
    STACK_NAME: $CI_PROJECT_NAME-onboard
    ENVIRONMENT_HOST: $CI_PROJECT_NAME-onboard.$AUTO_DEVOPS_DOMAIN

Proposal

I would like to define .bundle_job and the &bundle_job anchor in an include file, and use the &bundle_job anchor as an alias in .gitlab-ci.yml. So, basically something like the following.

bundle.yml

.bundle_job: &bundle_job
  image:
    name: docker/compose:1.19.0
    entrypoint: [""]
  services:
    - name: docker:stable-dind
  script:
    - docker-compose config --resolve-image-digests > $STACK_NAME.yml
  artifacts:
    name: ${STACK_NAME}-stack
    paths:
      - $STACK_NAME.yml

.gitlab-ci.yml

include: 'https://gitlab-host/repository/bundle.yml'

bundle:onboard:
  <<: *bundle_job
  variables:
    EXTERNAL_TRAEFIK_NETWORK: 'true'
    STACK_NAME: $CI_PROJECT_NAME-onboard
    ENVIRONMENT_HOST: $CI_PROJECT_NAME-onboard.$AUTO_DEVOPS_DOMAIN

I tried this in GitLab 10.6 and got a `yaml invalid' error on the pipeline that says, “Unknown alias: bundle_job.”

Links / references