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

如何在for循环内的jenkins中构建json文件,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我试图在jenkins作业中构建json文件 我有一份回购协议列表,其中包含每个回购协议的分支机构列表。我想获取这些数据并用它构建一个json文件 最终结果应该是 [ { "name": "repoName", "branches" : [ "name" : "branchName", "name" : "branchName2" ] }, { "name": "repoName2

我试图在jenkins作业中构建json文件

我有一份回购协议列表,其中包含每个回购协议的分支机构列表。我想获取这些数据并用它构建一个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
         }
     }
 }
脚本{
节点{
取消“构建”
env.WORKSPACE=pwd()
def buildConfig=load“GenerateBuildSelections.Groovy”
def repos=buildConfig.GetListOfRepos(“${env.WORKSPACE}/repos.json”)
对于(i=0;i

我希望能够将每个带有分支列表的回购都保存到相同的json中。我不知道如何做到这一点,每次都要覆盖它。

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()
    }
}
}
管道{
任何代理人
舞台{
阶段('创建生成参数'){
台阶{
sh'echo!--设置----!'
git凭据ID:'',url:'https://github.com/GenesisGaming/DevOpsJenkins.git'
httpRequest acceptType:'APPLICATION_JSON',身份验证:“”,consoleLogResponseBody:false,contentType:'APPLICATION_JSON',outputFile:'Repos.JSON',responseHandle:'NONE',url:'https://api.github.com/search/code?q=org:GenesisGaming+文件名:Project.xml&per_page=100'
读取文件“Repos.json”
隐藏内容包括:'**',名称:'build'
剧本{
节点{
取消“构建”
env.WORKSPACE=pwd()
def buildConfig=load“GenerateBuildSelections.Groovy”
def repos=buildConfig.GetListOfRepos(“${env.WORKSPACE}/repos.json”)
def dataMap=newhashmap()
对于(i=0;i
我不明白你所说的“不覆盖构建器”是什么意思。你能包括你尝试的代码和产生的错误文件吗?问题是该文件只包含一个回购协议?如果是这样,我认为您需要初始化一个HashMap,在迭代时填充它,然后在最后将其转储到json