Transfer files and execute commands from jenkins scripted pipeline to AWS instance 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 ubuntu@ec2-3-83-239-32.compute-1.amazonaws.com:'


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 ubuntu@ec2-x-83-xxx-32.compute-1.amazonaws.com:'


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 :)
Below is sample groovy code for sending file or execute command over SSH in jenkins: 

node(master)
{
  stage(Deploy)
  {
     sshPublisher(publishers: [sshPublisherDesc(configName: 'SERVER_NAME', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'apt-get update', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '*.war')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
  }
}