I made a multibranch pipeline project in Jenkins. I need to use two repositories and both need credentials.
I created a Jenkinsfile in repository1:
n
What kind of credentials do you use?
I suggest that you use SSH credentials (i.e. private/public keys):
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://BRNTZN@bitbucket.org: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.
As stated in Pipeline plugin tutorial, for a multibranch project, you don't have to specify the repository in the node. Just use checkout scm
.
Your GitSCM class instantiation is incorrect. You have created two UserRemoteConfig objects - one with a URL of 'git@bitbucket.org: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: 'git@bitbucket.org:BRNTZN/repository2.git'],[credentialsId:'jenkinsmaster']]
])
Should be:
checkout([
$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'git@bitbucket.org: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.