简体   繁体   English

如何在 gitlab ci/cd 中使用自定义变量?

[英]How to use custom variables in gitlab ci/cd?

I'm struggling with gitlab ci/cd variables.我正在为 gitlab ci/cd 变量苦苦挣扎。 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?例如,在作业配置中,我可以使用 bash 命令在脚本部分分配一个变量吗?

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.而且我在网上找不到一个使用 bash 命令在脚本中设置自定义变量的示例。

When I'm setting variables in bash, I always do it without the spaces around the = :当我在 bash 中设置变量时,我总是在=周围没有空格的情况下这样做:

VAR1="some string"
VAR2=23
VAR3=true
VAR4=$(cat /path/to/file.txt)

Let's go through these examples one at a time:让我们一次一个地通过这些示例 go:

  1. You can set a variable as a string by using quotes around the string.您可以通过在字符串周围使用引号将变量设置为字符串。
  2. You can set it to an int (probably a float too, but haven't personally used it)您可以将其设置为 int (也可能是浮点数,但没有亲自使用过)
  3. You can set it to a bool您可以将其设置为布尔值
  4. You can set it to the output of a command.您可以将其设置为命令的 output。 The command is inside the command: $(#command) .该命令在命令内部: $(#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 .这说明了不同 forms 之间的区别,但通常,您使用$+variable-name引用变量的值。 Doing "SOME_VAR" or SOME_VAR just prints out the string "SOME_VAR" (ie, not referencing a variable at all).执行"SOME_VAR"SOME_VAR只会打印出字符串“SOME_VAR”(即,根本不引用变量)。

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. $SOME_VAR${SOME_VAR}之间的区别在于后者允许您在变量之前或之后直接有其他内容时使用它而不会出错。

How to use custom variables in gitlab ci/cd?如何在 gitlab ci/cd 中使用自定义变量?

Normally like in any other shell.通常与任何其他 shell 一样。

But note that gitlab-ci.yml is a yaml file and yaml has special parsings.但请注意gitlab-ci.yml是一个 yaml 文件,并且 yaml 有特殊的解析。 Because of that in script: ex.因为在script:例如。 - 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. - echo bla- 'echo bla'相同,因为在 yaml 中script:的内容是一个字符串数组,稍后由 shell 吐出。

how to use variables outside and within scripts.如何在脚本内外使用变量。

Normally like in any other shell script.通常就像在任何其他 shell 脚本中一样。

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字面意思

"${SOME_VAR}"

is the same as "$SOME_VAR" ."$SOME_VAR"相同。 When you want to have the content of SOME_VAR variable literally.当您想从字面上SOME_VAR变量的内容时。

 ${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 .当您想要分词和文件名扩展后SOME_VAR变量的内容时。 That means that SOME_VAR='*' and then echo "$SOME_VAR" will print * , but echo $SOME_VAR will print all files in current directory.这意味着SOME_VAR='*'然后echo "$SOME_VAR"将打印* ,但echo $SOME_VAR将打印当前目录中的所有文件。 You usually always want to quote expansions.你通常总是想引用扩展。

The form ${SOME_VAR} is used if concatenated with some other string, ex.如果与其他字符串连接,则使用${SOME_VAR}形式,例如。 $SOME_VARbla is not ${SOME_VAR}bla . $SOME_VARbla不是${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.请注意冲突 - COLUMN PATH USER UID是已使用变量的示例。

can I assign a variable in the script section with a bash command?我可以使用 bash 命令在脚本部分分配一个变量吗?

Shell is space aware . Shell 具有空间意识 var = val will execute a command named var with two arguments = and val . var = val执行一个名为var的命令,其中包含两个 arguments =val var=val will assign the string val to variable named var . var=val会将字符串val分配给名为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.在 gitlab-ci 中,我更喜欢使用cat以防我想搬到高山。 $(< is a bash extension. $(<是 bash 扩展。

- 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 .在环境中设置提供SOME_VAR 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 .使用http://shellcheck.net检查您的脚本。 Read https://mywiki.wooledge.org/BashGuide a good shell introduction and https://wiki.bash-hackers.org/scripting/obsolete .阅读https://mywiki.wooledge.org/BashGuide一个很好的 shell 介绍和https://wiki.bash-hackers.org/scripting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM