如何在jenkins管道groovy文件中使用凭证设置多个凭证

问题描述 投票:0回答:1

我需要为一个作业设置两个或多个凭据,我的计划是像下面一样单独使用它,以便可以在多个作业中使用它

static void _artifactoryCredentialBinding(Job job) {
    job.with {
        wrappers {
            credentialsBinding {
                usernamePassword('USERNAME', 'PASSWORD', 'xxxxx')
            }
        }
    }
}

static void _jasyptCredentialBinding(Job job) {
    return job.with {
        wrappers {
            credentialsBinding {
                usernamePassword('', 'PASSWORD', 'jasypt-credentials')
            }
        }
    }
}

[当我这样做时,第一个凭证已被第二个凭证取代。

我将在Groovy文件中必要的地方将这两种方法称为辅助方法。

我需要在少数几个作业中添加多个凭据,而在一个作业中仅添加一个凭据。

在一个包装器下添加凭据将起作用-multiple-credentials,但如果我在同一包装器下添加多个凭据,则将无法重用。

我尝试通过上述方法返回Job,并使用相同的方法设置信用,但在构建时出现错误-

错误:(CredentialBindingUtil.groovy,第28行)无方法签名:xxxx.CredentialBindingUtil $ __ pfJasyptCredentialBinding_closure3.wrappers()适用于参数类型:(xxx.CredentialBindingUtil $ __ pfJasyptCredentialBinding_closure3 $ _closure9)值:[xxxx.Credential $ _closure9 @ 11b4d391][Office365connector]没有Webhook通知

我如何使凭证与现有凭证一起附加?

jenkins groovy jenkins-pipeline jenkins-job-dsl
1个回答
0
投票

如评论中所述,可以通过Configure Block来实现。

static void _artifactoryCredentialBinding(def job) {
    job.with {
      configure { node ->

        node / 'buildWrappers' / 'org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper' / 'bindings' << 'org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding' {

          usernameVariable 'some-credential-id'
          credentialsId PASS1
          passwordVariable USER1

        }
      }
    }
}

static void _jasyptCredentialBinding(def job) {
  job.with {
    configure { node ->

      node / 'buildWrappers' / 'org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper' / 'bindings' << 'org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding' {

        usernameVariable 'some-credential-id'
        credentialsId PASS2
        passwordVariable USER2

      }
    }
  }
}

def a_job = job('a-temporaryjob')

_artifactoryCredentialBinding(a_job)
_jasyptCredentialBinding(a_job)


[为了了解配置块的工作原理,我强烈建议阅读wiki page和较旧的blog post,其中逐步说明了如何配置不受支持的插件。

© www.soinside.com 2019 - 2024. All rights reserved.