Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ssh credentials in jenkins pipeline with ssh, scp or sftp

I want to use scp/ssh to upload some files to a server. I discover that I need to use certificate-based authentication, but the question is how? Really what I want to do is to use the same sort of credentials I use with git - passworded ssh cert stored in Jenkins. However, I can't work out how to - the snippet generator has no obvious option for that.

What do others do? Is there an undocumented feature that would do this?

like image 273
johnfo Avatar asked Jun 05 '17 20:06

johnfo


People also ask

How do I use Jenkins credentials in Jenkins?

From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click Manage Jenkins > Manage Credentials. Under Stores scoped to Jenkins on the right, click on Jenkins. Under System, click the Global credentials (unrestricted) link to access this default domain. Click Add Credentials on the left.

What is the use of ssh-agent in Jenkins?

SSH Agent. The SSH Agent plugin enables you to inject credentials - SSH private keys - into the build jobs using an SSH Agent. The build can run on any node, the Jenkins controller will provide it with the set of credentials.


1 Answers

    withCredentials([sshUserPrivateKey(credentialsId: "yourkeyid", keyFileVariable: 'keyfile')]) {        stage('scp-f/b') {         sh "scp -i ${keyfile} do sth here"        }    } 

Maybe this is what you want. Install Credentials Plugin and
Credentials Binding Plugin. Add some credentials and then you will get "yourkeyid", bind this credentials to keyFileVariable, passwordVariable etc.

More details and documentation can be found on the Github site of the Jenkins Credentials Binding Plugin, Credentials Plugin, SSH Pipeline Steps plugin

like image 185
tianzhipeng Avatar answered Oct 11 '22 06:10

tianzhipeng