'Pass Jenkins build parameters to pipeline nodes

I created a new Jenkins pipeline. The pipeline is (currently) parametrized with a single boolean option named VAR_A. My pipeline script is:

node ('windows') {
    echo "$VAR_A"
    bat 'env'
}

When I manually build the project with VAR_A checked, "true" is echoed, as expected. The list of environment variables, however, does not show VAR_A=true.

I am able to get env to show VAR_A if I wrap the call in a withEnv block:

node ('windows') {
    echo "$VAR_A"
    withEnv(["VAR_A=$VAR_A"]) {
        bat 'env'
    }
}

I will more parameters than this, so specifying each parameter individually is not desired. Is there a way to transfer all build parameters to a node's environment?



Solution 1:[1]

Passing the node dynamically in the build job step is a good feature. It helps to run multiple jobs parallelly on different nodes. The addition of the following script in the Jenkins pipeline script might be helpful.

def build_one() 
{
        parallel  one: { 
            stage('XYZ') {
                        catchError(buildResult: 'SUCCESS', stageResult:'FAILURE') {
                        
 build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['nodeName'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XX"), string(name: 'Browser', value: 'chrome'), string(name: 'Environment', value: 'envName')]
                        }
            }
        },
        two : { 
            stage('SecondArea') {
                        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                         build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['Your'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XYX"), string(name: 'Browser', value: 'firefox'), string(name: 'Environment', value: 'envName')]
                        }
            }
        }
        
} 


build_one()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mauryaAjay