Add variable to enable fallback cache key

Erik Lindahl requested to merge (removed):fallback_cache_key into master

What does this MR do?

It adds a FALLBACK_CACHE_KEY variable that is used in a second attempt to fetch the cache if none is found for the default cache key. It should support the same generalization as the normal cache key specification, including referring to other environment variables.

Why was this MR needed?

This solves the cache key "conflict" described at https://docs.gitlab.com/ee/ci/yaml/#cache, where we can either use a common cache key (which is constantly overwritten), or have per-branch cache (with ${CI_COMMIT_REF_SLUG}) where the first build in each branch is not cached. By first trying a specific key and then having a fallback to clone from if the per-branch one is not available yet, there will be a cache for the initial build in each MR branch too.

For large repos this has potential to drastically cut down the CI build time, in particular when there are many small merge requests that each will appear as a new branch.

Does this MR meet the acceptance criteria?

  • Documentation created/updated
  • Added tests for this feature/bug
  • In case of conflicts with master - branch was rebased

This code is close to trivial, but presently there does not seem to be any place in the gitlab-runner for documenting the cache settings, nor any mechanisms to test caches, so I have not added it here either.

I guess the ideal long-term implementation would be to add a proper "fallback_key" field to the cache nodes in the .gitlab-ci.yml yaml specification and read it from there instead, but that will require changes beyond gitlab-runner.

Related Issues

#4792 (closed)

Edited by Tomasz Maczukin

Merge request reports