I'm struggling with gitlab ci/cd variables. I see so many conflicting examples. Anyhow, what I would like to know is how to use variables outside and within scripts.
For example, in a job config, can I assign a variable in the script section with a bash command?
some-job:
variables:
SOME_VAR: ''
script:
- SOME_VAR = $(<file_with_a_line_of_text.txt)
In the above case, I'm not sure if I can do this. But I need to populate a variable with the file contents (ie artifact). Also, when do I use '$' in front of the variable? Some examples I see using these formats:
"SOME_VAR" #in quotes, no dollar sign
"${SOME_VAR}" #in quotes, with dollar sign and wrapped with curly braces
${SOME_VAR} #no quotes, with dollar sign and wrapped with curly braces
$SOME_VAR #i.e. without the double quotes or curly braces
SOME_VAR #i.e. without the double quotes, dollar sign, and curly braces
So many variations of usage that I can see in examples but don't really know when to use each style. And I can't find one example online of a custom variable being set in a script using a bash command.
When I'm setting variables in bash, I always do it without the spaces around the =
:
VAR1="some string"
VAR2=23
VAR3=true
VAR4=$(cat /path/to/file.txt)
Let's go through these examples one at a time:
$(#command)
.Now let's use them:
echo $VAR1
# some string
echo "This is my variable $VAR1"
# This is my variable some string
echo "This is my variable ${VAR1}"
# This is my variable some string
echo ${VAR1}
# some string
echo "Error code ${VAR2}A"
# Error code 23A
echo "Error code $VAR2A"
# Error code --- Note: the variable $VAR2A dosn't exist
echo "Error code ${VAR2}${VAR1}"
# Error code 23some string
echo VAR1
# VAR1
echo "VAR1"
# VAR1
This illustrates the difference between the different forms, but in general, you reference a variable's value with $+variable-name
. Doing "SOME_VAR"
or SOME_VAR
just prints out the string "SOME_VAR" (ie, not referencing a variable at all).
The difference between $SOME_VAR
and ${SOME_VAR}
is that the latter lets you use it when there is other content directly before or after the variable without erroring.
How to use custom variables in gitlab ci/cd?
Normally like in any other shell.
But note that gitlab-ci.yml
is a yaml file and yaml has special parsings. Because of that in script:
ex. - echo bla
is the same as - 'echo bla'
, because in yaml the content of script:
is an array of strings that are later spitted by shell.
how to use variables outside and within scripts.
Normally like in any other shell script.
when to use each style
"SOME_VAR" #in quotes, no dollar sign SOME_VAR #ie without the double quotes, dollar sign, and curly braces
when you want to have a string SOME_VAR
literally
"${SOME_VAR}"
is the same as "$SOME_VAR"
. When you want to have the content of SOME_VAR
variable literally.
${SOME_VAR} #no quotes, with dollar sign and wrapped with curly braces $SOME_VAR #ie without the double quotes or curly braces
When you want the content of SOME_VAR
variable after word splitting and filename expansion . That means that SOME_VAR='*'
and then echo "$SOME_VAR"
will print *
, but echo $SOME_VAR
will print all files in current directory. You usually always want to quote expansions.
The form ${SOME_VAR}
is used if concatenated with some other string, ex. $SOME_VARbla
is not ${SOME_VAR}bla
.
Do not use upper case variables in your scripts - prefer lower case. Prefer using upper case variables for exported variables. Be aware of clashes - COLUMN
PATH
USER
UID
are examples of already used variables.
can I assign a variable in the script section with a bash command?
Shell is space aware . var = val
will execute a command named var
with two arguments =
and val
. var=val
will assign the string val
to variable named var
. Do:
- SOME_VAR=$(<file_with_a_line_of_text.txt)
In gitlab-ci I would prefer to use cat
in case I will want to move to alpine. $(<
is a bash extension.
- SOME_VAR=$(cat file_with_a_line_of_text.txt)
There doesn't seem to be any point in setting providing SOME_VAR
in environment with variables: SOME_VAR
.
When do I use '$' in front of the variable?
When you want to trigger variable expansion. Variable expansion substitutes variable name for the variable value.
Check your scripts with http://shellcheck.net . Read https://mywiki.wooledge.org/BashGuide a good shell introduction and https://wiki.bash-hackers.org/scripting/obsolete .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.