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

u84*_*six 16 bash shell gitlab gitlab-ci

我正在与 gitlab ci/cd 变量作斗争。我看到很多相互矛盾的例子。无论如何,我想知道的是如何在脚本外部和内部使用变量。

例如,在作业配置中,我可以使用 bash 命令在脚本部分分配变量吗?

some-job:
   variables:
      SOME_VAR: ''
   script:
      - SOME_VAR = $(<file_with_a_line_of_text.txt)
Run Code Online (Sandbox Code Playgroud)

在上述情况下,我不确定我是否可以做到这一点。但我需要用文件内容(即工件)填充变量。另外,什么时候在变量前面使用“$”?我看到使用这些格式的一些示例:

"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
Run Code Online (Sandbox Code Playgroud)

我可以在示例中看到如此多的用法变化,但实际上不知道何时使用每种样式。我无法在网上找到使用 bash 命令在脚本中设置自定义变量的示例。

Ada*_*all 10

当我在 bash 中设置变量时,我总是在周围不加空格=

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

让我们一次一个地看一下这些例子:

  1. 您可以通过在字符串周围使用引号将变量设置为字符串。
  2. 你可以将它设置为 int (也可能是 float,但没有亲自使用它)
  3. 您可以将其设置为布尔值
  4. 您可以将其设置为命令的输出。该命令位于命令: 内$(#command)

现在让我们使用它们:

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
Run Code Online (Sandbox Code Playgroud)

这说明了不同形式之间的差异,但一般来说,您可以使用 引用变量的值$+variable-name。执行"SOME_VAR"SOME_VAR仅打印出字符串“SOME_VAR”(即根本不引用变量)。

$SOME_VAR和之间的区别${SOME_VAR}在于,后者允许您在变量之前或之后直接有其他内容时使用它,而不会出错。


Kam*_*Cuk 6

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

通常就像在任何其他 shell 中一样。

但请注意,这gitlab-ci.yml是一个 yaml 文件,并且 yaml 有特殊的解析。因为在前script:- echo bla与 相同- 'echo bla',因为在 yaml 中, 的内容script:是稍后由 shell 吐出的字符串数组。

如何在脚本外部和内部使用变量。

通常与任何其他 shell 脚本一样。

何时使用每种样式

"SOME_VAR" #in quotes, no dollar sign
SOME_VAR #i.e. without the double quotes, dollar sign, and curly braces
Run Code Online (Sandbox Code Playgroud)

当你想要一个SOME_VAR字面上的字符串时

"${SOME_VAR}"
Run Code Online (Sandbox Code Playgroud)

是相同的"$SOME_VAR"。当你想从字面上获得变量的内容SOME_VAR时。

${SOME_VAR} #no quotes, with dollar sign and wrapped with curly braces
$SOME_VAR #i.e. without the double quotes or curly braces
Run Code Online (Sandbox Code Playgroud)

当您想要分词和文件名扩展后SOME_VAR的变量内容时。这意味着then将 print ,但将打印当前目录中的所有文件。您通常总是想引用扩展。SOME_VAR='*'echo "$SOME_VAR"*echo $SOME_VAR

${SOME_VAR}如果与其他字符串连接,例如,则使用该形式。$SOME_VARbla不是${SOME_VAR}bla

不要在脚本中使用大写变量 - 最好使用小写变量。优先使用大写变量作为导出变量。注意冲突 -COLUMN PATH USER UID是已使用变量的示例。

我可以使用 bash 命令在脚本部分分配变量吗?

Shell 具有空间感知能力var = val将执行一个以两个参数和命名的命令var。会将字符串分配给名为 的变量。做:=valvar=valvalvar

- SOME_VAR=$(<file_with_a_line_of_text.txt)
Run Code Online (Sandbox Code Playgroud)

cat在 gitlab-ci 中,如果我想迁移到 alpine,我更愿意使用它。$(<是 bash 扩展。

- SOME_VAR=$(cat file_with_a_line_of_text.txt)
Run Code Online (Sandbox Code Playgroud)

SOME_VAR在环境中提供设置似乎没有任何意义variables: SOME_VAR

什么时候在变量前面使用“$”?

当你想触发变量扩展时。变量扩展用变量名代替变量值。

使用http://shellcheck.net检查您的脚本。阅读https://mywiki.wooledge.org/BashGuide一个很好的 shell 介绍和https://wiki.bash-hackers.org/scripting/obsolete

  • `我现在遇到的困难是我声明了 var,在工作中更改它(我也回显它以查看它已更改),然后当我在同一阶段的另一个工作中使用它时,它具有原始的值而不是新的`使用工件在阶段之间传输上下文。在 bash 中,您可以使用“declare -p”来序列化变量值,然后只需“source”文件来反序列化其值。 (2认同)