Skip to content

Caches

Goal

Learn how to...

  • define caches
  • store data in a cache
  • restore data from a cache
  • avoid relying on the cache

Task 1: Test caching

The cache is used by adding the keyword cache in a pipeline job, a job template or in the default section. Let's give it a try:

  1. Add a job test_cache to the first stage
  2. Download dependency information from the uniget project:
    curl -sSLfO https://github.com/uniget-org/cli/raw/main/go.mod
    curl -sSLfO https://github.com/uniget-org/cli/raw/main/go.sum
    
  3. Use the official example for Go to enable caching
  4. Instead of go test run go mod download to download dependencies only

Afterwards check the pipeline in the GitLab UI. You should see a successful pipeline run. Retry the job to see the effect of the cache.

Hint (Click if you are stuck)

Use the .go-cache job template from the official example for Go.

Solution (Click if you are stuck)

.gitlab-ci-yml:

#...

.go-cache:
  variables:
    GOPATH: $CI_PROJECT_DIR/.go
  before_script:
  - mkdir -p .go
  cache:
    key: ${CI_PROJECT_PATH_SLUG}
    policy: pull-push
    paths:
    - .go/pkg/mod/

test_cache:
  stage: check
  extends:
  - .go-cache
  script:
  - go mod download  

#...

This was just a demonstration. The changes will not be preserved in the following chapters.

Task 2: Add caching

Now, integrate the job template .go-cache into the pipeline and use it for the jobs build and unit_test.

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

Solution (Click if you are stuck)

go.yaml:

.go-targets:
  parallel:
    matrix:
    - GOOS: linux
      GOARCH: amd64
    - GOOS: linux
      GOARCH: arm64

.go-cache:
  variables:
    GOPATH: $CI_PROJECT_DIR/.go
  before_script:
  - mkdir -p .go
  cache:
    key: ${CI_PROJECT_PATH_SLUG}
    policy: pull-push
    paths:
    - .go/pkg/mod/

.build-go:
  extends:
  - .go-targets
  - .go-cache
  script:
  - |
    go build \
        -o hello-${GOOS}-${GOARCH} \
        -ldflags "-X main.Version=${CI_COMMIT_REF_NAME} -X 'main.Author=${AUTHOR}'" \
        .
  artifacts:
    paths:
    - hello-${GOOS}-${GOARCH}

.test-go:
  extends:
  - .go-targets
  before_script:
  - apt-get update
  - apt-get -y install file
  script:
  - |
    file hello-${GOOS}-${GOARCH}

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

git checkout upstream/160_gitlab_ci/265_caches -- '*'