简体   繁体   English

如何使用 Pipeline Utility Steps 插件更新现有 yaml 文件的内容

[英]How to update the content of an existing yaml file with Pipeline Utility Steps plugin

In my jenkins pipeline I am reading data stored in yaml file using Pipeline Utility Steps plugin在我的 jenkins 管道中,我正在使用Pipeline Utility Steps插件读取存储在 yaml 文件中的数据

I can read data from file, now I want to update the value and write it back to the file, like this:我可以从文件中读取数据,现在我想更新值并将其写回文件,如下所示:

pipeline {
agent any

stages {

    stage('JOb B ....'){
        steps{
            script{
               def datas = readYaml file:"${WORKSPACE}/Version.yml"
               echo datas.MAJOR_VERSION //output is 111

               datas = ['MAJOR_VERSION': '222']
               writeYaml file:"${WORKSPACE}/Version.yml", data: datas
            }
        }//steps
    }//stage

}//stages

}//pipeline

But I am getting error - Version.yml already exist:但我收到错误 - Version.yml 已经存在:

java.nio.file.FileAlreadyExistsException: /var/lib/jenkins/workspace/t-cicd-swarm-example_hdxts-job-B/Version.yml already exist.
at org.jenkinsci.plugins.pipeline.utility.steps.conf.WriteYamlStep$Execution.run(WriteYamlStep.java:175)
at org.jenkinsci.plugins.pipeline.utility.steps.conf.WriteYamlStep$Execution.run(WriteYamlStep.java:159)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

It seems it can only write a new file and it cannot overwrite the existing file.它似乎只能写入一个新文件,而不能覆盖现有文件。 How to update the content of an existing yaml file from my script shown above?如何从上面显示的脚本更新现有 yaml 文件的内容?

It looks like you need to delete or rename the original file before you overwrite it because the writeYaml method doesn't have an overwrite flag.看起来您需要在覆盖之前删除或重命名原始文件,因为 writeYaml 方法没有覆盖标志。

sh '''
  if [ -e Version.yaml ]; then
    rm -f Version.yaml
  fi
'''

According to the latest documentation.根据最新文档。 There is a parameter you can use to overwrite the content of designated file:有一个参数可以用来覆盖指定文件的内容:

writeYaml: Write a yaml from an object.
...
overwrite (optional): Allow existing files to be overwritten. Defaults to false.

Please refer to: https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#writeyaml-write-a-yaml-from-an-object请参考: https : //www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#writeyaml-write-a-yaml-from-an-object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM