检查 Gitlab 管道中的变量是否为空
Checking if variable is null in Gitlab pipeline
当 Gitlab 管道中的空变量声明为另一个空变量的内容时,如何检查它?就像下面的变量 VAR_NULL
当 NO_VAR
为 null 时:
variables:
VAR_EMPTY: ""
VAR_NULL: "${NO_VAR}"
检查管道结果,其中只有 VAR_EMPTY == ""
和 NO_VAR == null
的计算结果为 true,其他均为 false .
管道结果(为方便起见截图,完整结果:https://gitlab.com/labaz/test-gitlab-pipeline-null-var/-/pipelines/493036820):
完整管道脚本 (https://gitlab.com/labaz/test-gitlab-pipeline-null-var/-/blob/main/.gitlab-ci.yml):
variables:
VAR_EMPTY: ""
VAR_NULL: "${NO_VAR}"
jobTest-Var_Empty-IsNull: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_EMPTY == null'
script:
- 'echo "VAR_EMPTY IS null"'
jobTest-Var_Empty-IsEmpty: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_EMPTY == ""'
script:
- 'echo "VAR_EMPTY IS \"\""'
jobTest-Var_Null-IsNull: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_NULL == null'
script:
- 'echo "VAR_NULL IS null"'
jobTest-Var_Null-IsEmpty: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_NULL == ""'
script:
- 'echo "VAR_NULL IS Empty"'
jobTest-No_Var-IsNull: # This job runs in the build stage, which runs first.
rules:
- if: '$NO_VAR == null'
script:
- 'echo "NO_VAR IS null"'
jobTest_No_Var-IsEmpty: # This job runs in the build stage, which runs first.
rules:
- if: '$NO_VAR == ""'
script:
- 'echo "NO_VAR IS Empty"'
您遇到的问题是 VAR_NULL: "${NO_VAR}"
不是空变量。它实际上与 VAR_EMPTY: ""
相同——您用空值声明变量(因此它不为空)。
测试变量是否是用另一个空变量创建的唯一方法是测试原始变量本身。即测试NO_VAR
,而不是VAR_NULL
.
另一种策略是使用 rules:variables:
以有条件地声明 VAR_NULL
workflow:
rules:
- if: '$NO_VAR' # or '$NO_VAR != null' depending on what you want
variables:
VAR_NULL: "$NO_VAR"
- when: always
jobTest-Var_Null-IsNull:
rules:
- if: '$VAR_NULL == null'
script:
- echo "VAR_NULL is null"
当 Gitlab 管道中的空变量声明为另一个空变量的内容时,如何检查它?就像下面的变量 VAR_NULL
当 NO_VAR
为 null 时:
variables:
VAR_EMPTY: ""
VAR_NULL: "${NO_VAR}"
检查管道结果,其中只有 VAR_EMPTY == ""
和 NO_VAR == null
的计算结果为 true,其他均为 false .
管道结果(为方便起见截图,完整结果:https://gitlab.com/labaz/test-gitlab-pipeline-null-var/-/pipelines/493036820):
完整管道脚本 (https://gitlab.com/labaz/test-gitlab-pipeline-null-var/-/blob/main/.gitlab-ci.yml):
variables:
VAR_EMPTY: ""
VAR_NULL: "${NO_VAR}"
jobTest-Var_Empty-IsNull: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_EMPTY == null'
script:
- 'echo "VAR_EMPTY IS null"'
jobTest-Var_Empty-IsEmpty: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_EMPTY == ""'
script:
- 'echo "VAR_EMPTY IS \"\""'
jobTest-Var_Null-IsNull: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_NULL == null'
script:
- 'echo "VAR_NULL IS null"'
jobTest-Var_Null-IsEmpty: # This job runs in the build stage, which runs first.
rules:
- if: '$VAR_NULL == ""'
script:
- 'echo "VAR_NULL IS Empty"'
jobTest-No_Var-IsNull: # This job runs in the build stage, which runs first.
rules:
- if: '$NO_VAR == null'
script:
- 'echo "NO_VAR IS null"'
jobTest_No_Var-IsEmpty: # This job runs in the build stage, which runs first.
rules:
- if: '$NO_VAR == ""'
script:
- 'echo "NO_VAR IS Empty"'
您遇到的问题是 VAR_NULL: "${NO_VAR}"
不是空变量。它实际上与 VAR_EMPTY: ""
相同——您用空值声明变量(因此它不为空)。
测试变量是否是用另一个空变量创建的唯一方法是测试原始变量本身。即测试NO_VAR
,而不是VAR_NULL
.
另一种策略是使用 rules:variables:
以有条件地声明 VAR_NULL
workflow:
rules:
- if: '$NO_VAR' # or '$NO_VAR != null' depending on what you want
variables:
VAR_NULL: "$NO_VAR"
- when: always
jobTest-Var_Null-IsNull:
rules:
- if: '$VAR_NULL == null'
script:
- echo "VAR_NULL is null"