18
votes

We are using Jenkins server for our daily build process and executes some bash scripts on remote hosts over SSH. This scripts are generating html log files on remote hosts.

We are using Copy to slave plugin to copy files on slave machines and Publish over ssh plugin to manage SSH sessions in build process.

Now the question is, We want to copy some files (log files of Scripts) from remote ssh host to Jenkins Server. Which will be possible and better option for the same (plugin will be better if any).

EDIT :

sshpass is an option, but looking for any plugin or better way to do the job.

3
Hav you considered using an Archive Artifacts step? It would keep the logs associated with the build on the Jenkins server.gaige
the file is generated by execution script...it is not the jenkins log. Artifacts is available for this file also ??Not a bug
As long as it is created while the Jenkins job is running and you have access to it, it is an Artifact. In fact, those types of files (generated html, compiled binaries, etc) are exactly what Archive Artifacts is designed for.gaige
ok, will try to implement it tomorrow morning..thank you for your efforts..Not a bug
Care to share a more verbose description of your sshpass command solution?Randy

3 Answers

9
votes

use sshpass command to send file in

Build Environment -> Execute Shell script on remote host using ssh -> Post build script

sample command :

sshpass -p "password" scp path/of/file <new_server_ip>:/path/of/file

This will skip password prompt for scp command and will provide password to scp.

2
votes

I think you can generate ssh keypair and pass it to the slave as a parameter with, for example, Config File Provider Plugin

Then just use scp to retrieve files using this keypair for authentication process.

0
votes

Obviously way too late, but in case you're already using publish-over-ssh, want to avoid duplicating the credentials and have a shared library you can use this piece of groovy to obtain the host configuration:

import jenkins.plugins.publish_over_ssh.*

@NonCPS
def getSSHHost(name) {
  def found = null
  Jenkins.instance.getDescriptorByType(BapSshPublisherPlugin.Descriptor.class).each{
    it.hostConfigurations.each{host ->
      if (host.name == name) {
        found = host
      }
    }
  }

  found
}

As mentioned, this either requires a Global Shared Library (so that your code is trusted) or (probably) a number of admin approvals, sorry for that.

This returns a BapSshHostConfiguration.

For a password connection you can do:

def sshHost = getSSHHost('Configuration Name')
def host = [host: sshHost.hostname, user: sshHost.username, password: sshHost.password]
sshHost = null
sh("""
  set +x
  sshpass -p "${host.password}" scp -o StrictHostKeyChecking=no ${host.user}@${host.host}:filename.extension .
  set -x
""")

This copies the file to your local work directory. Probably not the best code ever, but I'm not a groovy specialist. It works and that is enough for me. (the set +x is to avoid it echoing the command in the log, showing the password). Getting rid of anything Non-CPS (sshHost = null) before you perform a CPS call saves you a lot of headaches :)

Since it took me quite a while to figure out I wanted to share this for whoever comes next.