How to use the "cache" in gitlab?

Viewed 214

I have created the following test pipeline in gitlab:

job1:
  stage: stage1
  rules:
    - if: $RUN != "run2"
      when: always
    - when: never
  script:
    - echo "Job stage1 updating file dates.txt"
    - date >> data/dates.txt
  cache:
    key: statusfiles
    paths:
      - data/dates.txt

job2:
  stage: stage2
  rules:
    - if: $RUN == "run2"
      when: always
  script:
    - echo "Running stage 2"
    - cat data/dates.txt
  cache:
    key: statusfiles
    paths:
      - data/dates.txt

where I want to use the "cache" feature of gitlab.

Here I first run job1 which update the file date.txt and add an entry to this example files, so it contains two lines.

However, when I run a new pipeline with job2 alone, the files contain only ONE line. The files seem to be the original, unmodified files.

How can I "upload" or "save" the files into the cache in job1, so I can use the updated file in a later run of job2?

1 Answers

Test first if the section "Share caches between jobs in the same branch" is relevant.

To have jobs in each branch use the same cache, define a cache with the key: >

$CI_COMMIT_REF_SLUG:
cache:
  key: $CI_COMMIT_REF_SLUG

This configuration prevents you from accidentally overwriting the cache.

In your case, from the discussion, and from "How archiving and extracting works":

stages:
  - build
  - test

before_script:
  - echo "Hello"

job A:
  stage: build
    rules:
      - if: $RUN != "run3"
    when: always
      - when: never
  script:
    - mkdir -p data/
    - date > data/hello.txt
  cache:
    key: build-cache
    paths:
      - data/
  after_script:
    - echo "World"

  job B:
    stage: test
    rules:
      - if: $RUN == "run3"
    script:
      - cat data/hello.txt
    cache:
      key: build-cache
      paths:
        - data/

By using a single runner on a single machine, you don’t have the issue where job B might execute on a runner different from job A.

This setup guarantees the cache can be reused between stages.

It only works if the execution goes from the build stage to the test stage in the same runner/machine. Otherwise, the cache might not be available.

Related