Answers: <p>Below is sample groovy code for sending file or execute command over SSH in jenkins: </p>
  
  <pre><code>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)])
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-12T10:22:18.837Z
Answers: 2019-01-12T10:22:18.837Z
Answers: Jay
Answers: <p>You can use '<strong>Publish Over SSH plugin</strong>'. Using this plugin you can send files and execute the command on the remote server.</p>
  <p><a href="https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin" rel="nofollow noreferrer">Click Here For install plugin.</a></p>
  <h1>Configure</h1>
  <ol>
  <li>Click “Manage Jenkins”</li>
  <li>Click “Configure System”</li>
  <li>Go to “Publish over SSH” section</li>
  <li>Enter “/Users/Shared/Jenkins/.ssh/id_rsa” to “Path to Key”</li>
  <li>Click “Add” at “SSH Servers”</li>
  <li>Enter any logical name to “Name”</li>
  <li>Enter IP Address or Hostname of the server to “Hostname”</li>
  <li>Enter the user name to log in to “Username”</li>
  <li>Enter any directory to “Remote Directory”</li>
  <li>Click “Test Configuration”</li>
  <li>Click “Save” at bottom of the page</li>
  </ol>
  <p><a href="https://i.sstatic.net/mHsAV.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mHsAV.png" alt="enter image description here" /></a></p>
  <h1>Create a job</h1>
  <ol>
  <li>Create or copy a job that can build the WAR correctly.</li>
  <li>As I wrote in How to specify a Git tag to be processed, make a job to can specify a
  tag to be processed.</li>
  <li>Click “Add post-build action”</li>
  <li>Click “Send build artifacts over SSH”</li>
  <li>Enter “Source files”</li>
  <li>Enter “Remove prefix”</li>
  <li>Enter “Exec command”</li>
  </ol>
  <p><a href="https://i.sstatic.net/rlpr2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rlpr2.png" alt="enter image description here" /></a></p>
  <p>For More reference:</p>
  <ol>
  <li><a href="https://nozaki.me/roller/kyle/entry/articles-jenkins-sshdeploy" rel="nofollow noreferrer">https://nozaki.me/roller/kyle/entry/articles-jenkins-sshdeploy</a></li>
  <li><a href="https://medium.com/@weblab_tech/how-to-publish-artifacts-in-jenkins-f021b17fde71" rel="nofollow noreferrer">https://medium.com/@weblab_tech/how-to-publish-artifacts-in-jenkins-f021b17fde71</a></li>
  <li><a href="https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin" rel="nofollow noreferrer">https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin</a></li>
  </ol>
  <p>you can also do the same thing in pipeline script, see below images:
  <a href="https://i.sstatic.net/XdynF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XdynF.png" alt="enter image description here" /></a></p>
  <pre><code>node(master)
  {
    stage(Deploy)
    {
       sshPublisher(publishers: [sshPublisherDesc(configName: 'LAB-35', 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)])
    }
  }
  </code></pre>
  <p>Here configName: 'LAB-35' is your remote ssh server.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-02-24T17:59:47.043Z
Answers: @Subhash  in mycase Jenkins is in aws account 1 and ec2 is in aws account 2 ; what is the best way to copy files from jenkins workspace to the ec2 account
Answers: DJ_Stuffy_K
Answers: CC BY-SA 4.0
Answers: 2019-01-11T09:54:55.837Z
Answers: Thanks for the answer @shubhas, but I'm looking for solutions which fit in scripted pipeline. I guess Publish over ssh only work in freestyle jobs.
Answers: smc
Answers: CC BY-SA 4.0
Answers: 2019-01-11T11:21:15.423Z
Answers: @smc you can also do the same thing in pipeline script please read the answer again I add pipeline script for send files and execute a command on the remote server.
Answers: Subhash
Answers: CC BY-SA 4.0
Answers: 2019-01-11T13:32:26.330Z
Answers: @shubash, I owe you :). Thank you so much. It worked as expected.
Answers: smc
Answers: CC BY-SA 4.0
Answers: 2019-01-11T09:41:25.577Z
Answers: 2019-01-11T11:19:47.920Z
Answers: 2020-06-18T08:28:10.803Z
Answers: Subhash
Body: <p>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</p>
  
  <p>1) Using hardcoded pem file
  I have following scripted pipeline to transfer files from Jenkins pipeline to AWS EC2 instance.</p>
  
  <pre><code>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:'
  </code></pre>
  
  <p>However it is not working, and waiting for a long time without showing logs.</p>
  
  <p>2) Using Jenkins "credential"</p>
  
  <p>I have added the pem file in Jenkins credential, and then invoked the credential by the following method. (Referred from - <a href="https://stackoverflow.com/a/47627460/9403545">https://stackoverflow.com/a/47627460/9403545</a>)</p>
  
  <pre><code>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:'
  </code></pre>
  
  <p>Unfortunately, the above method also didn't work and got the following error.</p>
  
  <blockquote>
    <p>/tmp/../Hello directory doesn't exist.</p>
  </blockquote>
  
  <p>I have tried to use Publish over SSH, but it doesn't seem to be supported in scripted pipeline.</p>
  
  <p>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 :)</p>
  
