Using a bash
runner, is there any reason why the following variable expansion shouldn't work?
variables:
GIT_BRANCH: "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH:-$CI_DEFAULT_BRANCH}}"
job1:
script:
- echo $GIT_BRANCH
The job outputs }
.
I'm using GitLab Enterprise Edition 14.1.8-ee.
CodePudding user response:
These expansions are done by GitLab CI, not by bash, so they probably simply can't handle nested expansions as robustly as bash can.
They probably think the variable name is everything up to the next }
, so CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH:-$CI_DEFAULT_BRANCH
without a closing curly brace. That would explain why there is a leftover }
when the dust settles.
CodePudding user response:
Gitlab variables can not perform bash operations as you try here.
Gitlab understands ${VARIABLE_NAME}
In your case
${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH:-$CI_DEFAULT_BRANCH}}
Gitlab tries to find the variable CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH:-$CI_DEFAULT_BRANCH}
But this variable doesn't exist, so it only outputs }
CodePudding user response:
As stated by the other answers, this syntax won't expand how you expect. As a workaround, you might be able to use rules:variables:
to get the same effect.
my_job:
rules:
- if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME'
variables:
GIT_BRANCH: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
- if: '$CI_COMMIT_BRANCH'
variables:
GIT_BRANCH: $CI_COMMIT_BRANCH
- when: on_success
variables:
GIT_BRANCH: $CI_DEFAULT_BRANCH
You could also consider simply setting the variable directly in the job script using export
. Then the bash variable expansion will work as expected.