Run a Gitlab CI job if its name is in a variable

214 Views Asked by At

I was trying to execute a Gitlab CI job if its name is in a variable. I don't want to hardcode the job name inside the if condition.

rules:
  - if: '$JOBS =~ /\b$CI_JOB_NAME\b/'

This approach seems clean and efficient. Unfortunately, the rule is not matched, and the job is not executed.

I also tried the following code, and it works. So, I suppose the problem is related to the regex. The limit of the solution below is that it doesn't allow to specify multiple jobs.

rules:
  - if: '$JOBS == $CI_JOB_NAME'
1

There are 1 best solutions below

0
On

I found the problem. We can't use variables inside a pattern. There are possible solutions: Pattern Matching for rules parameter Gitlab CI