Instead of using "Send build artifacts over SSH" I would like to do the same using a Groovy script (Groovy Postbuild, as it would have access to the Jenkins API).
I would rather have this in a Groovy script, because I need to perform some extra logic for the files. Also because many transfer sets takes a lot of space (scrolling madness).
The Groovy script must get the SSH Server defined in Jenkins configuration, then transfer files over SCP.
Is it possible? Can I do it within Groovy and access to the Jenkins API?
I tried looking into the Jenkins API Javadoc, but could not find any method to gain access to the SSH Server defined in Jenkins Configuration.
Having access to the workspace through Jenkins API I could perhaps do something like this:
import hudson.FilePath.FileCallable
import hudson.remoting.VirtualChannel
def jenkinsInstance = jenkins.model.Jenkins.getInstance()
def project = jenkinsInstance.getItem("myMatrixProject")
def someWorkspace = project.getSomeWorkspace()
//SSH Server information: If this is possible
def sshServer = jenkinsInstance.getSshServer("mySshServer")
def host = sshServer.getHost()
def user = sshServer.getUsername()
def password = sshServer.getPassword()
//SSH Server information
private class ScpFileCallable implements FileCallable<String> {
private static final long serialVersionUID = 1L;
@Override
public String invoke(File file, VirtualChannel channel) throws IOException, InterruptedException {
/*
* Find the files and transfer them over SCP
*/
}
@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
}
}
try {
someWorkspace.act(new ScpFileCallable());
} catch (IOException e) {
println e.getMessage()
} catch (InterruptedException e) {
println e.getMessage()
}