Laser42
Laser42

Reputation: 744

Dynamically setup gitlab-ci cache.key

It there a way to set up cache: key: dynamically, like this:

cache:
    key: if $CI_PIPELINE_SOURCE -eq 'merge_request_event' { "MR_$CI_MERGE_REQUEST_TRAGET_BRANCH_NAME_CACHE" } else { "PUSH_$CI_COMMIT_REF_NAME_CACHE" }

Or maybe there is a way to define a pipile variable dynamically:

variables:
    CACHE_KEY: if $CI_PIPELINE_SOURCE -eq 'merge_request_event' { "MR_$CI_MERGE_REQUEST_TRAGET_BRANCH_NAME_CACHE" } else { "PUSH_$CI_COMMIT_REF_NAME_CACHE" }                            

cache:
    key: $CACHE_KEY

Upvotes: 2

Views: 1346

Answers (2)

Oleksandr Savchenko
Oleksandr Savchenko

Reputation: 692

As the documentation says:

Possible inputs:

  • A string
  • A predefined variables
  • A combination of both.

As an option, you might define one "default" cache which fits best for most cases and another one for specific cases

Upvotes: 0

Paul Ryan
Paul Ryan

Reputation: 491

What you're looking for is a rules check, so like this:

  variables:
    //Your default value
    CACHE_KEY: "PUSH_$CI_COMMIT_REF_NAME_CACHE"
  rules:
    - if: if $CI_PIPELINE_SOURCE == 'merge_request_event'
      //Override based on the above condition 
      variables:
        CACHE_KEY: "MR_$CI_MERGE_REQUEST_TRAGET_BRANCH_NAME_CACHE"

Upvotes: 2

Related Questions