'Gitlab CI - Custom variable as a branch in trigger downstream
I would like to use a custom variable as branch inside a trigger downstream stage, but it looks like so far that before_script
(when I put the variable assignment) is not called before a trigger stage.
That leads to not have the desired branch triggered, because of empty $CUSTOM_VARIABLE
.
Basically, this is to trigger the child on a specific TAG name instead of the branch name, if TAG is given...
Here is a sample code showing what I want to do:
default:
before_script:
if [ $CONDITION ];
then
CUSTOM_VARIABLE=$TAG_NAME;
else
CUSTOM_VARIABLE=$CI_COMMIT_REF_NAME;
fi
build:
stage: build
variables:
VAR1: var1
VAR2: var2
trigger:
project: my/child/project
branch: $CUSTOM_VARIABLE
strategy: depend
I also tried to assign the variable conditionally inside variables
, but the code is not evaluated and is pasted as it is inside the custom variable.
variables:
CUSTOM_VARIABLE: $([ $CONDITION ] && echo "$TAG_NAME" || echo "$CI_COMMIT_REF_NAME")
Is there a way to use a custom variable as a trigger branch ?
Solution 1:[1]
You need to use dotenv
before_script:
if [ $CONDITION ];
then
CUSTOM_VARIABLE=$TAG_NAME;
echo "CUSTOM_VARIABLE=$CUSTOM_VARIABLE" >> build.env
else
CUSTOM_VARIABLE=$CI_COMMIT_REF_NAME;
echo "CUSTOM_VARIABLE=$CUSTOM_VARIABLE" >> build.env
fi
artifacts:
reports:
dotenv: build.env
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Taylan Derinbay |