Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make credentials available to shell script in jenkins

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:

  • credentials are not stored securely
  • they are visible to anybody having access to the project configuration
  • they leak in the Jenkins console

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?

like image 675
blueFast Avatar asked Dec 07 '16 09:12

blueFast


People also ask

How use Jenkins 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.


2 Answers

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: yourCredentialsId, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
    // user name and password will be stored in USERNAME and PASSWORD envs
}
like image 56
Amityo Avatar answered Sep 20 '22 20:09

Amityo


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: enter image description here

You can use $PASSWORD in your shell later on.

like image 28
MaTePe Avatar answered Sep 22 '22 20:09

MaTePe