Currently having trouble attempting to resolve an odd caching issue after rebasing and pushing to a branch.
Currently I’m defining my cache as the following:
cache: &global_cache
key: "cache-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA"
paths:
- ".gradle/caches"
policy: pull-push
I have two stages:
stages:
- Dependencies
- Lint
gradle_dependencies:
stage: Dependencies
cache:
# Inherit global cache settings
<<: *global_cache
# Override policy
policy: push
script:
- gradle customTask()
when: always
gradle_ktlintCheck:
stage: Lint
script:
# Rely on cache from dependencies stage
- gradle --offline ktlintCheck
artifacts:
paths:
- app/build/reports/ktlint/*
when: on_success
needs:
- job: gradle_dependencies
This pipeline has been successful for weeks when running independently on one branch. However, now that we’ve integrated it into our main branch we’re running into concurrency issue. For example, when rebasing a branch pushing those changes and pushing up to another branch at around the same time.
I see the dependencies stage make the correct cache file:
Creating cache cache-release-1-0-0-7205d38d-1-non_protected...
.gradle/caches: found 32673 matching artifact files and directories
No URL provided, cache will not be uploaded to shared cache server. Cache will be stored only locally.
Created cache
but the lint stage will just randomly not find the same exact cache:
Checking cache for cache-release-1-0-0-7205d38d-1-non_protected...
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.
WARNING: Cache file does not exist
Failed to extract cache
If I manually run the pipeline again when another pipeline is not running it works totally fine. I’m confused if the cache is getting deleted or something by another pipeline? It’s just strange because I would have thought cache-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA
was going to be extremely unique and not have any conflicts with other pipelines and branches.
My only other thought was to integrate a Resource Group, to avoid concurrency and this issue but I don’t feel like this should be failing the way it’s integrated.
I see another post similiar to this: When two pipelines are executed simultaneously, do they access the cache concurrently? - #2 by dnsmichi
If jobs run concurrently, they might download the same cache, but later override the cache upload, depending which job finishes first. To prevent that, you can use resource_groups to lock the job, not being run in parallel. Resource group | GitLab
Alternatively, configure a cache key that is not global, but per branch. Caching in GitLab CI/CD | GitLab
But I feel like the cache key I have should resolve this problem?
Versions
Self Hosted Runner: 17.0.0 on Ubuntu 24.04