0

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

2

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)])
  }
}
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

  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

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

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

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.

  • 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 Jan 11 at 9:54
  • @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 Jan 11 at 11:21
  • @shubash, I owe you :). Thank you so much. It worked as expected. – smc Jan 11 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.