How to call private git repository with username and password in Jenkins pipeline code



we can use jenkins pipeline to clone git repositories to your workingspace. for that you need to use following pipeline code.

  node {   
  //checkout from development   
  stage 'checkout'   
  git url: 'https://private/repository/usrl.git' , branch: 'master'     
  }  


but if you need to access private repository of git or other like stash you can use this code as follow.

 node {  
 //checkout from development  
 stage 'checkout'  
 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {  
 git url: 'https://user@mycompany.org/stash/scm/devops/automation_framework.git' , branch: 'master'   
  }  
 }  

to use this code you need to add credentialsId to jenkins. follow below steps.
Go to jenkins homepage and click Credentials


then clikc Jenkins store.



then click "Global credentials"



and add your credentials along with ID. this ID should be same as pipeline code credentialsId.





Comments

Popular Posts