Simon
Simon

Reputation: 3264

Passing credentials to downstream build step in Jenkins pipeline

We're trying to start a downstream build which expects "UserName" and "UserPassword" as parameters.

withCredentials([usernamePassword(
        credentialsId: params.deployCredentialsId,
        usernameVariable: 'MY_USER',
        passwordVariable: 'MY_PASS',
)]) {

    build(job: "deploy/nightly",
        parameters: [stringParam(name: "UserName", value: MY_USER), password(name: "UserPassword", value: MY_PASS),
        ... more parameters
    )
}

but the downstream job never sees the UserName / UserPassword parameters. Is there a bug in the above definition, or should I look at the downstream job?

Upvotes: 0

Views: 1935

Answers (2)

Samit Kumar Patel
Samit Kumar Patel

Reputation: 2098

Let's say you have 2 Jenkins pipeline jobs called job-A and job-B and you want to call job-B from job-A by passing some parameters and you want to access those on job-B. The example would look like below:

job-A

pipeline {
    agent any;
    stages {
        stage('Build Job-B') {
            steps {
                build(job: "job-B", parameters: [string(name:"username",value: "user"),password(name:"password",value: "fake")], propagate: false, wait: true, quietPeriod: 5)
            }
        }
    }
}

job-B

pipeline {
    agent any;
    stages {
        stage('echo parameter') {
            steps {
                sh "echo $params"
            }
        }
    }
}

Note-

  1. params is by default available in all pipeline job, no matter whether you use a scripted pipeline or a declarative pipeline.

  2. you no need to define any parameter in the downstream job

Upvotes: 0

WackyYeti
WackyYeti

Reputation: 125

You need to look in the downstream job. It should have a 'parameters' block that looks like:

parameters {
    string(defaultValue: "", description: 'foo', name: "UserName")
    string(defaultValue: "", description: 'foo', name: "UserPassword")
}

Then in your stage you can do this:

stage('PrintParameter'){
    steps{
        sh 'echo ${UserName}'
    }
}

Upvotes: 1

Related Questions