arun kumar
arun kumar

Reputation: 267

Define credential parameter in parameters in Jenkins declarative pipeline?

I Currently using Jenkins Delarative pipeline with a parameterised build

pipeline {
    agent any
    parameters {
        booleanParam(name: 'cleanDB',defaultValue: false,description: 'should clean db ?' )
        string(name: 'host',defaultValue: 'xyx',description: 'DB Host')
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn verify'
            }
        }
        stage('Execute') {
            steps {
                withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIALS', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])
                        {
                            sh "ant " +"-Ddb.clean=${params.cleanDB} -Ddb.host=${params.host} -Ddb.userid=$USERNAME \"-Ddb.password=$PASSWORD\" "
                        }
            }
        }
    }
}

when i try to build with parameters it prompts only two param cleanDB,host params.i would like it to also ask which credential parameter to take.it takes only when explicitly added though UI in parameterised build.

so how can i add credential parameter in parameters can any one share an example of defining it in below syntax.

parameters {
        booleanParam(name: 'cleanDB',defaultValue: false,description: 'should clean db ?' )
        string(name: 'host',defaultValue: 'xyx',description: 'DB Host')
credentialParam(name: 'host',description: 'Credentials')
    }

Upvotes: 7

Views: 23051

Answers (4)

Alex
Alex

Reputation: 849

Apply usernamePassword(credentialsId: env.CREDENTIALS, .. ) to provide selected credential to the script as custom variables, example:

steps {
    withCredentials([ usernamePassword(
            credentialsId: env.CREDENTIALS,
            usernameVariable: 'USERNAME',
            passwordVariable: 'PASSWORD') ]) {
        script {
            sh( script: "./my_shell_script.sh" )   // echo "$USERNAME, $PASSWORD"
        }
    }
}

.. see my detailed answer to a similar question: https://stackoverflow.com/a/79197253/1745341

Upvotes: 0

FueledByPizza
FueledByPizza

Reputation: 55

Sorry for being late, but I guess I have to answer for newly comments and future issues.

No documentation, but from http://<yourJenkinsController>/job/<yourJobName>/directive-generator/ you can insert parameters you want and then get the corresponding code to enter in a parameters directive. And it should be:

parameters {
    credentials(name: 'USERPASS', required: true, credentialType: "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl", defaultValue: 'My-CredentialItem-Id', description: 'Description')
}

Upvotes: 1

BartBiczBoży
BartBiczBoży

Reputation: 2672

While as of today (2017-08-29) jenkins docs mention only string and boolean types of possible parameters, there is some ticket that answer this question. It says to do:

parameters {
    credentials(name: 'CredsToUse', description: 'A user to build with', defaultValue: '', credentialType: "Username with password", required: true )
} 

I just tried it and it works fine. When executed for the first time it doesn't ask anything, it just creates parameter for the job. After then it asks for credentials as it should.

Naturally, it works for Declarative Pipeline syntax, so must be enveloped with 'pipeline'.

Upvotes: 16

haschibaschi
haschibaschi

Reputation: 2792

Try the following:

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIALS', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])
                        {
                            sh 'ant -Ddb.clean=${params.cleanDB} -Ddb.host=${params.host} -Ddb.userid=$USERNAME -Ddb.password=$PASSWORD'
                        }

according to the documentation on cloudbees https://support.cloudbees.com/hc/en-us/articles/204897020-Fetch-a-userid-and-password-from-a-Credential-object-in-a-Pipeline-job-

Upvotes: 1

Related Questions