Prathyush P
Prathyush P

Reputation: 413

How to use credential type "SSH Username with private key" inside jenkinsfile for pipeline job

I'm trying to run remote commands using ssh from jenkinsfile. For this I'm using *.pem file for accessing the remote machine. I already have jenkins credentials created with the type "SSH Username with private key" inside jenkins.

Is there any way that i can use that credential inside jenkinsfile instead of giving sh "ssh -i *.pem username@hostname command" to authenticate?

Upvotes: 9

Views: 23734

Answers (1)

hayderimran7
hayderimran7

Reputation: 580

No, you would need to use Jenkins Credentials Binding Plugin. Basically you create a binding from the key file, set the key as any variable , lets say mykey and you can use the key in your build scripts as cat mykey. Or store in your build script as:

cat mykey > sshkey
chmod 600 sshkey
eval `ssh-agent -s`
ssh-add sshkey

and then you can ssh since the ssh key is added This post lays all features of Jenkins Credentials Binding plugin really good https://support.cloudbees.com/hc/en-us/articles/203802500-Injecting-Secrets-into-Jenkins-Build-Jobs

Upvotes: 16

Related Questions