Derek Lawrence
Derek Lawrence

Reputation: 1608

How to build a json file in jenkins inside a for loop

Im trying to build a json file in a jenkins job.

I have a list of repos with a list of branches for each repo. I want to take this data and build a json file with it.

the end result should look like

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

repoName and branchName both come from vars.

my code looks like this

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

I want to be able to save each repo with the list of branches all to the same json. I cant figure out how to do this without overwriting it every time.

Upvotes: 2

Views: 3860

Answers (1)

Derek Lawrence
Derek Lawrence

Reputation: 1608

burnettk pointed me towards using a hashmap which worked perfectly. Here is the updated code that saves it to a 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()
    }
}
}

Upvotes: 1

Related Questions