2017-06-07 108 views
1

我試圖在jenkins作業中構建json文件。如何在for循環內jenkins中創建json文件

我有一個回購清單和每個回購分行清單。我想獲取這些數據並用它構建一個json文件。

最終的結果應該像

[ 
    { 
    "name": "repoName", 
    "branches" : 
     [ 
     "name" : "branchName", 
     "name" : "branchName2" 
     ] 
    }, 
    { 
    "name": "repoName2", 
    "branches" : 
     [ 
     "name" : "branchName", 
     "name" : "branchName2", 
     "name" : "branchName3", 
     ] 
    } 
] 

repoName和BRANCHNAME都來自瓦爾。

我的代碼看起來像這樣

script { 
    node{ 
     unstash 'build' 
     env.WORKSPACE = pwd() 
     def buildConfig = load "GenerateBuildSelections.Groovy" 
     def repos = buildConfig.GetListOfRepos("${env.WORKSPACE}/Repos.json") 

     for(i = 0; i < repos.size(); i++){ 
      def repoName = repos[i] 
      httpRequest acceptType: 'APPLICATION_JSON', authentication: '********-****-****-****-************', consoleLogResponseBody: true, contentType: 'APPLICATION_JSON', outputFile: "branches_${repoName}.json", responseHandle: 'NONE', url: "https://api.github.com/repos/GenesisGaming/${repoName}/branches" 
      env.WORKSPACE = pwd() 
      def branches = buildConfig.GetListOfBranches("${env.WORKSPACE}/branches_${repoName}.json") 

      //How do I save the Repo name with the branches here without overwriting the builder everytime 
     } 
    } 
} 

我希望能夠給每個回購保存分支機構相同的JSON列表。我不知道如何做到這一點,而不是每次都覆蓋它。

+0

我不明白你的意思是「沒有改寫建造者」。你能否包括你試過的代碼和產生的錯誤文件?是文件只包含一個回購的問題?如果是這樣,我認爲你需要初始化一個HashMap,在迭代時填充它,然後在最後將它轉儲到json。 – burnettk

+0

謝謝,這有助於我能夠使用hashmap –

回答

1

burnettk指出我使用完美運行的散列表。這是更新的代碼,將其保存到json。

pipeline { 
agent any 

stages { 
    stage ('Create Build Parameters'){ 
     steps{ 
      sh 'echo !---SETUP---!' 
      git credentialsId: '', url: 'https://github.com/GenesisGaming/DevOpsJenkins.git' 
      httpRequest acceptType: 'APPLICATION_JSON', authentication: '', consoleLogResponseBody: false, contentType: 'APPLICATION_JSON', outputFile: 'Repos.json', responseHandle: 'NONE', url: 'https://api.github.com/search/code?q=org:GenesisGaming+filename:Project.xml&per_page=100' 
      readFile 'Repos.json' 
      stash includes: '**', name: 'build' 
      script { 
       node{ 
        unstash 'build' 
        env.WORKSPACE = pwd() 
        def buildConfig = load "GenerateBuildSelections.Groovy" 
        def repos = buildConfig.GetListOfRepos("${env.WORKSPACE}/Repos.json") 

        def dataMap = new HashMap<String,List>() 
        for(i = 0; i < repos.size(); i++){ 
         def repoName = repos[i] 
         httpRequest acceptType: 'APPLICATION_JSON', authentication: '', consoleLogResponseBody: false, contentType: 'APPLICATION_JSON', outputFile: "branches_${repoName}.json", responseHandle: 'NONE', url: "https://api.github.com/repos/GenesisGaming/${repoName}/branches" 
         env.WORKSPACE = pwd() 
         def branches = buildConfig.GetListOfBranches("${env.WORKSPACE}/branches_${repoName}.json") 
         dataMap.put("${repoName}", "${branches}") 

        } 
        def builder = new groovy.json.JsonBuilder(dataMap) 
        new File("/home/service/BuildSelectionOptions/options.json").write(builder.toPrettyString()) 
       } 
      } 
     } 
    } 
} 
post { 
    always { 
     sh 'echo !---Cleanup---!' 
     cleanWs() 
    } 
} 
}