Skip to content

Defaults

Goal

Learn how to...

  • avoid repetition in jobs
  • specify defaults are the top of your pipeline

Task: Don't repeat yourself

All jobs currently have a dedicated image directive. Using defaults, this repetition can be avoided. See the official documentation.

Replace job specific image directives with the default directive.

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

Hint (Click if you are stuck)
  1. Remove image from all build jobs
  2. Add default with the image directive at the top
Solution (Click if you are stuck)

.gitlab-ci.yml:

stages:
- check
- build

default:
  image: golang:1.19.2

lint:
  stage: check
  script:
  - go fmt .

audit:
  stage: check
  script:
  - go vet .

build:
  stage: build
  script:
  - |
    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/050_default -- '*'

Bonus 1: Override defaults

Jobs can still choose to use an image different from the default:

  1. Add a new job
  2. Add an image directory to the new job
  3. Specify a different image
  4. Check out how the executation environment changes

Bonus 2: Default values for variables

See the official documentation for default as well as variables and check how they are related.

Solution (Click if you are stuck)

Global variables are not located under default but under the global variables keyword.