如何使用 Jenkins 共享变量中的 Closure 创建包装 Jenkins withCredentials 的 withXCredentials?
How to create withXCredentials that wraps Jenkins withCredentials using Closure in Jenkins shared variable?
我想在我的管道脚本中使用完全符合此语法的代码:
withXCredentials(id: 'some-cred-id', usernameVar: 'USER', passwordVar: 'PASS') {
//do some stuff with $USER and $PASS
echo "${env.USER} - ${env.PASS}"
}
请注意,您可以将要执行的 withXCredenitals
内的任何代码放入。 withXCredentials.groovy
驻留在 vars
文件夹下的我的 Jenkins 共享库中,它将使用
詹金斯原创 withCredentials
:
//withXCredentials.groovy
def userVar = params.usernameVar
def passwordVar = params.passwordVar
def credentialsId = params.credentialsId
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: usernameVar, passwordVariable: passwordVar]]) {
body()
}
我仍在学习高级 groovy 东西,但我不知道该怎么做。
请注意:
我的问题更多是关于 groovy 中的语法和使用 Closure
,答案 here 不是我想要的。使用该解决方案,我需要先实例化 class 然后调用该方法。所以我尽量避免做这样的事情:
new WithXCredentials(this).doSomthing(credentialsId, userVar, passwordVar)
在 Jenkins 文档中有一个使用闭包的例子:
// vars/windows.groovy
def call(Closure body) {
node('windows') {
body()
}
}
//the above can be called like this:
windows {
bat "cmd /?"
}
但是没有解释如何像这样传递参数
windows(param1, param2) {
bat "cmd /?"
}
见here
所以在网上搜索后我终于找到了答案。万一有人需要同样的东西。以下代码将起作用:
// filename in shared lib: /vars/withXCredentials.groovy
def call(map, Closure body) {
def credentialsId = map.credentialsId
def passwordVariable = map.passwordVariable
def usernameVariable = map.usernameVariable
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: usernameVariable, passwordVariable: passwordVariable]]) {
echo 'INSIDE withXCredentials'
echo env."${passwordVariable}"
echo env."${usernameVariable}"
body()
}
}
有了这个,您可以在您的管道中拥有以下内容:
node('name') {
withXCredentials([credentialsId: 'some-credential', passwordVariable: 'my_password',
usernameVariable: 'my_username']) {
echo 'Outside withXCredenitals'
checkout_some_code username: "$env.my_username", password: "$env.my_password"
}
}
我想在我的管道脚本中使用完全符合此语法的代码:
withXCredentials(id: 'some-cred-id', usernameVar: 'USER', passwordVar: 'PASS') {
//do some stuff with $USER and $PASS
echo "${env.USER} - ${env.PASS}"
}
请注意,您可以将要执行的 withXCredenitals
内的任何代码放入。 withXCredentials.groovy
驻留在 vars
文件夹下的我的 Jenkins 共享库中,它将使用
詹金斯原创 withCredentials
:
//withXCredentials.groovy
def userVar = params.usernameVar
def passwordVar = params.passwordVar
def credentialsId = params.credentialsId
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: usernameVar, passwordVariable: passwordVar]]) {
body()
}
我仍在学习高级 groovy 东西,但我不知道该怎么做。
请注意:
我的问题更多是关于 groovy 中的语法和使用 Closure
,答案 here 不是我想要的。使用该解决方案,我需要先实例化 class 然后调用该方法。所以我尽量避免做这样的事情:
new WithXCredentials(this).doSomthing(credentialsId, userVar, passwordVar)
在 Jenkins 文档中有一个使用闭包的例子:
// vars/windows.groovy
def call(Closure body) {
node('windows') {
body()
}
}
//the above can be called like this:
windows {
bat "cmd /?"
}
但是没有解释如何像这样传递参数
windows(param1, param2) {
bat "cmd /?"
}
见here
所以在网上搜索后我终于找到了答案。万一有人需要同样的东西。以下代码将起作用:
// filename in shared lib: /vars/withXCredentials.groovy
def call(map, Closure body) {
def credentialsId = map.credentialsId
def passwordVariable = map.passwordVariable
def usernameVariable = map.usernameVariable
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: usernameVariable, passwordVariable: passwordVariable]]) {
echo 'INSIDE withXCredentials'
echo env."${passwordVariable}"
echo env."${usernameVariable}"
body()
}
}
有了这个,您可以在您的管道中拥有以下内容:
node('name') {
withXCredentials([credentialsId: 'some-credential', passwordVariable: 'my_password',
usernameVariable: 'my_username']) {
echo 'Outside withXCredenitals'
checkout_some_code username: "$env.my_username", password: "$env.my_password"
}
}