Using credentials from Jenkins store in a jenkinsfile

Your GitSCM class instantiation is incorrect. You have created two UserRemoteConfig objects - one with a URL of '[email protected]:BRNTZN/repository2.git' and one with a credentialsId of 'jenkinsmaster'. Instead you want one object with both properties set.

checkout([
  $class: 'GitSCM', branches: [[name: '*/master']],
  userRemoteConfigs: [[url: '[email protected]:BRNTZN/repository2.git'],[credentialsId:'jenkinsmaster']]
])

Should be:

checkout([
  $class: 'GitSCM', branches: [[name: '*/master']],
  userRemoteConfigs: [[url: '[email protected]:BRNTZN/repository2.git',credentialsId:'jenkinsmaster']]
])

Notice there are no brackets around the comma in the "userRemoteConfigs" section in the second case.

I had just ran into the same issue and connected up an Eclipse debugger to Jenkins to find the issue.

See git-plugin GitSCM does not support ssh credentials when using checkout in a Jenkinsfile (45007).


I've had the exact same issue: checkout using credentials in a freestyle project works fine, checkout in a shell (as the jenkins user) works fine, and checkout in the pipeline fails. I've updated Jenkins + plugins to the latest version.

I finally managed to get it to work by placing the correct key in /var/lib/jenkins/.ssh/id_rsa. It looks like the GitSCM plugin completely ignores the provided credentialsId, and just uses the key in /var/lib/jenkins/.ssh/id_rsa. I generated a keypair without passphrase for this.

It is a workaround, and I suspect that GitSCM has a bug, but at least you can use the pipeline plugin.


What kind of credentials do you use?

I suggest that you use SSH credentials (i.e. private/public keys):

  1. Generate a SSH key pair (make sure you generate it for the correct username!)
  2. Add your public SSH key to your Bitbucket account
  3. Configure your Jenkins to use your newly created SSH private key, as shown in the example below:

Enter image description here

Then you need to use SSH URL as connection to your Git your credentials in your pipeline (instead of HTTP URL), as follows:

checkout([
    $class: 'GitSCM', branches: [[name: '*/master']],
    userRemoteConfigs: [[url:'ssh://[email protected]:BRNTZN/repository2.git'],[credentialsId:'jenkins_ssh_key']]
])

Also, note that you might want to set a specific id for your credentials (e.g. jenkins_ssh_key or BRNTZN_ssh_key) to improve readability and simplify pipeline configuration.