Gitlab-CI.yml - issue handling regex in before_script in conditionals

Summary

In gitlab-ci.yml file, when I try to use regex in a conditional expression like below, it doesn't work as expected (matching is not happening).

Steps to reproduce

  1. Define a global variable in variables section like so:
variables:
   IMAGE_TAG_DYNAMIC: latest # the default value
  1. Try using the following logic in gitlab-ci.yml in before_script section:
    - echo "$CI_COMMIT_BRANCH"
    - >
        if [[ "$CI_COMMIT_BRANCH" == "master" ]]; then
        echo "master!";
        export IMAGE_TAG_DYNAMIC="latest";
        fi;
    - >
        if [[ "$CI_COMMIT_BRANCH" =~ /^[rR][cC]-.*$/ ]]; then
        echo "RC!";
        export IMAGE_TAG_DYNAMIC="rc";
        fi;
    - > 
        if [[ "$CI_COMMIT_BRANCH" == "release" ]]; then
        echo "release!"
        export IMAGE_TAG_DYNAMIC="release";
        fi;
    - echo "$IMAGE_TAG_DYNAMIC"

What is the current bug behavior?

While running build and deploy on a branch with a name starting with RC, the value of IMAGE_TAG_DYNAMIC is 'latest'.

What is the expected correct behavior?

While running build and deploy on a branch with a name starting with RC, conditional expression if [[ "$CI_COMMIT_BRANCH" =~ /^[rR][cC]-.*$/ ]]; should be true and the value of IMAGE_TAG_DYNAMIC should be set to rc.

Additional notes

Direct match on the branch name works, which means that other parts of the logic work as expected (the conditon itself, assigning a new value to the variable). Just the regex seems to be the issue.

I have tried syntax with single quotes and double quotes around the regex, tried having i in the end like so /.../i - none of the variations worked.

The same regex works well in 'only' condition in a different part of the file:

deploy_staging:
  <<: *deploy
 # (omitted stuff)
  only:
    # only if branch starts with RC
    - /^[rR][cC]-.*$/
Edited by Olya Samusik