4 min to read
GitLab CI/CD Template Guide
Optimizing Pipeline Implementation Using GitLab CI Templates

Overview
Learn how to optimize your GitLab CI implementation using templates. Templates help reduce code duplication and maintain consistent pipeline configurations across projects.
Using GitLab CI Templates
Built-in Templates
# Reference GitLab's built-in templates
include:
- template: Terraform/Base.gitlab-ci.yml
- template: Jobs/SAST-IaC.gitlab-ci.yml
Project Templates
If the template is defined in your gitlab’s project, you can write it as follows.
# Reference templates from your project
include:
- project: 'somaz94/server'
ref: master
file: '/template/my_template_file.yml'
- It is irrelevant to define it in
.gitlab-ci.yml
without reference.
🔧 Creating Custom Templates
Build Template Example
Using Build Template
And it can be used as below.
build_manual_image:
<<: *common_job_config
stage: build
image:
name: gcr.io/kaniko-project/executor:v1.22.0-debug
before_script: *common_build_before_script
script: *common_build_script
artifacts:
paths:
- build_status.txt
- service_status.txt
rules:
- if: '($CI_PIPELINE_SOURCE == "web")'
tags:
- build-image
And it can be used for other jobs.
.change_files_game: &change_files_game
changes:
- apps/game/src/**/*
build_auto_image_game:
<<: *common_job_config
stage: build
image:
name: gcr.io/kaniko-project/executor:v1.22.0-debug
before_script: *common_build_before_script
script: *common_build_script
variables:
SERVICE: $GAME_SERVICE
IMAGE_URL: $IMAGE_URL_GAME
IMAGE_URL_LATEST: $IMAGE_URL_LATEST_GAME
artifacts:
paths:
- build_status.txt
- service_status.txt
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
<<: *change_files_game
tags:
- build-image
Slack Notification Template
The template for sending a message to SLACK can also be written as follows. You can receive the result as an artifact for each job and send a message.
.common_update_after_script: &common_update_after_script
- >
if [ "$CI_JOB_STATUS" = "success" ]; then
echo "✅ success" > /builds/somaz94/server/update_status.txt
elif [ "$CI_JOB_STATUS" = "failed" ]; then
echo "❌ failed" > /builds/somaz94/server/update_status.txt
elif [ "$CI_JOB_STATUS" = "canceled" ]; then
echo "⚠️ canceled" > /builds/somaz94/server/update_status.txt
else
echo "🔍 unknown" > /builds/somaz94/server/update_status.txt
...
update_manual_image:
stage: update
image: alpine:latest
interruptible: true
retry:
max: 2 # Maximum of 2 retries
when:
- runner_system_failure
- unknown_failure
before_script: *common_update_before_script
script: *common_update_script
after_script: *common_update_after_script
artifacts:
paths:
- update_status.txt
rules:
- if: $CI_PIPELINE_SOURCE == "web"
tags:
- deploy-image
dependencies:
- build_manual_image
I used the SLACK webhook, and I used the workflow builder to write so that I could only get the result value.
Notification Script Template
Create a Slack notify template.
Using Notification Template
And write the yml file as below.
notify_slack:
stage: notify
image: curlimages/curl:latest
script: *common_notify_slack_script
rules:
- if: $CI_PIPELINE_SOURCE == "web"
- if: $CI_PIPELINE_SOURCE == "trigger"
tags:
- deploy-image
when: always
...
notify_slack_auto_game:
stage: notify
image: curlimages/curl:latest
script: *common_notify_slack_script
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
<<: *change_files_game
tags:
- deploy-image
when: always
dependencies:
- build_auto_image_game
- update_auto_image_game
If used in this way, a template can be created and applied to various jobs as follows.
Comments