Search code examples
cachinggitlab-cigitlab-ci-runner

Gitlab cache not uploading due to policy


I am getting the error Not uploading cache {name of branch} due to policy in my gitlab runner. My .yaml file looks like this:

stages:
  - test
  - staging
  - publish
  - deploy

# cache using branch name
# https://gitlab.com/help/ci/caching/index.md
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .yarn
    - node_modules/
  policy: pull

before_script:
  - yarn install --cache-folder .yarn

test:
  stage: test
  image: node:14
  script:
    - yarn install
    - yarn test

pages:
  stage: staging
  image: alekzonder/puppeteer
  except:
    - master
  script:
    - yarn install
    - yarn compile
    - yarn build

publish:
  stage: publish
  image: alekzonder/puppeteer
  when: manual
  script:
    - yarn install
    - yarn compile
    - yarn build
  artifacts:
    paths:
      - dist

deploy:
  image: google/cloud-sdk:latest
  stage: deploy
  needs:
    - publish
  script:
    - gcloud auth activate-service-account --account ${GCP_SERVICE_ACCOUNT} --key-file ${GOOGLE_APPLICATION_CREDENTIALS}
    - gsutil -u test rsync -r -d dist/ gs://test.ca

I was wondering why it always fails to upload, and thereby fails to extract the cache. Any tips or corrections welcome. Here is a screenshot of where it fails: gitlab runner screenshot


Solution

  • You have the following set:

    cache:
      key: ${CI_COMMIT_REF_SLUG}
      paths:
        - .yarn
        - node_modules/
      policy: pull
    

    Which sets a pipeline-global precedent that you only want to (policy: pull) pull from the cache.

    You'll want to read https://docs.gitlab.com/ee/ci/yaml/#cachepolicy

    If you omit the policy: piece, the default is to pull-push (which will get your cache uploading).

    I tend to have my pipelines structured a little different than yours, though. I typically have a "prep" step that I define, and then run the yarn install once there:

    "Installing Dependencies":
      image: node:lts
      stage: Prep
      cache:
        paths:
          - .yarn
          - node_modules/
        policy: pull-push
      script:
        yarn install
    
    ... 
    

    Note: Then you can leave your global policy of 'pull', since this one job will have an override to pull-push.

    Then, you can remove the yarn install on all other tasks, as the cache will be restored.