Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression for [STRING] in gitlab-ci.yml file

I am tyring to set rule for deployment stage in gitlab-ci.yml file where if the git commit message is having a particular [STRING] in this format then it should deploy to that particular environment where this rule is written.

# Deploy to QAT environment
deploy-qat:
  stage: deploy
  extends: .helm_deploy
  environment:
    name: qat
  tags:
    - exe-prd
  rules:
    - if: $CI_COMMIT_MESSAGE =~ "/[QAT]$/|/[qat]$/"  #&&  $CI_COMMIT_REF_NAME == "example/qat"
      when: always

I have wrote above rule however it is not working. I have tried below combinations of regular expressions however none of them are working.

"/\[QAT\]/|/\[qat\]/"
"/[QAT]/|/[qat]/"
"*\[QAT\]*|*\[qat\]*"
"\[\(QAT\|qat\)\]"
"\[\(QAT\|qat\)]"
"/\[(qat|QAT)\]/"

I tried following website for regular expression here which validates my requirement but it is not working inside gitlab-ci.yml file.

like image 222
Shailesh Sutar Avatar asked Mar 23 '26 17:03

Shailesh Sutar


1 Answers

You can use

# Deploy to QAT environment
deploy-qat:
  stage: deploy
  extends: .helm_deploy
  environment:
    name: qat
  tags:
    - exe-prd
  rules:
    - if: $CI_COMMIT_MESSAGE =~ /\[(QAT|qat)]/
      when: always

See more about how to format regex matching conditions at the rules:variables reference page.

NOTES:

  • /\[(QAT|qat)]/ should not be put inside quotes
  • You need to use /.../ regex literal syntax (the backslashes are regex delimiters)
  • \[(QAT|qat)] is a regex that matches [, then either QAT or qat, and then a ] char
  • =~ is a regex matching operator.
like image 105
Wiktor Stribiżew Avatar answered Mar 25 '26 13:03

Wiktor Stribiżew