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?
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With