Skip to content

Provide ability to reuse results of variable expressions in if directives

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Proposal

Provide ability to reuse results of expressions involving variables in rules:if directives. That is, provide the ability to assign the result of an expression to a variable/macro so that instead of reusing the whole expression again and again in multiple if directives, that variable/macro can be used.

To have them not get polluted by env variables inadvertently, maybe use a different syntax than ${name} - something like %{name}.

Consider the below snippet as an example. An omnibus-gitlab package build is considered an EE build if either of the following are true

  1. ee variable is true
  2. branch name ends with -ee
  3. tag name consists of ee
  4. It is an auto-deploy branch
  5. It is an auto-deploy tag

triggered-ee-job is supposed to run only on EE builds that are triggered. Similarly, scheduled-ee-job is supposed to run only on EE builds that are scheduled.

macros:
  EE_BUILD: '$ee == "true" || $CI_BRANCH_NAME =~ /^.*-ee$/ || $CI_BRANCH_NAME =~ /^[0-9]+-[0-9]+-auto-deploy-[0-9]+$/ || $CI_COMMIT_TAG =~ /^.*ee\.\d.*$/ || $CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+\+[^ ]{7,}\.[^ ]{7,}$/'

triggered-ee-job:
  rules:
    - if: '%{EE_BUILD} && $CI_PIPELINE_SOURCE == 'trigger'
      when: always
    - when: never
  script:
    - bash trigger.sh

scheduled-ee-job:
  rules:
    - if: '%{EE_BUILD} && $CI_PIPELINE_SOURCE == 'schedule'
      when: always
    - when: never
  script:
    - bash schedule.sh

Here, the macro EE_BUILD is used to store the result of an expression (is this an EE build). Then, that macro is reused in if directives instead of that long expression.

Edited by 🤖 GitLab Bot 🤖