I have a scenarios where I need to push a feature branch's commit to an environment for testing. There are few environments and a developer has a choice to push to any ENV based on the commit message.
rules:
- if: $CI_COMMIT_REF_NAME =~ /^feature\/CONN-*/ && $CI_COMMIT_MESSAGE =~ /^*Enterprise_1.*$/
variables:
Name_Tag: "Enterprise_1"
when: always
- if: $CI_COMMIT_REF_NAME =~ /^feature\/CONN-*/ && $CI_COMMIT_MESSAGE =~ /^*Enterprise_2.*$/
variables:
Name_Tag: "Enterprise_2"
when: always
- if: $CI_COMMIT_REF_NAME =~ /^feature\/CONN-*/ && $CI_COMMIT_MESSAGE =~ /^*Enterprise_3.*$/
variables:
Name_Tag: "Enterprise_3"
So if a commit message contains a string with "Enterprise_2", then deployment will go to "Enterprise_2" environment. likewise depending on Enterprise_X, an Enterprise_X env will be picked. I searched if capture groups are supported on gitlab, but couldn't find any documentation.
Also i tried the below:-
rules:
- if: $CI_COMMIT_REF_NAME =~ /^feature\/CONN-*/ && $CI_COMMIT_MESSAGE =~ /^*Enterprise_[1-9].*$/
variables:
Name_Tag: 'if [[ $CI_COMMIT_MESSAGE =~ ^.*(Enterprise_[1-9])(.*)$ ]]; then echo "${BASH_REMATCH[1]}"; fi'
But this sets Name_Tag variable to literal "if [[ $CI_COMMIT_MESSAGE =~ ^.(Enterprise_[1-9])(.)$ ]]; then echo "${BASH_REMATCH[1]}"; fi".
could you please advise on a solution.
rules
are not really meant to be used to set variables. As the reference states:
Use rules to include or exclude jobs in pipelines.
For this situation before_script
step should be used:
job:
rules:
- if: $CI_COMMIT_REF_NAME =~ /^feature\/CONN-*/ && $CI_COMMIT_MESSAGE =~ /^*Enterprise_[1-9].*$/
before_script:
- >
if [[ $CI_COMMIT_MESSAGE =~ "^.*(Enterprise_[1-9]).*$" ]]; then
NAME_TAG=$(BASH_REMATCH[1]);
fi
script:
- echo $NAME_TAG
First, we are going to have only one rule, for branches in feature/CONN-x
format, and whose commit message has Enterprise_x
string. Since we are sure that commit message has desired string we can extract it in before_script
. If the regex is matched, set NAME_TAG
to the value of the captured group.
If for any reason BASH_REMATCH
isn't working for you, like it (still) isn't working for me, you can try sed
. Match group you need, and substitute everything before and after the group:
job:
before_script:
- NAME_TAG=$(echo $CI_COMMIT_MESSAGE | sed -r "s/^.*(Enterprise_[1-9]).*$/\1/")