如何在for循环中的jenkins中构建json文件

时间:2017-06-07 00:08:27

标签: jenkins groovy jenkins-pipeline

我试图在jenkins工作中构建一个json文件。

我有一个repos列表,其中包含每个repo的分支列表。我想获取这些数据并用它构建一个json文件。

最终结果应该是

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

repoName和branchName都来自vars。

我的代码看起来像这样

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

我希望能够将每个包含所有分支列表的repo保存到同一个json中。我不知道如何做到这一点,而不是每次都覆盖它。

1 个答案:

答案 0 :(得分:1)

burnettk指出我要使用一个完美运行的hashmap。这是更新的代码,将其保存到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()
    }
}
}