Search code examples
javajenkinsgroovyjenkins-pipelinejenkins-groovy

How to access parameters dynamically in groovy scripts


Basically, I have a scenario where I have some parameters loaded from a file and additional parameters using a loop:

pipeline {
    stages {
        stage("prepare") {
            steps {
                script {
                    def params = load("parmas.gy")
                    def yml = readYaml file: "platforms.yml"
                    def selectedJobsMap = yml.selected_jobs
                    def extra_parameters = []

                    selectedJobsMap.each { param ->
                        def platformChoices = selectedJobsMap[param.key].collect { it.name as String }
                        params.add (
                            extendedChoice(
                                name: "${param.key}_PLATFORMS",
                                description: "Select ${param.key} platforms that you want to run tests",
                                type: 'PT_CHECKBOX',
                                multiSelectDelimiter: ',',
                                value: platformChoices.join(',')
                                )
                            )
                           extra_parameters.add("${param.key}_PLATFORMS")
                    }
                    properties([parameters(params)])
                    
                    extra_parameters.each { platformParam ->
                        println("${platformParam}") // First print
                        println("${WINDOWS_PLATFORMS}") // Second print
                    }
                }
            }
        }
    }
}

My YML file is like this (it is huge but mocked it):

selected_jobs:
    Windows:
        - { name: 'windows10', job: 'app-test_windows10_x86_64_vm' }
        - { name: 'windows2019', job: 'app-test_windows2019_x86_64_vm' }
        - { name: 'windows2022', job: 'app-test_windows2022_x86_64_vm' }
    RHEL:
        - { name: 'rhel_7', job: 'app-test_rhel7_x64_docker' }
        - { name: 'rhel_8', job: 'app-test_rhel_8_arm64_docker' }

So, extra_parameters is a list which will be like this: ["WINDOWS_PLATFORMS", "RHEL_PLATFORMS"] and my problem is that the first print from groovy script is giving different results from the second print. Basically, the first print is printing "WINDOWS_PLATFORMS" as a string, and the second print is printing the correct value, which is the value which is stored behind WINDOWS_PLATFORMS parameter (which is the value that is selected in the Jenkins job).

Any idea how to fix it?

Basically, this doesn't print the same thing:

def x1 = "WINDOWS_PLATFORMS"
println("${WINDOWS_PLATFORMS}")
println("${x1}")

Solution

  • https://www.jenkins.io/doc/book/pipeline/syntax/#parameters

    The parameters directive provides a list of parameters that a user should provide when triggering the Pipeline. The values for these user-specified parameters are made available to Pipeline steps via the params object

    so, after

    parameters { 
      string(name: 'WINDOWS_PLATFORMS', defaultValue: 'staging', description: '')
    }
    

    you should be able to access parameter value through params variable:

    println params.WINDOWS_PLATFORMS
    println params['WINDOWS_PLATFORMS']
    //or dymamically
    def key="WINDOWS"
    println params["${key}_PLATFORMS"]
    

    i noticed that this is also working when using instead of params