Skip to content

Cache fallback doesn't take increment index into account

Summary

Quote from @qventura gitlab-runner!1534 (comment 435187741):

I've been struggling to have a useful usage of this feature, and just figured out this was because of this: https://docs.gitlab.com/ee/ci/caching/#clearing-the-cache-manually

Basically, every time the cache get cleared, an integer get appended to the cache key. So my CACHE_FALLBACK_KEY which is master never match with the actual master cache which is master-xxx

Steps to reproduce

  1. Clear cache manually https://docs.gitlab.com/ee/ci/caching/#clearing-the-cache-manually
  2. Run pipeline for the main branch. After it, you will have a cache for the main branch.
  3. Make some branch from the main
  4. Run pipeline for it
.gitlab-ci.yml
jest:
  stage: quality_check
  variables:
    CACHE_FALLBACK_KEY: jest-main
  cache:
    key: jest-$CI_COMMIT_REF_SLUG
    paths:
      - .jestcache/
    when: 'always'
  script:
    - yarn jest

Actual behavior

The fallback will not have a cache index and will not use fallback:

Restoring cache
Checking cache for feature/ci-shared-cache-9...
FATAL: file does not exist                         
Failed to extract cache
Checking cache for main...
FATAL: file does not exist    

Expected behavior

It should fall back on $CACHE_FALLBACK_KEY-$CACHE_INCREMENT_INDEX (I did not find the real variable name in sources)

Relevant logs and/or screenshots

job log
Restoring cache
Checking cache for feature/ci-shared-cache-9...
FATAL: file does not exist                         
Failed to extract cache
Checking cache for main...
FATAL: file does not exist    

Environment description

Custom installation, but I think it doesn't matter.

Used GitLab Runner version

Version:      13.6.0
Git revision: 8fa89735
Git branch:   13-6-stable
GO version:   go1.13.8
Built:        2020-11-21T06:16:31+0000
OS/Arch:      linux/amd64

Possible fixes

As I understand we should append an incrementing index to CACHE_FALLBACK_KEY

@erik.lindahl @tmaczukin