Gitlab CI: How to reuse your .yml code

Giovanni Rossini
2 min readNov 9, 2018

When doing new studies on Gitlab Continuous Integration, I discovered two things that can help a lot in the development of pipelines: Hidden Keys and Anchors.

Hidden keys and anchors are great features when it comes to reusing code. With them, it is possible to reference the same block of code countless times, without the traditional copy and paste.

Let’s start with a practical example:

stages:
- deploy
- production
.template: &template
stage: deploy
cache:
key: site-package
policy: push
paths:
- ./build/project
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
expire_in: 2hr20m
paths:
- ./build/project/
script:
- npm install
- npm run build
- npm run start
deploy to develop:
<<: *template
deploy to production:
<<: *template
stage: production
cache:
key: site-package
policy: pull
script:
- npm run start

A lot is going on, so let’s break it down part by piece:

Each job definition that begins with a period is ignored. Therefore, .template does not define active and executable tasks. This feature is called hidden keys.
YAML allows you to reference other parts of yourself to avoid copying and pasting, thereby reducing the code. This resource is called “anchor”. In short:

.template: &template 
Where &template is an alias we gave to the .template job, which in turn will not run.

The &template is the “copy” and the *template is the “paste”. So:

deploy to develop: 
<<: *template
Where <<: *template will bring everything that was defined in the hidden job .template.The code would look like this:deploy to develop:
stage: deploy
cache:
key: site-package
policy: push
paths:
- ./build/project
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
expire_in: 2hr20m
paths:
- ./build/project/
script:
- npm install
- npm run build
- npm run start

When calling “<<: *template” we are saying “do everything the job template does”. If you want to override some keys present in the template, you need to re-declare them.

So in Production’s case, the code would look like this:

# With anchor
deploy to production:
<<: *template
stage: production
cache:
key: site-package
policy: pull
script:
- npm run start
# Without anchor
deploy to production:
stage: production
cache:
key: site-package
policy: pull
paths:
- ./build/project
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
expire_in: 2hr20m
paths:
- ./build/project/
script:
- npm run start

Preview:

Gitlab: CI Lint

Therefore, use as much as possible hidden keys and anchors. Every code is required to have a hidden key? No. This is not mandatory, it depends on each case and on how your DevOps cycle is. But in almost every pipeline you can dock an anchor to help clean your code and improve readability.

--

--