Answers: <p>withCredentials publish environment variables, and in sh to access the environment variables, you have to do it like this: &quot;${env.JOB_BASE_NAME}&quot;.</p>
  <p>Try this:</p>
  <pre><code>withCredentials([usernamePassword(credentialsId: params.git_credential, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
     sh 'git reset --hard; git clone https://${env.DOCKER_USERNAME}:${env.DOCKER_PASSWORD}@repo_url/scm/${params.repository}.git --branch master'
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-04-05T15:39:57.663Z
Answers: 2021-04-05T15:39:57.663Z
Answers: Damien
Body: <p>I am trying to use the credentials parameter for a git clone.  But i am getting the error that the variables are not found</p>
  <p>Param definition</p>
  <pre><code>  credentials (credentialType: 'Username with password', defaultValue: 'fcb2d7c3-4b35-4ef2-bdf0-24fc4ff1137c', description: 'Git credential', name: 'git_credential', required: true)
  </code></pre>
  <p>Usage in stage</p>
  <pre><code>withCredentials([usernamePassword(credentialsId: params.git_credential, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
  sh 'git reset --hard; git clone https://${DOCKER_USERNAME}:${DOCKER_PASSWORD}@repo_url/scm/${params.repository}.git --branch master'
  </code></pre>
  <blockquote>
  <p>Error: Wrong variable used</p>
  </blockquote>
  
Tags: <jenkins><jenkins-pipeline><jenkinsfile><groovy><credentials>
Title: Jenkinsfile credential as paramter unable to use it
UserDisplayName: Vini
ThreadText: # Jenkinsfile credential as paramter unable to use it
  
  (Asked by: username_0 on 2021-04-01)
  
  I am trying to use the credentials parameter for a git clone. But i am getting the error that the variables are not found
  
  Param definition
  
  ```
    credentials (credentialType: 'Username with password', defaultValue: 'fcb2d7c3-4b35-4ef2-bdf0-24fc4ff1137c', description: 'Git credential', name: 'git_credential', required: true)
  
  ```
  
  Usage in stage
  
  ```
  withCredentials([usernamePassword(credentialsId: params.git_credential, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
  sh 'git reset --hard; git clone https://${DOCKER_USERNAME}:${DOCKER_PASSWORD}@repo_url/scm/${params.repository}.git --branch master'
  
  ```
  
  > Error: Wrong variable used
  
  **Tags:** jenkins, jenkins-pipeline, jenkinsfile, groovy, credentials
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-04-05. Score: 1
  
  withCredentials publish environment variables, and in sh to access the environment variables, you have to do it like this: "${env.JOB\_BASE\_NAME}".
  
  Try this:
  
  ```
  withCredentials([usernamePassword(credentialsId: params.git_credential, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
     sh 'git reset --hard; git clone https://${env.DOCKER_USERNAME}:${env.DOCKER_PASSWORD}@repo_url/scm/${params.repository}.git --branch master'
  }
  
  ```
  
  
  ---
  
  