We have freestyle projects configured in Jenkins, running shell scripts as build steps. In some cases we need access to credentials for third-party services. We have solved this by providing the credentials as:
USER=theuser
PASS=thepass
in the project environment (Prepare an environment for the run -> Properties Content
)
This is working fine, but it is a bad solution because:
We have researched a bit and found a promising plugin, but we do not know how to make the credentials managed by the plugin available to our scripts, ideally as environment variables.
How can we access the credentials managed by the Jenkins plugin from a 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.
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: yourCredentialsId, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
// user name and password will be stored in USERNAME and PASSWORD envs
}
We are using this one: https://wiki.jenkins-ci.org/display/JENKINS/Mask+Passwords+Plugin
Then you specify the environment-variables you want the passwords to have.
So if you specify something like this:
You can use $PASSWORD
in your shell later on.
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