I'm trying to set up a Gitlab CI for running script on my gitlab-runner server. I have a string variable declared in the UI (Settings -> Ci/CD -> Variables) meaning the version of some script using for testing purposes. I need to include this variable in the job's title as build_version_5.1.1.M_x86
where 5.1.1.M is the var's value. Moreover, I want to use it in the script so will be script: ./build.sh -t target-5.1.1.M-x86
.
The issue is that it sets into the script, but job's name displayed as build_version_$UI_GLOB_VAR_x86
. How can I set a global variable in both job's title and script?
The structure is
.gitlab-ci.yml
...
gitlab-ci-jobs/.build.gitlab-ci.yml
I made a .gitlab-ci.yml file with an include section that connects files with jobs:
stages:
- some_stage
- build
job_name:
stage: some_stage
tags:
- my_runner
script:
- some script
include:
- local: 'gitlab-ci-jobs/**.yml'
Also I've created job's .yml file (there may be several in the future) in other directory specified in include section:
spec:
inputs:
UI_GLOB_VAR:
default: $UI_GLOB_VAR
---
"build_version_$[[ inputs.UI_GLOB_VAR ]]_x86":
stage: build
tags:
- my_runner
artifacts:
paths:
- ./artifacts/*.bin
script:
- ./build.sh -t target-$[[ inputs.UI_GLOB_VAR ]]-x86
when: always
"build_version_$[[ inputs.UI_GLOB_VAR ]]_x64":
stage: build
tags:
- my_runner
artifacts:
paths:
- ./artifacts/*.bin
script:
- ./build.sh -t target-$[[ inputs.UI_GLOB_VAR ]]-x64
when: always
The result contains correct scripts, but job's names are build_version_$UI_GLOB_VAR_x86
and build_version_$UI_GLOB_VAR_x64
.
I've also tried to add inputs in .gitlab-ci.yml:
include:
- local: 'gitlab-ci-jobs/**.yml'
inputs:
UI_GLOB_VAR: $UI_GLOB_VAR
and/or setted different vars namings in main and child ymls: LOCAL_VAR: $UI_GLOB_VAR
, but got the same.
It only worked (script and job's title) when setting a hardcoded default value for variable in child file w/o setted inputs in main yml file:
spec:
inputs:
UI_GLOB_VAR:
default: '5.1.1.M'
---
I am not sure why your question was down voted as I thought it was very well laid out and clearly made efforts to fix your issue. I had to take a bit of time to replicate your issue to understand what was happening.
In your current setup, where the value is being rendered inside the script but not in the job name. This is because the input spec will take the default as a literal.
This is why it appears as $UI_GLOB_VAR
in the job name. Its actually also rendered as that literally in the script block. Its just when the script block then runs the shell then interpolates that value with the value of that var from your CICD settings. We can see this with this simple job
"build_version_$[[ inputs.UI_GLOB_VAR ]]_x86":
stage: build
script:
- echo target-$[[ inputs.UI_GLOB_VAR ]]-x86
when: always
You can see in the job output the command has not the value of the var but the literal value of the input which will then be replaced by the shell.
$ echo target-$UI_GLOB_VAR-x86
target-5.1.1.M-x86
In order to acheive what you want, you need to make use of the expand_vars
input function https://docs.gitlab.com/ci/yaml/inputs/#expand_vars
Use expand_vars to expand CI/CD variables in the input value.
if I re-write the job as
"build_version_$[[ inputs.UI_GLOB_VAR | expand_vars ]]_x86":
stage: build
script:
- echo target-$[[ inputs.UI_GLOB_VAR | expand_vars ]]-x86
when: always
you can see the job now renders with the interpolated value and in the shell we see the command with the value rather than the literal variable name
$ echo target-5.1.1.M-x86
target-5.1.1.M-x86