Templates


Make jobs reusable

Templates contain required keywords

Templates must not be well-formed jobs

Job templates begin with a dot to prevent execution

Templates can be located in the same .gitlab-ci.yml (inline)

Templates can be imported using include from…

See also the official development guide for templates


Templates 1/

The following pipeline…

.template:
  image: alpine
  script: pwd

job_name:
  extends: .template

…result in the following job:

job_name:
  image: alpine
  script: pwd

Keywords from job_name are applied after keywords from .template


Templates 2/

The following pipeline…

.template:
  image: alpine
  script: pwd

job_name:
  extends: .template
  script: ls -l

…result in the following job:

job_name:
  image: alpine
  script: ls -l

Keywords from job_name are applied after keywords from .template


Templates 3/3

The following pipeline…

.template:
  image: alpine
  variables:
    foo: bar
  script: pwd

job_name:
  extends: .template
  variables:
    bar: baz

…result in the following job:

job_name:
  image: alpine
  variables:
    foo: bar
    bar: baz
  script: pwd

Variables are merged!


Hands-On

See chapter Templates


Pro tip: Multiple inheritence

Jobs can inherit from multiple templates

job_name:
  extends:
  - .template1
  - .template2

With conflicting templates…

.template1:
  script: pwd
.template2:
  script: whoami

…last writer wins!

job_name:
  script: whoami

But variables are merged!


Pro tip 2: Solve multiple inheritence

Conflicting templates…

.template1:
  script: pwd
.template2:
  script: whoami

…can be resolved by using reference tags

job_name:
  script:
  - !reference[.template1, script]
  - !reference[.template2, script]

Pro tip 3: Public Template Library

Project to help building professional pipelines

Pipeline generator

Documentation

Source code