Transfers artifacts using SCP with Groovy and Jenkins API

3,156 views
Skip to first unread message

Sverre Moe

unread,
Apr 15, 2015, 9:58:21 AM4/15/15
to jenkin...@googlegroups.com
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()
}


Sverre Moe

unread,
Apr 15, 2015, 10:17:30 AM4/15/15
to jenkin...@googlegroups.com
I may have had a small realisation that the SSH credentials aren't available through the Jenkins API, but the Publish over SSH plugin. However the question remains, how to get these credentials so I could transfer the artifacts manually.

Sverre Moe

unread,
May 27, 2016, 6:39:29 AM5/27/16
to Jenkins Developers
I found the plugin SSH Agent where I could access SSH credentials. However I'm still not sure how to use it in Groovy Postbuild to transfer files to the server.

I could find a Pipeline example using the SSH Agent Plugin:
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
    sh("git tag -a some_tag -m 'Jenkins'")
    sh('git push https://${GIT_USERNAME}:${GIT_PASSWORD}@<REPO> --tags')
}


My current implementation is using "Send build artifacts over SSH" in Flexible Publish.  It is big, complex and hard to maintain. I haven't given up to do the same in Groovy Postbuild script. As a fallback I was thinking of just manually copy ssh public key to server and use Bash script to scp the files. No one else have used Groovy postbuild to transfer files to server or at least know how?




onsdag 15. april 2015 15.58.21 UTC+2 skrev Sverre Moe følgende:
Reply all
Reply to author
Forward
0 new messages