I am trying to manipulate a custom variable in my before_script based on the commit branch.
The TAG variable is set on the runner server in my process and is used as the version of the docker image that is created by the runners. I want to prepend ‘TEST-’ to $TAG if the commit branch is in the test/ namespace.
Relevant parts of my gitlab_ci.yml file are:
before_script:
- export TAG=$(date +%y.%.m.%d)
- |-
if [[ "$CI_COMMIT_BRANCH" =~ "/^test/\.*$/i" ]]; then
export TAG="TEST-${TAG}";
fi;
- echo $TAG
I’m reasonably certain that the regex test is correct because the build won’t happen unless that specific test passes in the rules: section of my build stage. As in:
rules:
- if: '$CI_COMMIT_BRANCH =~ /^test\/.*$/i'
The process runs, but the $TAG variable is set only to the date without the ‘TEST-’ prefix.
I’m certain this is something very minor that I have missed. Any help is greatly appreciated.
This does mean that you need to export the TAG variable in every job that uses the .job anchor, but you can probably get away with only implementing rules and before_script in the concrete jobs.
Yes, if that rule was for a specific job I would do that. What isn’t explained in the original question is that I’ve only showed the minimal code to explain my issue. The jobs in question have several rules that control when they run. This is an additional case.
Also I imagine if I can’t get it working in the before_script, that test likely won’t work in the script part of the job either.
I think this is probably a reg exp question, more than a GitLab question. Personally, I usually try an online reg exp tester when I have problems like this. There are many online (this one seems OK) and you should be able to use them to test with a variety of test strings, which should give you some confidence that you’ve ended up with a good solution.
Thank you for prompt response.
I think I do something wrong in GitLab because reg exp is created properly.
The first what I did was to check it in regex101.com