Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-45007

git-plugin GitSCM does not support ssh credentials when using checkout in a Jenkinsfile

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: Major Major
    • git-plugin
    • None
    • Jenkins 2.7.1
      Git plugin 2.5.2
      Pipeline 2.3

      It seems using the GitSCM class with the checkout step does not work with ssh credentials. Using the GitSCM class directly with checkout is sometimes needed in order to be able to access more advanced features.

      This is mentioned on stack overflow here:
      https://stackoverflow.com/questions/39451345/using-credentials-from-jenkins-store-in-a-jenkinsfile

      Example:

      checkout(
      checkout(
      	[$class: 'GitSCM', 
      		branches: [[name: "${branchName}"]], 
      		doGenerateSubmoduleConfigurations: false, 
      		extensions: [
      			[$class: 'RelativeTargetDirectory', relativeTargetDir: "${scriptsCheckoutDir}" ],
      			[$class: 'CloneOption', noTags: true, reference: '', shallow: true]], 
      		submoduleCfg: [], 
      		userRemoteConfigs: [
      			[url: "${gitUrl}" ],
      			[credentialsId:'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx']
      		]])
      

      Output:

      ERROR: Error cloning remote repo 'origin'
      hudson.plugins.git.GitException: Command "C:\git\bin\git.exe fetch --no-tags --progress ssh://git@host/foo/bar.git +refs/heads/*:refs/remotes/origin/* --depth=1" returned status code 128:
      stdout: 
      stderr: Permission denied (publickey).
      fatal: Could not read from remote repository.
      
      Please make sure you have the correct access rights
      and the repository exists.
      

      Looking at the code it appears createClient only looks for StandardUsernameCredentials:
      https://github.com/jenkinsci/git-plugin/blob/master/src/main/java/hudson/plugins/git/GitSCM.java#L746

      It never attempts to use SSHUserPrivateKey even though that is supported by the git-client-plugin:
      https://github.com/jenkinsci/git-client-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java#L51

            Unassigned Unassigned
            mrichar2 Mark R
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: