Dus*_*rea 7 groovy jenkins jenkins-pipeline
我想withCredentials()在共享变量(“vars/”)脚本中使用块,而不是直接在 Jenkins 管道中使用,因为这是特定库的低级语义,也可能需要也可能不需要,具体取决于情况。但是,withCredentials(或者,至少,它的签名)似乎不在范围内。
脚本:
def credentials = [
[$class: 'UsernamePasswordMultiBinding', credentialsId: '6a55c310-aaf9-4822-bf41-5500cd82af4e', passwordVariable: 'GERRIT_PASSWORD', usernameVariable: 'GERRIT_USERNAME'],
[$class: 'StringBinding', credentialsId: 'SVC_SWREGISTRY_PASSWORD', variable: 'SVC_SWREGISTRY_PASSWORD']
]
withCredentials(credentials) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
安慰:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: BuildagentInstallAndRun.withCredentials() is applicable for argument types: (java.util.ArrayList, org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [[[$class:UsernamePasswordMultiBinding, credentialsId:6a55c310-aaf9-4822-bf41-5500cd82af4e, ...], ...], ...]
Run Code Online (Sandbox Code Playgroud)
有没有人在这方面取得过任何成功?
我使用的是共享库而不是共享变量,但我想这是类似的情况。我没有使用$class参数,但我直接调用管道片段生成器建议的函数之一。你可以在这里列出一份清单。在下面的示例中,我使用了usernameColonPassword绑定。在管道中,我实例化类实用程序,然后传递this给构造函数。然后,在库中,我使用step对象来访问管道步骤(例如withCredentials或usernameColonPassword)。
class Utilities implements Serializable {
def steps
Utilities(steps) {
this.steps = steps
}
def doArchiveToNexus(String credentials, String artifact, String artifact_registry_path){
try {
this.steps.withCredentials([steps.usernameColonPassword(credentialsId: credentials, variable: 'JENKINS_USER')]) {
this.steps.sh "curl --user " + '${JENKINS_USER}' + " --upload-file ${artifact} ${artifact_registry_path}"
}
} catch (error){
this.steps.echo error.getMessage()
throw error
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以尝试以下操作:
import jenkins.model.*
credentialsId = '6a55c310-aaf9-4822-bf41-5500cd82af4e'
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
Run Code Online (Sandbox Code Playgroud)
但它不安全,一切都会在控制台日志中
| 归档时间: |
|
| 查看次数: |
10971 次 |
| 最近记录: |