A Singapore Government Agency Website

Customising GitLab Pipelines

CStack’s default CI/CD pipeline for code projects is built on SHIP-HATS templates.

In some cases, you may wish to extend on the default functionality of the pipeline to cater to your team’s specific use cases. This page highlights some pipeline customisations frequently performed by other users.

Before proceeding to modify the default CI/CD pipeline, users are encouraged to familiarise with the following:

Using Environment Variables in Dockerfile

You may wish to use environment variables in your pipeline jobs during the image build job. Environment variables can be populated within the job definition or via CI/CD variables in the settings.

Kaniko (the tool used to build images) uses the flag --build-arg to specify ARGs in Dockerfile. We can specify an OPTS variable in the build-image job with the required arguments to be passed to the Dockerfile. Multiple --build-args can be specified for every ARG required.

Below is an example of passing environment variables from a job to a Dockerfile.

.gitlab-ci.yml

include:
  - project: wog/gvt/cstack/groups/references/pipelines/tenant-pipeline
    ref: main
    file: single-container/.gitlab-ci.yml

build-image:
  variables:
    JOB_VAR1: value1
    JOB_VAR2: value2
    OPTS: "--build-arg DOCKERFILE_ARG1=${JOB_VAR1} --build-arg DOCKERFILE_ARG2=${JOB_VAR2}

Dockerfile

FROM alpine:3.18

# Declare arguments matching those provided in .gitlab-ci.yml
ARG DOCKERFILE_ARG1
ARG DOCKERFILE_ARG2

# Set environment variables using the arguments
ENV MY_VAR1=${DOCKERFILE_ARG1}
ENV MY_VAR2=${DOCKERFILE_ARG2}

# Example: setting working directory based on passed environment variables
WORKDIR /app/${MY_VAR1}

# Other Docker commands that use the environment variables
...
Chat with me!
Bot Launcher