Checkout Jenkins Pipeline Git SCM With Credentials?


Answer :

You can use the following in a pipeline:

git branch: 'master',     credentialsId: '12345-1234-4696-af25-123455',     url: 'ssh://git@bitbucket.org:company/repo.git' 

If you're using the ssh url then your credentials must be username + private key. If you're using the https clone url instead of the ssh one, then your credentials should be username + password.


To explicitly checkout using a specific credentials

    stage('Checkout external proj') {         steps {             git branch: 'my_specific_branch',                 credentialsId: 'my_cred_id',                 url: 'ssh://git@test.com/proj/test_proj.git'              sh "ls -lat"         }     } 

To checkout based on the configred credentials in the current Jenkins Job

    stage('Checkout code') {         steps {             checkout scm         }     } 

You can use both of the stages within a single Jenkins file.


If you want to use ssh credentials,

  git(        url: 'git@github.com<repo_name>.git',        credentialsId: 'xpc',        branch: "${branch}"     ) 

if you want to use username and password credentials, you need to use http clone as @Serban mentioned.

    git(        url: 'https://github.com/<repo_name>.git',        credentialsId: 'xpc',        branch: "${branch}"     ) 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?