Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple credentials in withCredentials in Jenkins Pipeline

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 my autobuild user and for some admintest 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 my admintest user. Do I have to 'nest' it with a second withCredentials part or can I add again a usernamePassword 'array'?

like image 931
lvthillo Avatar asked Nov 24 '17 14:11

lvthillo


People also ask

How do I use credentials parameters in Jenkins?

Head to the Build Environment section of your job definition. Check Use secret text(s) or file(s). This will actually inject the secret into your build environment. The "Credentials Parameter" created earlier can be used here to let you select different credentials parameters.

How do I pass AWS credentials in Jenkins pipeline?

To configure AWS credentials in Jenkins: On the Jenkins dashboard, go to Manage Jenkins > Manage Plugins in the Available tab. Search for the Pipeline: AWS Steps plugin and choose Install without restart. Navigate to Manage Jenkins > Manage Credentials > Jenkins (global) > Global Credentials > Add Credentials.


1 Answers

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') ]){     //... } 
like image 142
Vitalii Vitrenko Avatar answered Sep 20 '22 19:09

Vitalii Vitrenko