7

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.

1 Answer 1

8

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}"
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.