0

I want to update a string in quotes in yaml file in Jenkins Job. While updating the file, single quotes around the string are replaced by triple quotes. Following is the method that I have written:

{  
def fileName = 'config.yml'
    datas = readYaml file: fileName
    var = "'" + params.ReleaseBranchName + "'"
    println var        // this shows output as expected, string in single quotes -> 'rel-21.9'
    datas.branchName = var
    println datas     // this prints the yaml with single quotes -> productiveBranch='rel-21.9',
    writeYaml file: fileName, data: datas, overwrite: true     //this show value in triple quotes -> productiveBranch: '''rel-21.9'''
    }

Could someone suggest how can I save string with single quotes in yaml file? Thanks!

Toto
  • 89,455
  • 62
  • 89
  • 125
  • Just use `var = params.ReleaseBranchName`. then `writeYaml` will apply quotes to your value if required. – daggett Jun 19 '21 at 03:37

1 Answers1

1

The value of var, as written, is 'rel-21.9', i.e. the single quotes are part of the value.

In YAML input, when 'rel-21.9' is encountered, the single quotes are not part of the value; they are part of the syntax and enclose the value, so the value is rel-21.9.

Therefore, if you want your value to be rel-21.9, which is most probably what you want, do not put single quotes in the value; just do var = params.ReleaseBranchName.

Your code does not do anything with var; I assume what you're trying is to put it into datas. This would result in YAML writing out "'rel-21.9'" (not triple single quotes, that can't happen since it would be invalid YAML). By surrounding the value with double quotes, the single quotes become part of the value just like your code requested.

When you do not put single quotes into the data, YAML will probably serialise it without any quotes. This is expected since rel-21.9 does not contain any special characters that would require quoting. There are ways to force a YAML processor to quote a value, but they are complex and I am unsure whether the API is exposed to Groovy. For references, this is how you would do it in Java.

Since you are editing a YAML file, you might want to read this question which details how and why updating YAML files in code can lead to style changes.

flyx
  • 35,506
  • 7
  • 89
  • 126
  • Hi, Thanks for your reply. I edited the question, while trimming the code, by mistake I missed the line where I am updating data with new value. While writing to yaml file, I want to have single quotes around the string. I also tried this ``` {.... var = "'" + params.ReleaseBranchName + "'" datas.branchName = "${var}" ...} ``` But also gives output with triple quotes. – asksomething Jun 18 '21 at 12:13
  • @asksomething Did you try to understand what I wrote? Your value is `rel-21.9` and putting single quotes in the value will change the value, which is not what you want. You want to change how YAML serialises the value by setting the quoting mechanism for that one value. This is simply not possible through the [Jenkins API](https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#writeyaml-write-a-yaml-from-an-object-or-objects) since it only exposes a single function that is too limited and as shown in the linked answer, you need deeper access to the SnakeYAML API to do it. – flyx Jun 18 '21 at 13:07