Pattern Matching for rules parameter Gitlab CI

Viewed 4976

I am trying to make use of the rules: parameter to make a job only appear in a pipeline if specified users did the push. I dont want to define the list of users for each job, so I have the following global variable:

variables:
  USER_LIST: "user1 user2 user3"

and in the job, I have the following:

rules:
 - if '$USER_LIST =~ /$GITLAB_USER_LOGIN/'
    when: on_success
 - when: never

This does not appear to be working, as I suspect the regex pattern being used is not being replaced by the variable, and using $GITLAB_USER_LOGIN as the search string. If I use an explicit search:

rules:
 - if '$USER_LIST =~ /user1/'
    when: on_success
 - when: never

then the pattern matches just fine.

NOTE: I am aware that GITLAB_USER_LOGIN is a protected variable. I get the same problem with GITLAB_USER_EMAIL too.

So the question is, how can I put a GITLAB predifined variable into a string that will be used for pattern matching?

2 Answers

You are using a variable and not a regular expression in your rule. So you don't need the slashes around the $GITLAB_USER_LOGIN variable. Try something like this:

rules:
  - if '$USER_LIST =~ $GITLAB_USER_LOGIN'

The documentation shows that the content matching regex should look like this:

$VARIABLE =~ /^content.*/

for your code you need to add ^ and .*:

rules:
 - if '$USER_LIST =~ /^user1.*/'
    when: on_success
 - when: never
Related