Skip to content

CI variables in rules regexp

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

Summary

I'd like to make my gitlab-ci as generic as possible by defining rules using variables to trigger the pipelines. To that end, I would also like to use variable values in the regexp of my rules.

Example configuration

Here's the sample of my gitlab-ci code:

variables:
  JAVA_PROJECTS: ressources|calculateur

.onJavaTag:
  rules:
    - if: '$CI_COMMIT_TAG =~ /($JAVA_PROJECTS)@(.+)/'

Example Project

I've setup a small test repo with that gitlab-ci here: https://gitlab.com/ArtyMaury/gitlab-ci-bug

Desired behavior

The pipeline should be triggered on a ressources@1.0.0 tag


Workaround via inputs

This example from the comments shows how to use variables with regexp via expand_vars.

# .gitlab-ci.yml

include:
  - local: my_template.yml
    inputs:
      my_var: $MY_VAR  # Defined as a Project variable with value: "my_test|my_value"
# my_template.yml

spec:
  inputs:
    my_var:
---

test-job:
  script: echo
  rules:
    - if: '"my_value" =~ /^($[[ inputs.my_var | expand_vars ]])$/'

Full configuration:

Screenshot_2023-10-03_at_9.43.50_AM

This results in test-job added to the pipeline.

NOTE: expand_vars currently only supports the same variables available to include. Documentation: https://docs.gitlab.com/ee/ci/yaml/inputs.html#expand_vars – so variables defined with the global or job-level variables: keyword cannot be expanded. Neither can job-level predefined variables.

Edited by 🤖 GitLab Bot 🤖