[英]Use withCredentials([usernamePassword( ... ) ]) in Jenkins Pipeline Library
I'm trying to move some functions from a Jenkins Pipeline to a shared jenkins library.我正在尝试将一些功能从 Jenkins 管道移动到共享的 jenkins 库。 But I get errors when using the Credentials Binding Plugin (withCredentials)
但是在使用凭据绑定插件(withCredentials)时出现错误
For example I have this block of code:例如我有这个代码块:
withCredentials([usernamePassword(credentialsId: 'foobar', usernameVariable: 'fooUser', passwordVariable: 'fooPassword')]) {
// do something with credentials
}
When I move this block to a static library function I get the following error:当我将此块移动到 static 库 function 时,我收到以下错误:
hudson.remoting.ProxyException:
groovy.lang.MissingMethodException:
No signature of method: static mylib.MyClass.usernamePassword() is applicable for argument types:
(java.util.LinkedHashMap) values: [[credentialsId:foobar, usernameVariable:fooUser, ...]]
Library Code:图书馆代码:
package mylib;
class MyClass {
def static String doSomething() {
withCredentials([usernamePassword(credentialsId: 'foobar', usernameVariable: 'fooUser', passwordVariable: 'fooPassword')]) {
// some code
}
}
}
Usage in Jenkins Pipeline: Jenkins 管道中的用法:
@Library('my-pipeline-library')
import mypackage.MyClass
...
MyClass.doSomething();
How can I use withCredentials/usernamePassword in my Jenkins Library?如何在我的 Jenkins 库中使用 withCredentials/usernamePassword? Do I need to qualify the functions with some package?
我是否需要使用一些 package 来限定功能? Do I need extra imports?
我需要额外的进口吗? Is there any documentation about this?
有这方面的文件吗?
I found a possible solution, not sure if i really like it:我找到了一个可能的解决方案,不确定我是否真的喜欢它:
I can pass the current script (this) from the pipeline script to the library.我可以将当前脚本(this)从管道脚本传递到库。 Then I can use this script variable to use functions in my pipeline library.
然后我可以使用这个脚本变量来使用我的管道库中的函数。
Looks like this:看起来像这样:
Library Code:图书馆代码:
package mylib;
class MyClass {
def static String doSomething(script) {
script.withCredentials([script.usernamePassword(credentialsId: 'foobar', usernameVariable: 'fooUser', passwordVariable: 'fooPassword')]) {
// some code
}
}
}
Usage in Jenkins Pipeline: Jenkins 管道中的用法:
@Library('my-pipeline-library')
import mypackage.MyClass
...
MyClass.doSomething(this);
Myabe not the best method, but you can use the credential in the pipeline and pass what you extract from it as a parameter to the class: Myabe 不是最好的方法,但您可以在管道中使用凭证并将从中提取的内容作为参数传递给 class:
jenkinsfile:詹金斯文件:
import com.mydomain.gcp
Gcp gcp = new Gcp()
Pipeline{
environment {
gcpCredentialId = 'YOUR-CREDENTIAL-ID'
}
stages {
stage('Authenticate') {
steps {
script {
withCredentials([file(credentialsId: env.gcpCredentialId, variable: 'gcpAuthFile')]) {
gcp.authenticate("${gcpAuthFile}")
}
}
}
}
}
}
then in the src\com\mydomain\Gcp.groovy file:然后在 src\com\mydomain\Gcp.groovy 文件中:
package com.mydomain.gcp
def authenticate(final String keyFile) {
sh "gcloud auth activate-service-account --key-file=${keyFile}"
}
return this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.