4

I am using the git plugin, which supposedly integrates wih the "Jenkins Credentials Management functionality":

Credentials: Credentials to use to connect to the repository (unless anonymous access is allowed), using the Jenkins Credentials Management functionality. The type of credentials used depends on the underlying protocol. For SSH connections only private key authentication is supported.

Great, the "Jenkins Credentials Management functionality" is working for me: I am able to run "Pipeline script from SCM".

So, how do I use those credentials with the git plugin? There are no examples neither in the git plugin documentation, nor in the web. This is the relevant part of my Jenkinsfile, with the git step:

node {
    stage('Checkout') {
        git url: 'ssh://git@5.6.7.8:5999/my/repo.git', branch: 'wip'
    }
    ...
}
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • Tried going to `Jenkins > Credentials > System > Global credentials > Add Credentials` then selecting `SSH Username with private key`? Then in your Jenkins job, you just reference those Credentials when setting up the job. You don't have to use Global credentials, you can use a different domain. – Harmelodic Sep 22 '16 at 09:53
  • @Harmelodic: As said, credentials are added, and they are working. I just do not know how to reference them from the git command in the Checkout stage of the Jenkinsfile running the Pipeline. – blueFast Sep 22 '16 at 10:14
  • I did not test this but according to the [documentation](https://github.com/jenkinsci/workflow-scm-step-plugin) (scroll to the end), you can use a `GenericSCMStep` and supply a _credentialId_: `checkout scm: [$class: 'MercurialSCM', source: 'ssh://hg@bitbucket.org/user/repo', clean: true, credentialsId: '1234-5678-abcd'], poll: false`. Alternatively you have [this sample](https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.Groovy) using command line git client and `Credentials Binding Plugin`, also untested. – Morfic Sep 22 '16 at 12:39
  • @delavnog have you been able to figure this out? I can't get it to use my credentials for ssh either – Explosion Pills Sep 28 '16 at 19:47

1 Answers1

1

They seem to have added some documentation in the meantime: https://jenkins.io/doc/pipeline/steps/git/

So for your example the following should work:

checkout([$class: 'GitSCM', branches: [[name: '*/wip']],
    userRemoteConfigs: [[url: 'ssh://git@5.6.7.8:5999/my/repo.git',
    credentialsId: 'your-credentials-id']]])
jansohn
  • 2,246
  • 2
  • 28
  • 40