runcmd
runcmd

Reputation: 602

Access Jenkins credential store secrets with env groovy file

I've implemented loading a groovy file for env variables by following this post.

env.groovy:

env.DB_USER = 'testuser'
env.DB_PASS = credentials('DB_PASS')

Jenkinsfile:

stages {
    stage ("print") {
        steps {
            load "${WORKSPACE}/env.groovy"
            echo "${env.DB_USER}"
            echo "${env.DB_PASS}"
        }
    }
}

Output:

[Pipeline] echo
testuser
[Pipeline] echo
@credentials(<anonymous>=DB_PASS)

Is accessing the Jenkins credential store possible for DB_PASS when loading a groovy env variable file?

Note: I know I can access the environment variables in the environment { } block of my Jenkinsfile. But since I have so many env variables, I was wondering if I could reference them all in a separate groovy file instead.

Upvotes: 2

Views: 3901

Answers (2)

Rodyb
Rodyb

Reputation: 61

If you have

environment {
            DB_PASS = credentials('DB_PASS')
        }

If you really would like to see the contents of that you could do

  sh """
     echo "$DB_PASS" | base64
                    
     """

That will show you the secret, hope this helps.

Upvotes: 1

Shiv Rajawat
Shiv Rajawat

Reputation: 998

For the time being, try to do it like this inside your jenkinsfile.

environment {
            DB_PASS = credentials('DB_PASS')
        }

However you still can't echo your DB_PASS environment variable since credentials type variables retain the property of secrecy. And also keep in mind that variables have scope in jenkinsfile.

Upvotes: 1

Related Questions