Gitlab CI: Use `rules` to check if value exists in an array

Viewed 123

I am using an anchor for defining list of branches to run some stages of my pipeline on like so:

.branches:
  &BRANCHES:
  - master
  - my-feature-branch

I want to use the rules in a way that it checks if the commit branch is same as one of the branches listed in the BRANCHES variable, and if true then do something:

rules:
 - if: ($CI_COMMIT_BRANCH in *BRANCHES)
   // Do something

I know this syntax is wrong, so how can I achieve this?

1 Answers

Since YAML anchors are used for variable assignations or scripts, a simpler approach (described in this thread) would be to use an actual string array, in a shell-based script: section:

BRANCHES="master my-feature-branch"
for BRANCH in ${BRANCHES}
do
  if [[ "${CI_COMMIT_BRANCH}" == "${BRANCH}" ]]; then
    # do something
  fi
done
Related