How to make credentials available for shell script in jenkins

We have freestyle projects configured in Jenkins that run shell scripts as build steps. In some cases, we need access to credentials for third-party services. We solved this by providing credentials like:

USER=theuser
PASS=thepass

in the project environment ( Prepare an environment for the run -> Properties Content)

This works fine, but this is a bad solution because:

  • credentials are not stored securely
  • they are visible to anyone who has access to the project configuration
  • they flow in the Jenkins console.

We studied a little and discovered a promising plugin , but we don’t know how to make the credentials managed by the plugin accessible to our scripts, ideally as environment variables.

How can we access credentials managed by the Jenkins plugin from a script?

+4
source share
2 answers
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: yourCredentialsId, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
    // user name and password will be stored in USERNAME and PASSWORD envs
}
+4
source

We use this: https://wiki.jenkins-ci.org/display/JENKINS/Mask+Passwords+Plugin

then you specify the environment variables that you want passwords to have

So, if you specify something like this: enter image description here

You can use $ PASSWORD in your shell later.

+3
source

Source: https://habr.com/ru/post/1663063/


All Articles