Home > OS >  how can i use a regex expression in a gitlab CI IF THEN Statement
how can i use a regex expression in a gitlab CI IF THEN Statement

Time:10-22

how can i use a regex in a if then statement in a gitlab-ci pipline? i need to set a Variable if the branchname begins with feature/..... or develop/......

i have tryed:

before_script:
- >
if [[${CI_COMMIT_BRANCH} =~ feature\/.*]]; then
  echo "Feature Branch Found !"
else
  echo "Production Branch Found !"  
fi;

i have try the Regex with https://regex101.com/ regex101 say the regex matcht!

but its dont work, what is my mistake?

CodePudding user response:

You can use

if [[ "${CI_COMMIT_BRANCH}" =~ feature/ ]];
  echo "Feature Branch Found !"
else
  echo "Production Branch Not Found !"  
fi;

Note:

  • There must be spaces after [[ and before ]]
  • The .* in your case is redundant and can be removed (it just matches the rest of the string, and since you are not capturing the data and not using it later, this is not necessary)
  • It is always a good idea to quote your string variables, "${CI_COMMIT_BRANCH}".
  • Related