2015-10-16 29 views
0

我有一個json字符串定義在'擴展選擇參數'插件的groovy腳本部分。另外,我想在groovy腳本區域內的master側的文件中寫入json配置。我想,也許工作目錄將是最好的地方?詹金斯:擴展的選擇參數 - 常規 - 如何創建主文件

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

如果你現在問,爲什麼我應該這樣做;背後的原因是,我不希望配置預先保存在其他地方。我不喜歡在作業配置之外配置文件的想法。我想在一個地方看到/調整配置 - 在作業配置中。

我需要json配置中的許多其他信息,以便以後在同一作業中的Python代碼部分使用。

我的問題是:

  • 我在這裏下一個錯誤的道路?有什麼建議麼?
  • 我可以直接在主端寫入json配置嗎?它不一定是詹金斯的工作目錄。我不在乎設備/目錄。
  • 如果方法可以接受,我該怎麼做?

下面的代碼doesn't工作:

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

感謝您的幫助,西蒙

回答

0

我懂了:

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