Arpit
Arpit

Reputation: 87

Unable to pass a parameter from one pipeline job to another

Editing the question: I am trying to run a simple pipeline job which triggers another pipeline job and sends parameter values. I tried a simplified usecase in the example below

Piepeline - Parent

pipeline{
agent any
options {
    buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
}
stages {
    stage('Invoke sample_pipleline') {
        steps {
                CommitID = 'e2b6cdf1e8018560b3ba51cbf253de4f33647b5a'
                Branch = "master"
            input id: 'Environment', message: 'Approval', parameters: [choice(choices: ['FRST', 'QA', 'PROD'], description: '', name: 'depServer')], submitter: 'FCMIS-SFTP-LBAAS', submitterParameter: 'user'
            build job: 'simple_child', 
        parameters: [string(name: 'CommitID', value: 'e2b6cdf1e8018560b3ba51cbf253de4f33647b5a'),string(name: 'Environment', value: depServer), string(name: 'Branch', value: 'master')], 
                quietPeriod: 1
        }
    }
}

}

Pipeline - child

pipeline{
agent any
parameters {
    string defaultValue: '', description: 'K', name: 'depServer'
}
options {
    buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
}
stages {
    stage('CodePull') {
        steps {
            echo "Testing"
            echo "${depServer}"
        }
    }
}

}

When I run the parent pipeline it did not trigger child pipeline but gave error.

    Started by user ARAV
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Windows_aubale in                 C:\Users\arav\Documents\Proj\Automation\Jenkins\Jenkins_slave_root_directory\workspace\sample_parent2
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Invoke sample_pipleline)
    [Pipeline] input
    Input requested
    Approved by ARAV
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    groovy.lang.MissingPropertyException: No such property: depServer for class: groovy.lang.Binding

After implementing changes suggested and with some tweaks The parent job triggers the child job but the child log shows that it doesn't receive the parameter passed.

    Started by upstream project "sample_parent" build number 46
    originally caused by:
    Started by user ARAV
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /var/jenkins_home/workspace/simple_child
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (CodePull)
    [Pipeline] echo
    Testing
    [Pipeline] echo

    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS

Please help me understand what am I doing wrong here. Appreciate your help!!

Upvotes: 1

Views: 1522

Answers (3)

Arpit
Arpit

Reputation: 87

Thank you so much zett42 and MaratC! So finally the code that worked is as follows(combining both the answers):

Parent script:

pipeline{
    agent any
    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
    }
    stages {
        stage('Invoke sample_pipleline') {
            steps {
                script{
                    def CommitID
                    def depServer
                    def Branch
                    CommitID = 'e2b6cdf1e8018560b3ba51cbf253de4f33647b5a'
                    Branch = "master"
                userip = input id: 'Environment', message: 'Approval', parameters: [choice(choices: ['FRST', 'QA', 'PROD'], description: '', name: 'input_env')], submitter: 'FCMIS-SFTP-LBAAS', submitterParameter: 'user'
                depServer = userip.input_env
                echo "${depServer}"
                build job: 'simple_child', 
            parameters: [string(name: 'CommitID', value: "${CommitID}"),
                         string(name: 'Environment', value: "${depServer}"), 
                         string(name: 'Branch', value: "${Branch}")], 
                    quietPeriod: 1
                }
            }
        }
    }
}

Child script:

pipeline{
    agent any
    parameters {
        string defaultValue: '', description: 'K', name: 'Environment'
    }
    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '5')
    }
    stages {
        stage('CodePull') {
            steps {
                echo "Testing"
                echo "${params.Environment}"
            }
        }
    }
}

Upvotes: 1

MaratC
MaratC

Reputation: 6889

If your child pipeline has parameter named "depServer":

parameters {
    string name: 'depServer', defaultValue: '', description: ''
}

You should provide a value for it:

build job: 'simple_child', 
    parameters: [string(name: 'depServer', value: 'SOMETHING'] 

Finally, you should address it:

        steps {
            echo "Testing"
            echo "${params.depServer}"
        }

Upvotes: 1

zett42
zett42

Reputation: 27806

groovy.lang.MissingPropertyException: No such property: depServer for class: groovy.lang.Binding

This means that you don't have defined a variable depServer.

Fix it by assigning the result of the input step to variable depServer:

steps {
    script {
        def input_env = input id: 'Environment', message: 'Approval', parameters: [choice(choices: ['FRST', 'QA', 'PROD'], description: '', name: 'depServer')], submitter: 'FCMIS-SFTP-LBAAS', submitterParameter: 'user'

        build job: 'simple_child', 
            parameters: [string(name: 'CommitID', value: 'aa21a592d1039cbce043e5cefea421efeb5446a5'),string(name: 'Environment', value: input_env.depServer), string(name: 'Branch', value: "master")], 
            quietPeriod: 1
    }
}

I've added a script block, to be able to create and assign a variable.

The input actually returns a HashMap that looks like this:

[depServer:QA, user:someUser]

That's why we have to write input_env.depServer as argument for the build job.

Upvotes: 1

Related Questions