<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>

CC BY-SA 4.0
2019-01-12T10:22:18.837Z
2019-01-12T10:22:18.837Z
Jay
<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>

CC BY-SA 4.0
2021-02-24T17:59:47.043Z
@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
DJ_Stuffy_K
CC BY-SA 4.0
2019-01-11T09:54:55.837Z
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.
smc
CC BY-SA 4.0
2019-01-11T11:21:15.423Z
@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.
Subhash
CC BY-SA 4.0
2019-01-11T13:32:26.330Z
@shubash, I owe you :). Thank you so much. It worked as expected.
smc
CC BY-SA 4.0
2019-01-11T09:41:25.577Z
2019-01-11T11:19:47.920Z
2020-06-18T08:28:10.803Z
Subhash
<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>

<jenkins><amazon-web-services><jenkins-pipeline><amazon-ec2>
Transfer files and execute commands from jenkins scripted pipeline to AWS instance
smc
# 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.
Configure

Click “Manage Jenkins”
Click “Configure System”
Go to “Publish over SSH” section
Enter “/Users/Shared/Jenkins/.ssh/id_rsa” to “Path to Key”
Click “Add” at “SSH Servers”
Enter any logical name to “Name”
Enter IP Address or Hostname of the server to “Hostname”
Enter the user name to log in to “Username”
Enter any directory to “Remote Directory”
Click “Test Configuration”
Click “Save” at bottom of the page


Create a job

Create or copy a job that can build the WAR correctly.
As I wrote in How to specify a Git tag to be processed, make a job to can specify a
tag to be processed.
Click “Add post-build action”
Click “Send build artifacts over SSH”
Enter “Source files”
Enter “Remove prefix”
Enter “Exec command”


For More reference:

https://nozaki.me/roller/kyle/entry/articles-jenkins-sshdeploy
https://medium.com/@weblab_tech/how-to-publish-artifacts-in-jenkins-f021b17fde71
https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin

you can also do the same thing in pipeline script, see below images:

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  

---

