Bharadwaj V
Bharadwaj V

Reputation: 29

How to enter git credentials in jenkins pipeline code

Am having a Jenkins server running on GCP-Linux Vm, I want to access a code from git's private repo, am trying to access it from my Jenkins pipeline code, how can I enter my git credentials in Jenkin pipeline code?

Upvotes: 3

Views: 10863

Answers (1)

TudorIftimie
TudorIftimie

Reputation: 1140

you should share the pipeline you use for more specific answers.

As a general answer:

  • If you use only one repo ( the one you are building ) you don't need to call the credentials in the pipeline. Just configure your credentials in jenkins credentials area and your job like this:

enter image description here

In this case no special reference in pipeline code is necessary

  • If you want to clone a second repository in your build you can use inside your pipeline:

    pipeline {

      environment {
      gitCredentialId = 'Jenkins-Bitbucket' //defined in credentials area
      gitUrl = 'https://bitbucket.org/companyNameHere/repoNameHere.git'
      deployBranch = 'branch-name-here'
      }
      stages {
      stage('Cloning Git') {
          steps {
              git(
              url: gitUrl,
              credentialsId: gitCredentialId,
              branch: deployBranch
          )
          }
      }
    

    }

  • to store credentials: manage jenkins > manage credentials > click on global domain > add credential . Make sure to put meaningful description and ids as this area tends to become a mess in time and it is hard to cleanup as you will never know what can be deleted.

enter image description here

Each credential will tell you where it is used:

enter image description here

Upvotes: 2

Related Questions