当前位置: 首页 > 知识库问答 >
问题:

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

洪育
2023-03-14

我正在使用jenkins和Jobdsl创建jenkins的工作。我试图通过在active choice参数中添加一个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?

共有1个答案

谈秦斩
2023-03-14

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

所以这应该行得通:

... "curl ... 'Authorization: token ${creds.password}' ...
 类似资料:
  • 我知道我可以通过Jenkins RTC插件使用scm轮询,我只是想知道是否有一个如何通过管道插件的groovy脚本做到这一点的例子? 例如: 类似于上面的内容,但不是git,而是将rtc toolkit与prod url一起使用,并指定一个流或一个工作区。。。在任何地方都找不到示例,也不确定如何通过api实现它(或者如果可能的话?)

  • 问题内容: 我有以下内容: 作为全球描述共享库创建这里。没什么特别的,文件夹中的一个脚本名为,可以尝试-工作。图书馆被称为 名为 调用的管道脚本正在调用 现在,当我像这样运行构建时,出现以下错误: 错误:找不到库的任何定义[myOneLib] 但是当我简单地将行移动到所有工作的顶部时。 所以我的问题是如何在导入/包含的脚本中使用?还是有其他方法可以指定全局库? 很少有其他注释:并且都在同一个git

  • 问题内容: 我想将“ $ WORSKPACE”变量用于jenkins脚本调用的常规文件。但是在SO上找到的所有解决方案都失败了: 我得到的消息:脚本不允许使用groovy.lang.GroovyObject方法setProperty java.lang.String java.lang.Object(JenkinsHelper.name)。管理员可以决定是批准还是拒绝此签名。 对设置为允许Jenk

  • 问题内容: 假设我在Jenkins中有一个Groovy脚本,其中包含多行shell脚本。如何在该脚本中设置和使用变量?正常方式会产生错误: 捕获:groovy.lang.MissingPropertyException:否这样的属性:foo对于类:groovy.lang.Binding 问题答案: 您需要更改为三重单引号或转义美元 然后,您将跳过常规模板,这就是给您这个问题的原因

  • 问题内容: 我有两个依赖的工作。我需要詹金斯(Jenkins)的groovy脚本的帮助,以便为插件编写预发送脚本。 我想检查buid原因是否是上游原因,然后设置cancel 但是我不知道如何在groovy中为jenkins写条件..对于单独的工作,jenkins中是否有任何单独的类(所以我可以创建实例并调用上游原因) 是否有任何方法可以检查下游作业的生成原因是由于上游造成的。 请帮助我这个代码片段

  • 我需要从soapui工具的xml中获取价值,并将这些数据存储到Excel表中。我在SoapUI工具中使用了groovy脚本。 如果响应有多个输出,这些输出存储在excel表中。像LocationName和CustCity333Name有两次,所以这些输出应该存储到excel表中。请帮助我解决这个问题