Skip to content

Allow variables in cache policy field

What does this MR do?

Allow variables in cache:policy field

Runner implementation: gitlab-runner!4085 (merged)

Related issues

Fix #371480 (closed)

How to run

  1. Checkout this branch and the corresponding cache-policy-variable branch in gitlab-runner!4085 (merged)
  2. Run the runner

Testing

The following CI examples show the new behavior:

Overwriting the value based on rules variables

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "web"

variables:
  CACHE_POLICY: pull

stages:
  - test

job_valid_variable_policy:
  stage: test
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      variables:
        CACHE_POLICY: pull-push
  tags:
    - runner
  script:
    - echo "Hello World" >> test.txt
  cache:
    key: 'foobar'
    policy: $CACHE_POLICY
    unprotect: true
    paths:
      - test.txt

Screenshot_2023-05-11_at_7.46.37_AM

Using a variable with an invalid value:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "web"

variables:
  CACHE_POLICY: pull

stages:
  - test
 
job_invalid_variable_policy:
  stage: test
  rules:
    - variables:
        CACHE_POLICY: blah
  tags:
    - runner
  script:
    - echo "Hello World" >> test.txt
  cache:
    key: 'foobar'
    policy: $CACHE_POLICY
    unprotect: true
    paths:
      - test.txt

Screenshot_2023-05-11_at_7.46.22_AM

Hard-coded invalid value:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "web"

variables:
  CACHE_POLICY: pull

stages:
  - test

job_invalid_hardcoded_policy:
  stage: test
  tags:
    - runner
  script:
    - echo "Hello World" >> test.txt
  cache:
    key: 'foobar'
    policy: blah
    unprotect: true
    paths:
      - test.txt

Screenshot_2023-05-11_at_8.10.38_AM

Checklist

Pre-merge

Consider the effect of the changes in this merge request on the following:

If new jobs are added:

This will help keep track of expected cost increases to the GitLab project average pipeline cost per merge request RPI

Post-merge

Edited by René Hernández Remedios

Merge request reports

Loading