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.
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.
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.
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.
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:
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
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With