I am trying to copy resources to another location. I am using maven wagon-ssh plugin to do this. It works fine locally, I am having issues when using Hudson/Jenkins.
This is what we use to populate known_hosts file on jenkins node:
org.codehaus.groovy.maven
gmaven-plugin
check-known-hosts
initialize
execute
import com.jcraft.jsch.*;
import org.apache.maven.wagon.providers.ssh.knownhost.*;
def keyString = "" // host key - the line from known_hosts after key type (ssh-rsa)
FileKnownHostsProvider fkhp = new FileKnownHostsProvider();
JSch sch = new JSch();
sch.setKnownHosts(new ByteArrayInputStream(fkhp.getContents().getBytes()));
def host = project.properties.serverAddress // define someserveraddress.com in
if (host != null) {
HostKeyRepository hkr = sch.getHostKeyRepository();
HostKey[] hk = hkr.getHostKey( host , null );
StringWriter stringWriter = new StringWriter();
String knownHost = host + " " + "ssh-rsa" + " " + keyString;
if ( hk != null )
{
PrintWriter w = new PrintWriter( stringWriter )
def containsKey = false;
for ( HostKey key : hk )
{
def toAdd = key.getHost() + " " + key.getType() + " " + key.getKey();
w.println(toAdd) ;
containsKey = knownHost.equals(toAdd);
}
if (!containsKey) {
println "Adding key for " + host + " to known_hosts"
w.println(knownHost);
fkhp.storeKnownHosts(stringWriter.toString() );
} else {
println "Key for " + host + " is already present in known_hosts"
}
}
}
org.apache.maven.wagon
wagon-ssh-common
2.10
com.jcraft
jsch
0.1.54
Seems to work pretty well.