GitLab CI — Reusable Job Attributes

Yashwanth Lakkaraju
2 min readJan 24, 2023

--

Pipelines that can happen with !reference

This feature has been introduced way back in 2020, if I recollect it correct, plus it has some great capabilities especially in the area of parallelization.

A detailed CI Guide is available at the Docs

Lets now understand the re-usability..

Problem Statement

Lets say I have the below script and I want to re-use the code than CTRL+C and CTRL+V

stages:
- build
- test

helm-install:
stage: build
image: alpine:latest
script:
- apk add --no-cache git wget curl
- wget -q https://get.helm.sh/helm-v3.9.4-linux-amd64.tar.gz -O - | tar -xzO linux-amd64/helm > /usr/local/bin/helm
- chmod +x /usr/local/bin/helm

helm-test:
stage: test
image: alpine:latest
script:
- helm version --client

Now, installing helm could be done at several places in other jobs, hence “re-usability” is one thing that strikes us.

stages:
- test

.helm-install:
script:
- apk add --no-cache git wget curl
- wget -q https://get.helm.sh/helm-v3.9.4-linux-amd64.tar.gz -O - | tar -xzO linux-amd64/helm > /usr/local/bin/helm
- chmod +x /usr/local/bin/helm

helm-test:
stage: test
image: alpine:latest
extends:
- .helm-install
script:
- helm version --client

The problem here is that the script is overriding the section after importing the job template .helm-install.

Solution

The !reference is at rescue where the job attributes in other jobs, as in - !reference [.helm-install, script] so the CI would like as below

stages:
- test

.helm-install:
script:
- apk add --no-cache git wget curl
- wget -q https://get.helm.sh/helm-v3.9.4-linux-amd64.tar.gz -O - | tar -xzO linux-amd64/helm > /usr/local/bin/helm
- chmod +x /usr/local/bin/helm

helm-test:
stage: test
image: alpine:latest
extends:
- .helm-install
script:
- !reference [.helm-install, script]
- helm version --client

AMAZING, right?

--

--

Yashwanth Lakkaraju
Yashwanth Lakkaraju

Written by Yashwanth Lakkaraju

📌 🇩🇪 • 🇮🇳 ➡️ 🇺🇲 ➡️ 🇩🇪 | Learner | Engineer | Techie | Nethead

No responses yet