2017-06-07 6 views
1

jenkinsジョブでjsonファイルを作成しようとしています。forループ内のjenkinsでjsonファイルを作成する方法

私は各リポジトリのブランチのリストを持つreposのリストを持っています。私はこのデータを取得し、それを使ってjsonファイルを作成したいと思います。

最終結果は

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

repoNameのように見えるとbranchName両方VARSから来る必要があります。

私のコードは、私が同じJSONに枝のすべてのリストを各レポを保存できるようにしたい、この

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 
     } 
    } 
} 

のように見えます。私は毎回それを上書きすることなくこれを行う方法を理解できません。

+0

"ビルダーを上書きしない"という意味を理解できません。試したコードとその結果として間違ったファイルを含めることはできますか?ファイルに1つのレポだけが含まれているという問題はありますか?もしそうなら、あなたはHashMapを初期化し、反復しながらそれを移入し、最後にそれをjsonにダンプする必要があると思います。 – burnettk

+0

ありがとうございました。私はハッシュマップを使って作業することができました –

答えて

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() 
    } 
} 
}