简体   繁体   English

在Jenkinsfile中设置环境变量

[英]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. 我正在尝试使用我们的Jenkins团队创建的函数,该函数需要一个称为secrets的环境变量,并使用它来推送至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. 当我使用参数进行构建并将秘密用作参数时,这可以很好地工作,我也可以使用print("{$env.secrets}")打印出秘密,但是我想在文件中生成秘密,而不是将其作为参数。

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, 如果我未在代码中包含参数并定义了一个称为secrets的变量,则print("{$env.secrets}")输出null且该功能无效,

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? 我认为,如果可以在运行时设置{$env.secrets} ,它应该可以解决我的问题,有人知道如何做到这一点吗?

Thanks 谢谢

Let's say the function is called as processSecret(), then you can do it like the below snippet: 假设该函数称为processSecret(),那么您可以像下面的代码片段一样进行操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM