AnonymousAlias
AnonymousAlias

Reputation: 1399

Setting Environment Variable in Jenkinsfile

this is a but of a strange problem. I am trying to use a function our Jenkins team has created that takes an environment variable called secrets and uses it to push to vault.

When I build with parameters and use secrets as a parameter this works fine, i can also print out the secrets using print("{$env.secrets}") But I want to generate the secrets in my file not send them in as a parameter.

If I don't include a parameter and define a variable called secrets in my code print("{$env.secrets}") prints null and the function does not work,

I have tried

 SECRETS = "{{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}}"
        environment {
            secrets = SECRETS
    }

The function that uses secrets does not accept it as a parameter directly so I cannot change what I send into it, it looks something like this

withCredentials([[$class: 'XXXXX', credentialsId: 'XXXX',
            usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
            vaultLoadSecrets(username:"$USERNAME",password:"$PASSWORD")
        }

I think if I can set {$env.secrets} at runtime it should be able to solve my issue, does anyone know how to do this?

Thanks

Upvotes: 1

Views: 644

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 328

Let's say the function is called as processSecret(), then you can do it like the below snippet:

withEnv(["secrets="+SECRETS]) {
    processSecret()
}

Upvotes: 1

Related Questions