Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jenkins

groovy

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?

like image 277
Prathyush P Avatar asked Jul 26 '17 09:07

Prathyush P


People also ask

How do I use SSH credentials in Jenkins?

Add SSH Key inside JenkinsIn the dropdown, select 'SSH username with private key' and then give a name for it. Copy the private key from the Jenkins server. Now you can clone any git repo in this Jenkins instance. You do not need to provide the credentials while configuring the job in Jenkins.

How do I bind credentials in Jenkins?

In Jenkins, go to your pipeline project. Click Pipeline Syntax to open the Snippet Generator. From the Sample Step dropdown menu, select withCredentials: Bind credentials to variables. In the Bindings section, select Add > Username and password (separated).


1 Answers

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

like image 157
hayderimran7 Avatar answered Oct 03 '22 05:10

hayderimran7