Answers: <p>While creating the credentials parameter in jenkins job, you can specify <code>required: true</code>, then jenkins should validate the credentials paramter.</p>
  
  <pre><code>parameters {
      credentials(name: 'GPG_PASSPHRASE', defaultValue: '', credentialType: "Username with password", required: true )
  }
  </code></pre>
  
  <p>You can also check the details mentioned in the link <a href="https://docwhat.org/jenkins-user-credentials" rel="nofollow noreferrer">https://docwhat.org/jenkins-user-credentials</a></p>
  
  <p>Adding Parameter</p>
  
  <pre><code>/* EXAMPLE */
  parameters {
      credentials(
          credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl',
          defaultValue: '',
          description: 'The credentials needed to deploy.',
          name: 'deployCredentialsId',
          required: true
      )
  }
  </code></pre>
  
  <p>Using in the stage</p>
  
  <pre><code>/* EXAMPLE */
  steps {
      withCredentials([usernamePassword(
          credentialsId: '${deployCredentialsId}',
          usernameVariable: 'DEPLOY_USERNAME',
          passwordVariable: 'DEPLOY_PASSWORD',
      )]) {
          sh './my-command.bash --username="${DEPLOY_USERNAME}" --password="${DEPLOY_PASSWORD}"'
      }
  }
  </code></pre>
  
  <p>If you just want to check if the variable is empty or not, you can check <code>isEmpty()</code> in groovy script. Your code can be as follows:</p>
  
  <pre><code>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></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-20T19:06:08.820Z
Answers: 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?
Answers: arielma
Answers: CC BY-SA 4.0
Answers: 2019-11-20T19:15:18.653Z
Answers: updated the answer to include the empty check as well.
Answers: Samit
Answers: CC BY-SA 4.0
Answers: 2019-11-20T23:57:12.703Z
Answers: I modified the question for a specific area.
Answers: arielma
Answers: CC BY-SA 4.0
Answers: 2019-11-21T05:07:42.303Z
Answers: 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.
Answers: Samit
Answers: CC BY-SA 4.0
Answers: 2019-11-21T05:28:36.867Z
Answers: Ok sorry. Will re edit this one again, accept the answer, and raise new one
Answers: arielma
Answers: CC BY-SA 4.0
Answers: 2022-04-05T13:26:26.467Z
Answers: Note that if you copy-paste the last code block in this answer you may get an invalid close-paren (it looks right but gets an error about a missing expected close-paren)
Answers: ShadSterling
Answers: CC BY-SA 4.0
Answers: 2019-11-20T18:56:51.530Z
Answers: 2019-11-20T19:13:02.637Z
Answers: 2019-11-20T19:13:02.637Z
Answers: Samit
Body: <p>I have a pipeline job which includes some parameters:
  <a href="https://i.sstatic.net/Z3SoQ.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Z3SoQ.jpg" alt="Pipeline"></a></p>
  
  <p>In the pipeline file I have the below code:</p>
  
  <pre><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
                  }
              }
          }
  </code></pre>
  
  <p>how can I ask before calling the groovy method 'amd_distribution_create_bundle', if some field is empty?</p>
  
Tags: <jenkins-pipeline>
Title: How to check if pipeline parameter is empty
UserDisplayName: arielma
ThreadText: # How to check if pipeline parameter is empty
  
  (Asked by: username_0 on 2019-11-20)
  
  I have a pipeline job which includes some parameters:
  [![Pipeline](https://i.sstatic.net/Z3SoQ.jpg)](https://i.sstatic.net/Z3SoQ.jpg)
  
  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?
  
  **Tags:** jenkins-pipeline
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-20. Score: 2 | Accepted answer
  
  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
              }
          }
      }
  
  ```
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_1**: updated the answer to include the empty check as well.  
  > **username_0**: I modified the question for a specific area.  
  > **username_1**: 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.  
  > **username_0**: Ok sorry. Will re edit this one again, accept the answer, and raise new one  
  > **username_2**: Note that if you copy-paste the last code block in this answer you may get an invalid close-paren (it looks right but gets an error about a missing expected close-paren)  
  
  ---
  
  