简体   繁体   English

如何在 jenkins DSL 的 groovy 脚本中使用存储在 Jenkins 中的凭据

[英]How to use the credentials stored in Jenkins in a groovy script in jenkins DSL

I am using jenkins with Jobdsl to create jenkins jobs.我正在使用 jenkins 和 Jobdsl 来创建 jenkins 工作。 I am trying to build a parameterized job by adding a groovy script in active choice parameter.我正在尝试通过在活动选择参数中添加一个 groovy 脚本来构建参数化作业。 The script uses a credential stored in jenkins credential, I am trying to fetch it in my script by using the code该脚本使用存储在 jenkins 凭据中的凭据,我正在尝试使用代码在我的脚本中获取它

import jenkins.model.*
import jenkins.*
import jenkins.model.* 
import hudson.*
import hudson.model.*

credentialsId = '1672622gjj'
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.Credentials.class,
        Jenkins.instance,
        null,
        null
).find{it.id == credentialsId};

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
  com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{
    it.id == credentialsId}
    println(creds.username)
    println(creds.password)


This code gives me the credential name and password but the result of the branches is blank. I am using `creds.password` as the authorization token.

What I am doing wrong?

You can replace the string there the same way, you do already with your script name ( + the strings), yet this not groovy at all.你可以用同样的方式替换那里的字符串,你已经用你的脚本名称( +字符串)做了,但这一点都不常规。

So this should work:所以这应该有效:

... "curl ... 'Authorization: token ${creds.password}' ...

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

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