JordanMazurke
JordanMazurke

Reputation: 1123

Jenkins ssh-agent unable to find credentials

I am trying to use ssh-agent to push commits to a remote git repository from a scripted Jenkins pipeline.

I have installed the plugin and have added the SSH private key to the Credentials section on Jenkins. THe following is a snippet from a Groovy class instantiated and called from the Jenkinsfile:

stages.sshagent(credentials:['26954b1f-2e32-4b4b-8923-3b7a9fbac6c9']) {
    stages.sh "git push origin ${branchName}"
}

Note:stages is just a representation of the stage within the Jenkinsfile. The class compiles fine but show the following error message each time:

FATAL: [ssh-agent] Could not find specified credentials

If I use the credentials with the git client then it works fine but unfortunately this does not support git push.

Upvotes: 9

Views: 11978

Answers (1)

Fulvio
Fulvio

Reputation: 965

This question has a few days(944) without answer and I think you may be already solved the problem, but just for the record. I think the problem you have/had its that you haven't indicated the credential ID in your code or the ID 26954b1f-2e32-4b4b-8923-3b7a9fbac6c9 isn't the correct one.

To add a private key to Jenkins, go to:

Jenkins > Credentials > System > Global credentials

Select SSH Username with private key then in ID field

Nota: you need to have the installed the SSH Agent Plugin

Jenkins-print-screen-ssh-agent-credential

ID:

An internal unique ID by which these credentials are identified from jobs and other configuration. Normally left blank, in which case an ID will be generated, which is fine for jobs created using visual forms. Useful to specify explicitly when using credentials from scripted configuration.

Groovy code sample

sshagent(['jenkins-ssh-qa']) {
    sh "ssh -o StrictHostKeyChecking=no [email protected] ${dockerRun}"
}

Upvotes: 10

Related Questions