You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jenkinsfile填充YAML时变量格式错误,如何修正为单引号格式?

Fix: Jenkins writeYaml Producing Triple Single Quotes in YAML

Hey there, let's break down why your YAML is ending up with those triple single quotes and how to fix it.

The root issue is how you're assigning Jenkins parameters to your YAML values—wrapping ${params.NAME} and ${params.CODE} in double quotes triggers extra escaping in the writeYaml step, which results in those unwanted triple quotes. Here are two simple solutions:

Option 1: Assign Params Directly (Skip Unnecessary Interpolation)

Since params.NAME and params.CODE are already string values, you don't need to wrap them in double quotes for interpolation. Just assign them directly to the YAML structure:

script {
    def filename = "configuration.yml"
    def yaml = readYaml file: filename
    // Assign params without string interpolation quotes
    yaml.global.name = params.NAME
    yaml.global.code = params.CODE
    // ...
    sh "rm $filename"
    writeYaml file: filename, data: yaml
}

This tells writeYaml to write the raw parameter values, avoiding any extra escaping that creates triple quotes.

Option 2: Explicitly Define Quoting Style

If you want more control over how strings are formatted in the YAML, use the quotingStyle parameter in writeYaml to force single-quoted strings:

script {
    def filename = "configuration.yml"
    def yaml = readYaml file: filename
    yaml.global.name = "${params.NAME}"
    yaml.global.code = "${params.CODE}"
    // ...
    sh "rm $filename"
    // Enforce single quotes for all string values
    writeYaml file: filename, data: yaml, quotingStyle: 'SINGLE_QUOTED'
}

This parameter ensures every string in the generated YAML uses single quotes instead of the escaped triple quotes.

Expected Result

After applying either fix, your configuration.yml will match your desired format perfectly:

global:
  name: 'my_name'
  code: 'my_code'

内容的提问来源于stack exchange,提问作者Juliatzin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:23:29