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

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-dev/37c7eb10-bffc-48da-8d8a-8f7602c4d1d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to