Define credential parameter in parameters in Jenkins declarative pipeline?
Asked Answered
R

3

6

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')
    }
Romie answered 2/5, 2017 at 11:22 Comment(0)
E
16

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'.

Eupatorium answered 29/8, 2017 at 12:46 Comment(1)
It boggles the mind that his is still undocumented 7 years later.Feminacy
I
1

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-

Ibis answered 2/5, 2017 at 14:24 Comment(1)
i was able to use this in code but i want define in parameters block. as i am picking jenkins file as pipelinescript from scm. i want it automatically ask at the time for buidwithparameters button.Romie
S
0

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')
}
Stcyr answered 11/10 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.