In Jenkins how to pass a parameter from Pipeline job to a freestyle job

I am running a pipeline job and with this we need to pass a parameter to a downsteam job but its not working. We tried as follows:

Pipeline JOB:

node {
    parameters {
            choice(
                name: 'OS',
                choices:"Windows\nLinux\nMAC",
                description: "Choose Environment to build!")
                }
    stage('Build') {
        if("${params.Environment}" == 'Windows')
        {
       paramAValue = "${params.Environment}"
       build job: 'QA-Test-Windows',parameters: [[$class: 'StringParameterValue', name: 'ParamA', value: "$paramAValue"]]
        }
    }
    }

QA-Test-Windows is a Freestyle job and in that we tried accessing the parameter in script as follows but its not working.

Write-output "OS selected for testing is ${params.ParamA}"

Write-output "OS selected for testing is ${ParamA}"

Tried accessing variables but its not working. Can anyone please help me on this. We tried creating QA-Test-Windows freestyle job as Pipelinejob but in this freestyle there are lot of Build steps.


Solution 1:

ON THE CALLING JOB:

pipeline {
    agent any

    parameters {
        string(defaultValue: "123", description: 'This is a parameter', name: 'PARAMETER01')
    }

    stages {
        stage('Start'){
            steps{
                    build job: 'ANOTHER_JOB_NAME', wait: false, parameters: [string(name: 'HELLO', value: String.valueOf(PARAMETER01))]
            }
        }
    }
}

ON THE SECOND JOB:

pipeline {
    agent any

    parameters {
        string(defaultValue: "", description: 'K', name: 'HELLO')
    }

    stages {
        stage('PrintParameter'){
            steps{
                sh 'echo ${HELLO}'
            }
        }
    }
}

Solution 2:

I'm not sure what exactly wrong in your code, looks like there is mistake. Maybe you need to wrap your "$paramAValue" into {} too. when you tries to run downstream job?

But, according to what you want, I just tested this working solution:

I have two pipeline jobs (upstream and downstream):

  • Downstream job has parameter with name OS

  • Upstream job has choice parameter PickAnOS

and there is working pipeline script for upstream job, which runs downstream job with the selected parameter

pipeline {
    agent any
    parameters {
        choice(choices: ['Windows', 'Linux'], description: 'What OS?', name: 'PickAnOS')
    }
    stages {
        stage("run downstream job") {
            steps {
                echo "You choose: ${params.PickAnOS}"
                build job: 'downstream_job', parameters: [string(name: 'OS', value: '${params.PickAnOS}')]
            }
        }
    }
}

I hope this helps