Tags: <jenkins><amazon-web-services><jenkins-pipeline><amazon-ec2>
Title: Transfer files and execute commands from jenkins scripted pipeline to AWS instance
UserDisplayName: smc
ThreadText: # Transfer files and execute commands from jenkins scripted pipeline to AWS instance
  
  (Asked by: username_0 on 2019-01-11)
  
  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 :)
  
  **Tags:** jenkins, amazon-web-services, jenkins-pipeline, amazon-ec2
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-01-12. Score: 2 | Accepted answer
  
  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)])
    }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2019-01-11. Score: 1
  
  You can use '**Publish Over SSH plugin**'. Using this plugin you can send files and execute the command on the remote server.
  
  [Click Here For install plugin.](https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin)
  
  # Configure
  
  1. Click “Manage Jenkins”
  2. Click “Configure System”
  3. Go to “Publish over SSH” section
  4. Enter “/Users/Shared/Jenkins/.ssh/id\_rsa” to “Path to Key”
  5. Click “Add” at “SSH Servers”
  6. Enter any logical name to “Name”
  7. Enter IP Address or Hostname of the server to “Hostname”
  8. Enter the user name to log in to “Username”
  9. Enter any directory to “Remote Directory”
  10. Click “Test Configuration”
  11. Click “Save” at bottom of the page
  
  [![enter image description here](https://i.sstatic.net/mHsAV.png)](https://i.sstatic.net/mHsAV.png)
  
  # Create a job
  
  1. Create or copy a job that can build the WAR correctly.
  2. As I wrote in How to specify a Git tag to be processed, make a job to can specify a
     tag to be processed.
  3. Click “Add post-build action”
  4. Click “Send build artifacts over SSH”
  5. Enter “Source files”
  6. Enter “Remove prefix”
  7. Enter “Exec command”
  
  [![enter image description here](https://i.sstatic.net/rlpr2.png)](https://i.sstatic.net/rlpr2.png)
  
  For More reference:
  
  1. <https://nozaki.me/roller/kyle/entry/articles-jenkins-sshdeploy>
  2. <https://medium.com/@weblab_tech/how-to-publish-artifacts-in-jenkins-f021b17fde71>
  3. <https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin>
  
  you can also do the same thing in pipeline script, see below images:
  [![enter image description here](https://i.sstatic.net/XdynF.png)](https://i.sstatic.net/XdynF.png)
  
  ```
  node(master)
  {
    stage(Deploy)
    {
       sshPublisher(publishers: [sshPublisherDesc(configName: 'LAB-35', 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)])
    }
  }
  
  ```
  
  Here configName: 'LAB-35' is your remote ssh server.
  
  > **Comments:**
  > **username_0**: Thanks for the answer @shubhas, but I'm looking for solutions which fit in scripted pipeline. I guess Publish over ssh only work in freestyle jobs.  
  > **username_2**: @username_0 you can also do the same thing in pipeline script please read the answer again I add pipeline script for send files and execute a command on the remote server.  
  > **username_0**: @shubash, I owe you :). Thank you so much. It worked as expected.  
  > **username_3**: @username_2  in mycase Jenkins is in aws account 1 and ec2 is in aws account 2 ; what is the best way to copy files from jenkins workspace to the ec2 account  
  
  ---
  
  