gpt4 book ai didi

动态选择参数中的 Jenkins withCredentials

转载 作者:行者123 更新时间:2023-12-04 17:54:03 26 4
gpt4 key购买 nike

你能帮我解决一点麻烦吗?

我尝试使用 jenkins 和您出色的插件找到解决方案:uno-choice,但我做不到。

我有一个非常简单的脚本:

#!/usr/bin/env groovy
def sout = new StringBuffer(), serr = new StringBuffer()
def proc ='/var/lib/jenkins/script.sh location'.execute()

proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)

def credential(name) {
def v;
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: name, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
v = "${env.USERNAME}"
}
return v
}

def key = credential('aws_prod_api')

String str = sout.toString()
String s = str.trim()
String[] items = s.split(",");
def v1 = Arrays.asList(items)
return v1

一般来说,我想从 bash 脚本中获取保存在 Jenkins 中的 AWS 凭证,并用它做一些事情。

我想在生成选定列表的 block 中使用 withCredentials,但我不知道该怎么做。

你能帮我吗?我会非常感激的

我尝试在 groovy 中使用 withCredentials,但出现错误:

Fallback to default script... groovy.lang.MissingMethodException: No signature of method: Script1.withCredentials() is applicable for argument types: (java.util.ArrayList, Script1$_credential_closure1) values: [[[$class:UsernamePasswordMultiBinding, credentialsId:aws_prod_api, ...]], ...] at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81) at

最佳答案

这是因为 withCredentials 不存在于 Script1 的范围内。它存在于 Jenkinsfile DSL 的范围内。您需要将其传入。

我建议将您的脚本转换为函数。然后将 Jenkinsfile DSL 传递给您的 Groovy 代码。

def doAwsStuff(dsl) {
...
def key = credential(dsl, 'aws_prod_api')
...
}

def credential(dsl, name) {
def v;
dsl.withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: name, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
v = "${env.USERNAME}"
}
return v
}

然后从您的 Jenkinsfile 调用它:

def result = MyAwsStuff.doAwsStuff(this)

关于动态选择参数中的 Jenkins withCredentials,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41961632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com