I have a first step in my pipeline for parameters verification. isEmpty() method is called against all parameter that shouldn't be empty. Unfortunetly isEmpty() method generates an exception when it is called to check a "password" type parameter. Same method work well with "choice" and "text" parameters types.
Exception
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: hudson.util.Secret.isEmpty() is applicable for argument types: () values: []
jenkinsFile
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")
}
}
}
}
}
}
How to check if password parameter is empty and where is it documented?
This issues lays in the way the value is exported.
Background:
You try to access a variable params.CREDENTIAL_ID
type of hudson.util.Secret
which is not a String, thus has not a method implemented called isEmpty()
.
Also have a look at the documentation, ant the methods provided by variables types of hudson.util.Secret
:
Solution:
For checking if your secret is empty you can obtain the string by using either
getPlainText()
method which returns a string that can be checked by using the isEmpty method.
if (params.CREDENTIAL_ID.getPlainText().isEmpty()) {
currentBuild.result = 'ABORTED'
error("CREDENTIAL_ID is empty")
}
toString()
method (and returning a string again)
if (params.CREDENTIAL_ID.toString().isEmpty()) {
currentBuild.result = 'ABORTED'
error("CREDENTIAL_ID is empty")
}
Note: Both methods will obtain the secret in a plain text.