Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern Matching for rules parameter Gitlab CI

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?

like image 853
Tricky Avatar asked Mar 28 '26 12:03

Tricky


2 Answers

We can't use variables inside pattern for some (gitlab) reason. But we can swap list of privileged users and the current user in rule. Just transform list of users to regexp pattern:

.job:
    variables: 
        PRIVILEGED_USERS_PATTERN: '/user1|user2|userN/i'
    rules:
        - if: $GITLAB_USER_LOGIN =~ $PRIVILEGED_USERS_PATTERN
          when: manual

This solution has two minuses:

  1. List of privileged users visible to all, who have reading access to the project

  2. Pattern length limit (no testing)

But for now i don't see other solution how to not running the job at all.

like image 63
Drozt Avatar answered Apr 02 '26 11:04

Drozt


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'
like image 22
TheLoneKing Avatar answered Apr 02 '26 12:04

TheLoneKing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!