【问题标题】:Jenkins: extended choice parameter - groovy - how to create file on the masterJenkins:扩展选择参数——groovy——如何在master上创建文件
【发布时间】:2015-10-16 20:06:44
【问题描述】:

我在“扩展选择参数”插件的 groovy 脚本部分定义了一个 json 字符串。此外,我想将 json 配置写入 groovy 脚本区域内主端的文件中。我想,也许工作目录是最好的地方?

http://hudson/hudson/job/MY_JOB/config.json

如果你现在问,我为什么要这样做;背后的原因是,我不希望配置预先保存在其他地方。我不喜欢在作业配置之外配置文件的想法。我想在一个地方查看/调整配置 - 在作业配置中。

我需要 json 配置中的许多其他信息,以便以后在同一作业的 python 代码部分中使用。

我的问题是:

  • 我在这里走错路了吗?有什么建议吗?
  • 我可以直接在master端写json配置吗?它不一定是 jenkins 工作目录。我不关心设备/目录。
  • 如果方法可以接受,我该怎么做?

以下代码不起作用:

def filename = "config.json"
def targetFile = new File(filename)

if (targetFile.createNewFile()) {
  println "Successfully created file $targetFile"    
} else {
  println "Failed to create file $targetFile"
}

备注:

hudson.FilePath 看起来很有趣!

http://javadoc.jenkins-ci.org/hudson/FilePath.html

感谢您的帮助,西蒙

【问题讨论】:

    标签: groovy jenkins


    【解决方案1】:

    我明白了:

    import groovy.json.*
    
    // location on the master : /srv/raid1/hudson/jobs
    jsonConfigFile = new File("/srv/raid1/hudson/jobs/MY_JOB/config.json")
    jsonConfigFileOnMaster = new hudson.FilePath( jsonConfigFile )
    
    if( jsonConfigFileOnMaster.exists() ){
      jsonConfigFileOnMaster.delete()
    }
    
    jsonConfigFileOnMaster.touch( System.nanoTime())
    
    jsonFormatted = JsonOutput.toJson( localJsonString )
    
    jsonConfigFile.write jsonFormatted 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多