Skip to content

Variables

Goal

Learn how to...

  • add local variable to your pipeline
  • consume pre-defined variables
  • add secrets in the UI

Task 1: Create a job variable

This exercise requires an updates version of our hello world program:

git checkout upstream/160_gitlab_ci/020_variables/inline -- main.go

Add a variable called version to the job called build and modify the build command as follows:

go build -o hello -ldflags "-X main.Version=${version}" .

See the official documentation about variables.

Afterwards check the pipeline in the GitLab UI. You should see a successful pipeline run.

Hint (Click if you are stuck)
  1. Use the variable keyword to define a variable inside the job called build
  2. Replace the build command with the one provided above
Solution (Click if you are stuck)

.gitlab-ci.yml:

stages:
- check
- build

lint:
  stage: check
  script:
  - apk update
  - apk add go
  - go fmt .

audit:
  stage: check
  script:
  - apk update
  - apk add go
  - go vet .

build:
  stage: build
  variables:
    version: dev
  script:
  - apk update
  - apk add go
  - |
    go build \
        -ldflags "-X main.Version=${version}" \
        -o hello \
        .
  - ./hello

If you want to jump to the solution, execute the following command:

git checkout upstream/160_gitlab_ci/020_variables/inline -- '*'

Task 2: Use a predefined variable

Read the official documentation about predefined variables and replace the job variable with the predefined variable CI_COMMIT_REF_NAME.

Afterwards check the pipeline in the GitLab UI. You should see a successful pipeline run.

Hint (Click if you are stuck)
  1. Remove the variable keyword from the job called build
  2. Replace the variable ${version} with the predefined variable ${CI_COMMIT_REF_NAME}
Solution (Click if you are stuck)

.gitlab-ci.yml:

stages:
- check
- build

lint:
  stage: check
  script:
  - apk update
  - apk add go
  - go fmt .

audit:
  stage: check
  script:
  - apk update
  - apk add go
  - go vet .

build:
  stage: build
  script:
  - apk update
  - apk add go
  - |
    go build \
        -ldflags "-X main.Version=${CI_COMMIT_REF_NAME}" \
        -o hello \
        .
  - ./hello

If you want to jump to the solution, execute the following command:

git checkout upstream/160_gitlab_ci/020_variables/predefined -- '*'

Task 3: Add a CI variable in the UI

This exercise requires an updates version of our hello world application:

git checkout upstream/160_gitlab_ci/020_variables/ci -- main.go

The application now also prints the name of the author which must be supplied during compilation as well.

Read the official documentation about CI variables and extend the build command to provide main.Author through a CI variable called AUTHOR.

Afterwards check the pipeline in the GitLab UI. You should see a successful pipeline run.

Hint 1 (Click if you are stuck)
  1. Go to Settings > CI/CD > Variables
  2. Add a variable called AUTHOR with your name
Hint 2 (Click if you are stuck)

The -ldflags option needs to be extended with -X 'main.Author=${AUTHOR}'

Solution (Click if you are stuck)

.gitlab-ci.yml:

stages:
- check
- build

lint:
  stage: check
  script:
  - apk update
  - apk add go
  - go fmt .

audit:
  stage: check
  script:
  - apk update
  - apk add go
  - go vet .

build:
  stage: build
  script:
  - apk update
  - apk add go
  - |
    go build \
        -ldflags "-X main.Version=${CI_COMMIT_REF_NAME} -X 'main.Author=${AUTHOR}'" \
        -o hello \
        .
  - ./hello

If you want to jump to the solution, execute the following command:

git checkout upstream/160_gitlab_ci/020_variables/ci -- '*'