使用Jenkinsfile填充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

