Search code examples
jenkinsjenkins-pipeline

How to check if a password parameter is empty in a Jenkins declarative pipeline?


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?


Solution

  • 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

    1. the provided 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")
      }  
      
    2. the providede (but deprecated) 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.