I want to transfer files and execute a few commands on remote AWS instance from Jenkins. I have the following methods in mind to do the same. Note that my Jenkins server is running in the local server, and I don't have SSH access to Jenkins
1) Using hardcoded pem file I have following scripted pipeline to transfer files from Jenkins pipeline to AWS EC2 instance.
stage('transfer docker image to ec2') {
sh 'scp -oIdentityFile=/var/lib/jenkins/.key.pem docker_image.tar [email protected]:'
However it is not working, and waiting for a long time without showing logs.
2) Using Jenkins "credential"
I have added the pem file in Jenkins credential, and then invoked the credential by the following method. (Referred from - https://stackoverflow.com/a/47627460/9403545)
withCredentials([sshUserPrivateKey(credentialsId: "bindu-test", keyFileVariable: 'keyfile')]) {
stage('transfer docker image to ec2') {
sh 'scp -i ${keyfile} test [email protected]:'
Unfortunately, the above method also didn't work and got the following error.
/tmp/../Hello directory doesn't exist.
I have tried to use Publish over SSH, but it doesn't seem to be supported in scripted pipeline.
Is there any way I can do this? P.S: Please keep in mind that I'm AWS newbie, it would be nice if the answer is in detail :)