如何检查 Jenkins 声明管道中的密码参数是否为空?
How to check if a password parameter is empty in a Jenkins declarative pipeline?
我在我的管道中进行了参数验证的第一步。针对所有不应为空的参数调用 isEmpty() 方法。不幸的是,isEmpty() 方法在被调用以检查“密码”类型参数时会生成异常。同样的方法适用于“选择”和“文本”参数类型。
异常
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: hudson.util.Secret.isEmpty() is applicable for argument types: () values: []
詹金斯文件
pipeline {
agent any
parameters {
choice(
choices: ["","TEST","PRODUCTION"],
description: 'Application',
name: 'ENV'
)
text(
name: 'FILE_LIST',
defaultValue: '',
description: 'Files'
)
password(
name: 'CREDENTIAL_ID',
defaultValue: '',
description: "Jenkins credential ID for application"
)
}
stages {
stage("Parameters check") {
steps {
script {
// This one is OK
if (params.ENV.isEmpty()) {
currentBuild.result = 'ABORTED'
error("ENV is empty")
}
// This one is also OK
if (params.FILE_LIST.isEmpty()) {
currentBuild.result = 'ABORTED'
error("FILE_LIST is empty")
}
// This throws an exception
if (params.CREDENTIAL_ID.isEmpty()) {
currentBuild.result = 'ABORTED'
error("CREDENTIAL_ID is empty")
}
}
}
}
}
}
如何检查密码参数是否为空以及记录在何处?
这个问题在于导出值的方式。
背景:
您尝试访问 params.CREDENTIAL_ID
类型 hudson.util.Secret
的变量 而不是 字符串,因此没有实现名为 isEmpty()
.[=19 的方法=]
另请查看文档,了解 hudson.util.Secret
:
变量类型提供的方法
解法:
要检查您的秘密是否为空,您可以使用
之一获取字符串
- 提供的
getPlainText()
方法,其中 returns 可以使用 isEmpty 方法检查的字符串。
if (params.CREDENTIAL_ID.getPlainText().isEmpty()) {
currentBuild.result = 'ABORTED'
error("CREDENTIAL_ID is empty")
}
- 提供(但已弃用)
toString()
方法(并再次返回字符串)
if (params.CREDENTIAL_ID.toString().isEmpty()) {
currentBuild.result = 'ABORTED'
error("CREDENTIAL_ID is empty")
}
注意:两种方式都将以明文形式获取secret。
我在我的管道中进行了参数验证的第一步。针对所有不应为空的参数调用 isEmpty() 方法。不幸的是,isEmpty() 方法在被调用以检查“密码”类型参数时会生成异常。同样的方法适用于“选择”和“文本”参数类型。
异常
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: hudson.util.Secret.isEmpty() is applicable for argument types: () values: []
詹金斯文件
pipeline {
agent any
parameters {
choice(
choices: ["","TEST","PRODUCTION"],
description: 'Application',
name: 'ENV'
)
text(
name: 'FILE_LIST',
defaultValue: '',
description: 'Files'
)
password(
name: 'CREDENTIAL_ID',
defaultValue: '',
description: "Jenkins credential ID for application"
)
}
stages {
stage("Parameters check") {
steps {
script {
// This one is OK
if (params.ENV.isEmpty()) {
currentBuild.result = 'ABORTED'
error("ENV is empty")
}
// This one is also OK
if (params.FILE_LIST.isEmpty()) {
currentBuild.result = 'ABORTED'
error("FILE_LIST is empty")
}
// This throws an exception
if (params.CREDENTIAL_ID.isEmpty()) {
currentBuild.result = 'ABORTED'
error("CREDENTIAL_ID is empty")
}
}
}
}
}
}
如何检查密码参数是否为空以及记录在何处?
这个问题在于导出值的方式。
背景:
您尝试访问 params.CREDENTIAL_ID
类型 hudson.util.Secret
的变量 而不是 字符串,因此没有实现名为 isEmpty()
.[=19 的方法=]
另请查看文档,了解 hudson.util.Secret
:
解法:
要检查您的秘密是否为空,您可以使用
- 提供的
getPlainText()
方法,其中 returns 可以使用 isEmpty 方法检查的字符串。if (params.CREDENTIAL_ID.getPlainText().isEmpty()) { currentBuild.result = 'ABORTED' error("CREDENTIAL_ID is empty") }
- 提供(但已弃用)
toString()
方法(并再次返回字符串)if (params.CREDENTIAL_ID.toString().isEmpty()) { currentBuild.result = 'ABORTED' error("CREDENTIAL_ID is empty") }
注意:两种方式都将以明文形式获取secret。