Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
Container registry
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
9a2878d5
未验证
提交
9a2878d5
编辑于
1 year ago
作者:
Marcel Amirault
提交者:
GitLab
1 year ago
浏览文件
操作
下载
补丁
差异文件
Add troubleshooting details about quoting in CI
Quoting can be important for inputs and variable expressions
上级
c54b5454
No related branches found
No related tags found
无相关合并请求
变更
2
隐藏空白变更内容
行内
左右并排
显示
2 个更改的文件
doc/ci/jobs/job_control.md
+46
-0
46 个添加, 0 个删除
doc/ci/jobs/job_control.md
doc/ci/yaml/inputs.md
+63
-3
63 个添加, 3 个删除
doc/ci/yaml/inputs.md
有
109 个添加
和
3 个删除
doc/ci/jobs/job_control.md
+
46
−
0
浏览文件 @
9a2878d5
...
...
@@ -1218,3 +1218,49 @@ a branch that has an open merge request associated with it.
To
[
prevent duplicate pipelines
](
#avoid-duplicate-pipelines
)
, use
[
`workflow: rules`
](
../yaml/index.md#workflow
)
or rewrite your rules to control
which pipelines can run.
### `This GitLab CI configuration is invalid` for variable expressions
You might receive one of several
`This GitLab CI configuration is invalid`
errors
when working with
[
CI/CD variable expressions
](
#cicd-variable-expressions
)
.
These syntax errors can be caused by incorrect usage of quote characters.
In variable expressions, strings should be quoted, while variables should not be quoted.
For example:
```
yaml
variables
:
ENVIRONMENT
:
production
job
:
script
:
echo
rules
:
-
if
:
$ENVIRONMENT == "production"
-
if
:
$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
```
In this example, both
`if:`
clauses are valid because the
`production`
string is quoted,
and the CI/CD variables are unquoted.
On the other hand, these
`if:`
clauses are all invalid:
```
yaml
variables
:
ENVIRONMENT
:
production
job
:
script
:
echo
rules
:
# These rules all cause YAML syntax errors:
-
if
:
${ENVIRONMENT} == "production"
-
if
:
"
$ENVIRONMENT"
== "production"
-
if
:
$ENVIRONMENT == production
-
if
:
"
production"
== "production"
```
In this example:
-
`if: ${ENVIRONMENT} == "production"`
is invalid, because
`${ENVIRONMENT}`
is not valid
formatting for CI/CD variables in
`if:`
.
-
`if: "$ENVIRONMENT" == "production"`
is invalid, because the variable is quoted.
-
`if: $ENVIRONMENT == production`
is invalid, because the string is not quoted.
-
`if: "production" == "production"`
is invalid, because there is no CI/CD variable to compare.
此差异已折叠。
点击以展开。
doc/ci/yaml/inputs.md
+
63
−
3
浏览文件 @
9a2878d5
...
...
@@ -233,11 +233,18 @@ Only variables you can [use with the `include` keyword](includes.md#use-variable
Example:
```
yaml
$[[ inputs.test | expand_vars ]]
spec
:
inputs
:
test
:
default
:
'
test
$MY_VAR'
---
test-job
:
script
:
echo $[[ inputs.test | expand_vars ]]
```
Assuming the value of
`inputs.test`
is
`test $MY_VAR`
, and the variable
`$MY_VAR`
is unmasked
w
ith a value of
`my value`
, then the output would be
`test my value`
.
In this example, if
`$MY_VAR`
is unmasked (exposed in job logs) with a value of
`my value`
, then the input
w
ould expand to
`test my value`
.
#### `truncate`
...
...
@@ -259,3 +266,56 @@ $[[ inputs.test | truncate(3,5) ]]
```
Assuming the value of
`inputs.test`
is
`0123456789`
, then the output would be
`34567`
.
## Troubleshooting
### YAML syntax errors when using `inputs`
[
CI/CD variable expressions
](
../jobs/job_control.md#cicd-variable-expressions
)
in
`rules:if`
expect a comparison of a CI/CD variable with a string, otherwise
[
a variety of syntax errors could be returned
](
../jobs/job_control.md#this-gitlab-ci-configuration-is-invalid-for-variable-expressions
)
.
You must ensure that expressions remain properly formatted after input values are
inserted into the configuration, which might require the use of additional quote characters.
For example:
```
yaml
spec
:
inputs
:
branch
:
default
:
$CI_DEFAULT_BRANCH
---
job-name
:
rules
:
-
if
:
$CI_COMMIT_REF_NAME == $[[ inputs.branch ]]
```
In this example:
-
Using
`include: inputs: branch: $CI_DEFAULT_BRANCH`
is valid. The
`if:`
clause evaluates to
`if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH`
, which is a valid variable expression.
-
Using
`include: inputs: branch: main`
is
**invalid**
. The
`if:`
clause evaluates to
`if: $CI_COMMIT_REF_NAME == main`
, which is invalid because
`main`
is a string but is not quoted.
Alternatively, add quotes to resolve some variable expression issues. For example:
```
yaml
spec
:
inputs
:
environment
:
default
:
"
$ENVIRONMENT"
---
$[[ inputs.environment | expand_vars ]] job
:
script
:
echo
rules
:
-
if
:
'
"$[[
inputs.environment1
|
expand_vars
]]"
==
"production"'
```
In this example, quoting the input block and also the entire variable expression
ensures valid
`if:`
syntax after the input is evaluated. The internal and external quotes
in the expression must not be the same character. You can use
`"`
for the internal quotes
and
`'`
for the external quotes, or the inverse. On the other hand, the job name does
not require any quoting.
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录