How to use multiple credentials in withCredentials in Jenkins Pipeline
48,803
Solution 1
Sure, you can use one withCredentials
block to assign multiple credentials to different variables.
withCredentials([
usernamePassword(credentialsId: credsId1, usernameVariable: 'USER1', passwordVariable: 'PASS1'),
usernamePassword(credentialsId: credsId2, usernameVariable: 'USER2', passwordVariable: 'PASS2')
]){
//...
}
Solution 2
Also, you can use this with $class
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'awsID',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],
[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'myID',
usernameVariable: 'USR',
passwordVariable: 'PWD']])
Author by
lvthillo
Updated on July 05, 2022Comments
-
lvthillo almost 2 years
I have the following step in my declarative jenkins pipeline: I create script which comes from my
resources/
folder using libraryResource. This script contains credentials for myautobuild
user and for someadmintest
user.stage('Build1') { steps { node{ def script = libraryResource 'tests/test.sh' writeFile file: 'script.sh', text: script sh 'chmod +x script.sh' withCredentials([usernamePassword(credentialsId: xxx, usernameVariable: 'AUTOBUILD_USER', passwordVariable: 'AUTOBUILD_PASSWD')]){ sh './script.sh " } } }
This works fine. I can use my
autobuild
user. Now I'm searching for the best way how I can include also the crendentials of myadmintest
user. Do I have to 'nest' it with a secondwithCredentials
part or can I add again ausernamePassword
'array'? -
stevek-pro over 5 yearsHow to do this with Groovy?
-
Vitalii Vitrenko about 4 years@meshfields this is Groovy
-
WhoAmI almost 4 yearsAny class for string text
-
Mohsen Abasi almost 3 years@SteveK stage('stage 2') { steps{ withCredentials([usernamePassword(usernameVariable: 'user_1', passwordVariable: 'password_1', credentialsId: 'id_1'), usernamePassword(usernameVariable: 'username_2', passwordVariable: 'password_2', credentialsId: 'id_2')]){ sshagent(credentials : ['jenkins_ssh_user_key']) { sh """ """ } } } }