HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to check if pipeline parameter is empty

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
pipelineemptyhowcheckparameter

Problem

I have a pipeline job which includes some parameters:

In the pipeline file I have the below code:

stage ("create bundle"){
            steps{
                script{
                        amd_distribution_create_bundle credential_id: params.DISTRIBUTION_CREDENTIAL_ID, distribution_url: params.DISTRIBUTION_URL, gps_credential_id: params.GPG_PASSPHRASE, bundle_name: params.BUNDLE_NAME, bundle_version: BUNDLE_VERSION
                }
            }
        }


how can I ask before calling the groovy method 'amd_distribution_create_bundle', if some field is empty?

Solution

While creating the credentials parameter in jenkins job, you can specify required: true, then jenkins should validate the credentials paramter.

parameters {
    credentials(name: 'GPG_PASSPHRASE', defaultValue: '', credentialType: "Username with password", required: true )
}


You can also check the details mentioned in the link https://docwhat.org/jenkins-user-credentials

Adding Parameter

/* EXAMPLE */
parameters {
    credentials(
        credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl',
        defaultValue: '',
        description: 'The credentials needed to deploy.',
        name: 'deployCredentialsId',
        required: true
    )
}


Using in the stage

/* EXAMPLE */
steps {
    withCredentials([usernamePassword(
        credentialsId: '${deployCredentialsId}',
        usernameVariable: 'DEPLOY_USERNAME',
        passwordVariable: 'DEPLOY_PASSWORD',
    )]) {
        sh './my-command.bash --username="${DEPLOY_USERNAME}" --password="${DEPLOY_PASSWORD}"'
    }
}


If you just want to check if the variable is empty or not, you can check isEmpty() in groovy script. Your code can be as follows:

stage ("create bundle"){
        steps{
            script{
                    if ( GPG_PASSPHRASE.isEmpty()​​ ) {
                             GPG_PASSPHRASE = 'custom_string'
                     }
                    amd_distribution_create_bundle credential_id: params.DISTRIBUTION_CREDENTIAL_ID, distribution_url: params.DISTRIBUTION_URL, gps_credential_id: params.GPG_PASSPHRASE, bundle_name: params.BUNDLE_NAME, bundle_version: BUNDLE_VERSION
            }
        }
    }

Code Snippets

parameters {
    credentials(name: 'GPG_PASSPHRASE', defaultValue: '', credentialType: "Username with password", required: true )
}
/* EXAMPLE */
parameters {
    credentials(
        credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl',
        defaultValue: '',
        description: 'The credentials needed to deploy.',
        name: 'deployCredentialsId',
        required: true
    )
}
/* EXAMPLE */
steps {
    withCredentials([usernamePassword(
        credentialsId: '${deployCredentialsId}',
        usernameVariable: 'DEPLOY_USERNAME',
        passwordVariable: 'DEPLOY_PASSWORD',
    )]) {
        sh './my-command.bash --username="${DEPLOY_USERNAME}" --password="${DEPLOY_PASSWORD}"'
    }
}
stage ("create bundle"){
        steps{
            script{
                    if ( GPG_PASSPHRASE.isEmpty()​​ ) {
                             GPG_PASSPHRASE = 'custom_string'
                     }
                    amd_distribution_create_bundle credential_id: params.DISTRIBUTION_CREDENTIAL_ID, distribution_url: params.DISTRIBUTION_URL, gps_credential_id: params.GPG_PASSPHRASE, bundle_name: params.BUNDLE_NAME, bundle_version: BUNDLE_VERSION
            }
        }
    }

Context

StackExchange DevOps Q#9869, answer score: 2

Revisions (0)

No revisions yet.