简体   繁体   English

在 Jenkins 管道库中使用 withCredentials([usernamePassword(... ) ])

[英]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.

相关问题 如何在 Jenkins Pipeline 的 withCredentials 中使用多个凭据 - How to use multiple credentials in withCredentials in Jenkins Pipeline 如何在不屏蔽 withCredentials.usernamePassword 中的 PASSWORD 的情况下重用 Jenkins 凭据? - How to reuse Jenkins credentials without masking the PASSWORD in withCredentials.usernamePassword? 当我与withCredentials一起使用时,Jenkins声明性管道会抛出NullPointerException - Jenkins Declarative Pipeline throws NullPointerException when I use withCredentials 使用 withCredentials SSH 到 Jenkins 管道中的远程服务器 - SSH into a remote server in a Jenkins pipeline using withCredentials 如何在 Jenkins 中使用 withCredentials for Perforce? - How to use withCredentials for Perforce in Jenkins? Jenkins withCredentials 机密不适用于共享库 - Jenkins withCredentials secret not working with shared library Jenkins:在全局环境部分使用 withCredentials - Jenkins : use withCredentials in global environment section 在库中使用Jenkins管道,但添加其他阶段 - Use Jenkins pipeline in library, but add additional stage 如何在 jenkins 管道 groovy 文件中使用 withcredentials 设置多个凭据 - How to Set multiple credentials using withcredentials in jenkins pipeline groovy file 在 Jenkins Pipeline 日志输出中隐藏密码而不使用 WithCredentials - Hiding passwords in Jenkins Pipeline log output without using WithCredentials
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM