How to pass all global credentials to Jenkins pipeline

Viewed 2675

This is my first question posted on stackoverflow, hence in case I did something incorrectly pleaselet me know.

Description
I am currently working on translation of freestyle projects to declarative pipelines in Jenkins (jenkinsfiles kept in Git repo). The original freestyle job was triggering PowerShell script which needed access to Global name/password pairs defined in Mask Passwords plugin section in Configure System. Solution to this problem was an additional tick in project itself (unfortunately I am not allowed adding screenshots to posts yet, hence editor uploaded screen to imgur and pasted link - please see Screenshot 1): Screenshot 1

Therefore I started looking for possible implementation of such solution to jenkinsfile, however wothout luck.

My problem
When the script is triggered from the pipeline, it errors out stating that it cannot find relevant passwords (powershell refers to those credentials as to environment variables). This works fine when ran from freestyle project. Which I reckon is caused by pipeline not being able to reach out to previously mentioned credentials.

What I tried
Wrapping the step into below block:

wrap([$class: 'MaskPasswordsBuildWrapper']) {
    bat(batch file launching ps script)
}

Then the above block containing relevant step wrapping into

script {
    wrap(...)
}

But none of them worked. I have taken a look at other plugins like Credentials Binding Plugin or Credentials Plugin but those allow to bind/pass one credential per step, and I need to pass all credentials specified in Jenkins (I am open to move saved credentials to any other location within Jenkins). I have looked at adding environment variable:

credentials('Credentials-ID')

But the problem is the same as with mentioned plugins.

By any chance, have anyone came across similar situation and know what can be done in order to allow pipeline to access/pass to pipeline all the credentials specified in Jenkins instead of binding/passing them one a time?

All tips are very welcome!

1 Answers

You can do this and the env variable will then be available throughout your job. You could define multiple env variable too.

environment {
        // Use credentials() to hide the environment variable's output
        MY_PERSONAL_TOKEN = credentials('Credentials-ID')
    }

stages {
  stage('Test Stage') {
        steps {
            script {
                 // do what you need to
            }
        }
    }
}
Related