Passing maps into Jenkins pipeline jobs Passing maps into Jenkins pipeline jobs jenkins jenkins

Passing maps into Jenkins pipeline jobs


I've come up with a hacky workaround using the pipeline-utility-steps plugin.

Essentially I pass my data maps around as JSON.

So my seed job might contain:

def SERVERS_MAP = ''' {  "prod": [    "prod-server1",    "prod-server2"  ],  "dev": [    "dev-server1",    "dev-server2"  ]}'''pipelineJob("job1") {    definition {        cps {            script(readFileFromWorkspace('job1.groovy'))        }        parameters {            choiceParam('ENV', ['dev', 'prod'], 'Build Environment')            stringParam('SERVERS_MAP', "${SERVERS_MAP}", "")         }    }}

and my pipeline would contain something like:

def serversMap = readJSON text: SERVERS_MAPdef targetServers = serversMap["${ENV}"]targetServers.each { server ->   echo server}

I could also extract these variables into a JSON file and read them from there.

Although it works, it feels wrong somehow.


You can use string parameter pass the Map val, downstream read it as json format.

UPSTREAM PIPELINE

timestamps{node("sse_lab_CI_076"){ //${execNode}    currentBuild.description="${env.NODE_NAME};"    stage("-- regression execute --"){        def test_map =         """            {                "gerrit_patchset_commit": "aad5fce",                "build_cpu_x86_ubuntu":     [                    "centos_compatible_build_test",                    "gdb_compatible_build_test",                    "visual_profiler_compatible_build_test"                ],            }        """        build(job: 'tops_regression_down',            parameters: [string(name: 'UPSTREAM_JOB_NAME',                                value: "${env.JOB_BASE_NAME}"),                        string(name: 'UPSTREAM_BUILD_NUM',                                value: "${env.BUILD_NUMBER}"),                        string(name: 'MAP_PARAM',                                value: "${test_map}"),                        ],            propagate: true,            wait: true)    }}

}

DOWNSTREAM PIPELINE

timestamps{node("sse_lab_inspur_076"){ //${execNode}    currentBuild.description="${env.NODE_NAME};"    stage('--in precondition--'){        dir('./'){            cleanWs()            println("hello world")            println("${env.MAP_PARAM}")            Map result_json = readJSON(text: "${env.MAP_PARAM}")            println(result_json)        }    }}

}