4

How can I get access to passwords defined in Jenkins Global Configuration?

Passwords are not injected by default and I was trying code below and was able to access "Global properties" but not luck with passwords.

def envVars = Jenkins.instance.getGlobalNodeProperties()[0].getEnvVars() 
println envVars['MY_VARIABLE']
awattar
  • 446
  • 5
  • 19
  • have you checked the following [answer](https://stackoverflow.com/a/27158053/2336650) ? I know its in powershell but i think same concept can be applied in bash – Mostafa Hussein Oct 11 '18 at 15:46
  • based on [this statement](https://stackoverflow.com/questions/27152656/jenkins-access-global-passwords-in-powershell/27158053) are you using EnvInject or Credentials Binding plugin? – StephenKing Oct 15 '18 at 04:20
  • 2
    To clarify some things - looking into config.xml file of the free style job where I can utilize "Global Passwords" it is plugin="envinject@2.1.6" with injectGlobalPasswords set to true. That part seems to don't work well with Multibranch pipeline based on Jenkinsfile that I'm trying to set up, although "Global properties" are injected properly. So I'm looking forward for the solution similar to the one for properties that I have posted in the question. – awattar Oct 17 '18 at 07:46

4 Answers4

2

Use the withCredentials step, which comes with the Credentials Binding Plugin.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
1

Are you referring to Jenkins -> Manage Jenkins -> Global properties ?

If yes, below is how we retrieve them in our groovy script:

import jenkins.model.*

instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()

globalNodeProperties.each {
  envVars = it.getEnvVars()
  if (envVars.get('ARTIFACTORY_USR') != null) {
   artifactory_usr = envVars.get('ARTIFACTORY_USR');
  }
  if (envVars.get('ARTIFACTORY_PSW') != null) {
   artifactory_pwd = envVars.get('ARTIFACTORY_PSW');
  }
}

ARTIFACTORY_USR and ARTIFACTORY_PSW are pre-defined global properties

ben5556
  • 2,915
  • 2
  • 11
  • 16
  • It's about Global Passwords not properties. – awattar Oct 12 '18 at 08:19
  • It seems that we are all confused and do not know this "Global Passwords" feature. [This question](https://stackoverflow.com/questions/27152656/jenkins-access-global-passwords-in-powershell/27158053) seems to have the answer... "Don't confuse EnvInject plugin and Credentials Binding plugin. The two do quite different things, however both allow the manage passwords globally, yet differently." – StephenKing Oct 15 '18 at 04:20
0

In general, I create credentials in Jenkins -> credentials page to access the credential in the pipeline.

How to create Credentials in Jenkins 1. Open Credential page (Jenkins --> Credential) 2. Create a credential with Username and password and define a valid ID (For Example:myCredentialId)

How to access the credential using withCredentials in the pipeline

pipeline {
agent any

stages {
    stage("Access the credentials") {
        steps {
            script {
               withCredentials([[
                $class:'UsernamePasswordMultiBinding', 
                credentialsId: 'myCredentialId',
                usernameVariable:'username',
                passwordVariable:'token'
                ]]) {
                 sh '''
                  curl -u ${username}:${token} -L <replace your git URL> -o master.txt
                  cat master.txt   
                '''
                }
            }
        }
    }
}

}

Naren Chejara
  • 1,124
  • 10
  • 7
0

The inject global passwords option for non-pipeline jobs comes from the EnvInject plugin, which isn't fully supported for pipeline jobs: https://plugins.jenkins.io/envinject/

Jenkins Pipeline compatibility
Even though it is possible to set up the EnvInject Job Property and build step in Pipeline, the plugin does not provide full compatibility with Jenkins Pipeline.

Supported use-cases:

Injection of EnvVars defined in the "Properties Content" field of the Job Property
These EnvVars are being injected to the script environment and will be inaccessible via the "env" Pipeline global variable
Please note there is also a known compatibility issue with Durable Task 
Plugin 1.13
All other use-cases of the plugin may work incorrectly in Jenkins Pipeline. Please see JENKINS-42614 for more information about unsupported use-cases. There is no short-term plan to fix the compatibility issues though any pull requests to the plugin will be appreciated.

I had the same use case and ended up recreating the global password as a credential.

Stew C
  • 697
  • 3
  • 10
  • 24