5 minute read

In this blogpost I’ll go over a few more advanced topics related to Gitlab-CI and how to structure pipelines and properly reuse them.

Although some of these tips are specific to Gitlab, some can be easily applied to other CI systems which also use YAML for workflow definition.

Using extends

extends is a great way to reuse some YAML parts in multiple places, for example:

    name: centos:latest

  extends: .image_template
    - echo "Testing"

  extends: .image_template
    - echo "Deploying"

We are defining a β€œhidden” key at the top of the file, which we are then reusing across our different stages by making use of the extends functionality. Of course this can be extended to keys/stanzas which are a lot more complex, in order to reduce complexity and make the pipeline definition more readable.

Using include

include just as the name suggests, is used to include other CI YAML file definitions into the current pipeline. The file can either be a local one or a remote one from an another repository.

  • base-testing.yml (file found in the ci-templates repository)
  - test

  ENV: "none"

    - echo "Testing in ${ENV}"
  • .gitlab-ci.yml (file found in our main repository)
include: 'https://my.gitlab/ci-templates/raw/master/base-testing.yml'

  ENV: "dev"

What we will achieve with this, is that we will be able to reuse the base-testing.yml file defined in our central repository of CI templates, and we simply overwrite any variables with their needed value after we include it. Pretty neat, huh?

YAML tips and tricks

Recently I came across this thing called anchors and I think it simplifies many of the common CI/CD YAML sections by a lot, helping with keeping your CI/CD definition DRY(Dont Repeat Yourself). Anchors basically define YAML references that can be used elsewhere.

.job_template: &job_definition  # Hidden key that defines an anchor named 'job_definition'
  image: ruby:2.6
    - postgres
    - redis

  <<: *job_definition           # Merge the contents of the 'job_definition' alias
    - test1 project

  <<: *job_definition           # Merge the contents of the 'job_definition' alias
    - test2 project

You can read more about this on the official gitlab documentation page about CI/CD.

Note for the reader ❕

You can’t use YAML anchors across multiple files when leveraging the include feature. Anchors are only valid within the file they were defined in.


There are many ways through which you can simplify your YAMLs, be it default capabilities of YAML or custom built features offered by the Gitlab CI/CD complex system.

The only golden rule that you should remind yourself at all times, is that most probably there is another way to simply something and make it more readable, so always try to read the docs and code of others in order to find such ways.

And as always, happy learning! πŸ™Œ