0

I have a pipeline job which includes some parameters: Pipeline

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?

1

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
            }
        }
    }
  • in my case it's not required, it can be empty. but I need to do some actions in that case, so how can I verify? – arielma Nov 20 '19 at 19:06
  • updated the answer to include the empty check as well. – Samit Nov 20 '19 at 19:15
  • I modified the question for a specific area. – arielma Nov 20 '19 at 23:57
  • You should have asked a new question or could have asked in comments rather than updating your complete question. For your previous question, the above answer was accepted. However, now you have updated the question, the answer doesn't make sense here. – Samit Nov 21 '19 at 5:07
  • Ok sorry. Will re edit this one again, accept the answer, and raise new one – arielma Nov 21 '19 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.