Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference global credentials in Bash script in Jenkins job

Tags:

jenkins

I created global credentials (username,password) and gave a custom ID. How do I reference this ID in my Bash script? I checked "inject global password" in my job configuration already.

I tried Jenkins: Access global passwords in powershell but doesn't work.

Assume the ID of the global credentials is "USER_PASSWORD", my bash script tried:

echo ${USER_PASSWORD} echo ${env:USER_PASSWORD} 

Doesn't work. I read some online post I would need to query Jenkins API to get the credentials. That doesn't sound right.

like image 236
CppLearner Avatar asked Mar 02 '16 00:03

CppLearner


People also ask

How do I use Jenkins global credentials in shell script?

To use, first go to the Credentials link and add items of type Secret file and/or Secret text. Now in a freestyle job, check the box Use secret text(s) or file(s) and add some variable bindings which will use your credentials. The resulting environment variables can be accessed from shell script build steps and so on.

How do I add global credentials in Jenkins?

From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click Manage Jenkins > Manage Credentials. Under Stores scoped to Jenkins on the right, click on Jenkins. Under System, click the Global credentials (unrestricted) link to access this default domain. Click Add Credentials on the left.

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.


2 Answers

Using the Credentials Binding Plugin, global credentials may be injected into the build context. This is done by checking the checkbox labeled Use secret text(s) or file(s) in the Build Environment section of the job's configuration page. Add a Username and password (separated) binding, choose the specific credential you created, and define your username and password variables.

Here's an example of the Credentials Binding Plugin in use, but with a colon-delimited username/password variable instead of the two separate variables:

Credentials Binding Plugin example

You can then use the username and/or password variables you defined in your shell script. Assuming a username value of foo and a password value of bar...

Username and password binding, variable name USER_CREDENTIALS:

echo $USER_CREDENTIALS # -> foo:bar 

Username and password (separated) binding, variable names USER_ID and USER_PASSWORD:

echo $USER_ID          # -> foo echo $USER_PASSWORD    # -> bar 
like image 98
Christopher Parker Avatar answered Sep 18 '22 00:09

Christopher Parker


If your credentials ID is "USER_PASSWORD", you have to read it using eg. the following command: USER_CREDENTIALS = credentials('USER_PASSWORD'). After doing this, the username and password are available in the following environment variables: USER_CREDENTIALS_USR and USER_CREDENTIALS_PSW. Jenkins always adds _USR and _PSW endings to the names of the variables.

Example pipeline (I did not fully tested it, but should work):

pipeline {     agent any      environment {         USER_CREDENTIALS = credentials('USER_PASSWORD')     }      stages {         stage('Run') {             steps {                 sh "echo $USER_CREDENTIALS_USR"                 sh "echo $USER_CREDENTIALS_PSW"             }         }     } } 

If you get **** (four asterisks) as output, it's ok - Jenkins automatically masks usernames and passwords in the console output.

More information: https://jenkins.io/doc/book/pipeline/jenkinsfile/#usernames-and-passwords .

like image 31
JustAC0der Avatar answered Sep 21 '22 00:09

JustAC0der