【问题标题】:How to use the credentials stored in Jenkins in a groovy script in jenkins DSL如何在 jenkins DSL 的 groovy 脚本中使用存储在 Jenkins 中的凭据
【发布时间】:2020-01-11 06:32:42
【问题描述】:

我正在使用 jenkins 和 Jobdsl 来创建 jenkins 工作。我正在尝试通过在活动选择参数中添加一个 groovy 脚本来构建一个参数化作业。该脚本使用存储在 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?

【问题讨论】:

    标签: jenkins groovy


    【解决方案1】:

    你可以用同样的方式替换字符串,你已经用你的脚本名称(+ 字符串),但这一点都不时髦。

    所以这应该有效:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2018-05-22
      相关资源
      最近更新 更多