Skip to content

Use commit sha for a given file in cache key

Marius Bobin requested to merge 18986-allow-to-use-commit-sha-in-cache-key into master

What does this MR do?

Adds an option for the caching key to be determined by some files in the repository (ie Gemfile.lock for Ruby, package.json for NPM, etc) instead of by static variables. This speeds up pipelines significantly as dependencies can be shared between branches and cache invalidation is less frequent.

cache:
  key:
    files:
      - Gemfile.lock
    prefix: gemfile-lock-at
  paths:
    - vendor/ruby

build:
  stage: build
  script:
    - ruby -v
    - bundle install -j $(nproc) --path vendor

With the definition from above, the cache key would be something like: gemfile-lock-at-feef9576d21ee9b6a32e30c5c79d0a0ceb68d1e5, where feef9576d21ee9b6a32e30c5c79d0a0ceb68d1e5 is the SHA for the latest commit that changed Gemfile.lock in the current branch.
We can add maximum two files when defining the cache key.

Related issue: #18986 (closed)

Screenshots

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Marius Bobin

Merge request reports