Dynamically setup gitlab-ci cache.key

Viewed 155

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
1 Answers

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"
Related