Search code examples
cachinggitlabgitlab-cicicd

GitLab cache sharing between branches


I'm trying to run job A and B on merge request. After a successful merge, job C should be executed. Job A is used to install a Nodejs dependencies and push them to cache so jobs B and C can use that cache. On Gitlab side I'm using a shared runners. Please help me to solve that task. My gitlab-ci.yml:

image: node:19-alpine
stages:
  - install_dependencies
  - test1
  - test2

cache: 
   key: somekeyvalue1234
   paths:
     - node_modules/
   policy: pull

install_dependencies:
  stage: install_dependencies
  cache:
    key: somekeyvalue1234
    paths:
      - node_modules/
    policy: push
  script:
    - npm ci
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'   

test1:
  stage: test1
  script:
    - npm run test1
  rules:
     - if: '$CI_COMMIT_BRANCH == "merge-request-test"'

test2:
  stage: test2
  script:
    - npm run test2
  rules:
    - if: '$CI_COMMIT_BRANCH == "branch-foo"'

For some reason the cache can be successfully downloaded by job B while MR is triggered. But after a successful MR job C can't find that cache. How to populate the cache while running my pipeline showed above?


Solution

  • Finally solved the issue. It was very simple but I lost a few days by googling. In CI/CD Settings -> General pipelines I unchecked the setting Use separate caches for protected branches and voila