Search code examples
gitlabyamlcicd

is there a gitlab cicd IN operator?


In the docs I find many OR examples:

job:
  script: echo "This job does NOT create double pipelines!"
  rules:
    - if: ($CI_COMMIT_BRANCH  == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == "develop") && $MY_VARIABLE

Is there a way to do something like this pseudo code:

job:
  script: echo "This job does NOT create double pipelines!"
  rules:
    - if: ($CI_COMMIT_BRANCH in ($CI_DEFAULT_BRANCH, "develop") && $MY_VARIABLE

Solution

  • No, in is not an available operator. However, regex offers an equivalent with patterns like (a | b) so $FOO in (a, b) could be expressed with a regex rule like $FOO =~ /(a | b)/

    So something like this might work for you:

    # ...
    variables:
      rules:
        - if: '$CI_COMMIT_BRANCH =~ /(main|develop)/ && $MYVAR'
    

    One caveat, however, is that because of unresolved issues in GitLab's regex and variable handling, you can't use variables directly in a pattern. So you have to statically declare your default branch as above in this case.

    In cases where you must use a variable, your first example is probably the most reasonable approach.