Jenkins plugin, how to execute a system command on a remote node?

Our company Jenkins has a master and two subordinate nodes. I am writing a plugin for it. One of the features of the plugin is to check some files from svn. This action cannot be retrieved from the plugin. To do this, I run the console command "svn checkout" from the java code of my plugin. The problem is that files from svn are checked by the master, and not by subordinate nodes. How can I make files to be verified as slaves?

+4
source share
2 answers

First you have these objects, usually taken as parameters for the perform method:

 Launcher launcher; AbstractBuild<?, ?> build; BuildListener listener; 

Then you created and added the arguments to the ListBuilder argument, perhaps something like:

 ArgumentListBuilder command = new ArgumentListBuilder(); command.addTokenized("xcopy /?"); 

Then do something like:

 ProcStarter ps = launcher.new ProcStarter(); ps = ps.cmds(command).stdout(listener); ps = ps.pwd(build.getWorkspace()).envs(build.getEnvironment(listener)); Proc proc = launcher.launch(ps); int retcode = proc.join(); 

ProcStarter will execute the command in the node specified by the launcher instance. But please, at least take a look at the javadocs of all the above classes before using, the above is not a direct copy-paste from the working code.

+7
source

Here is the code based on Hyde's answer, suitable for Groovy script console (in /script )

 def static Run(nodeName, runCommand) { def output = new java.io.ByteArrayOutputStream(); def listener = new hudson.util.StreamTaskListener(output); def node = hudson.model.Hudson.instance.getNode(nodeName); def launcher = node.createLauncher(listener); def command = new hudson.util.ArgumentListBuilder(); command.addTokenized(runCommand); def ps = launcher.launch(); ps = ps.cmds(command).stdout(listener); // ps = ps.pwd(build.getWorkspace()).envs(build.getEnvironment(listener)); def proc = launcher.launch(ps); int retcode = proc.join(); return [retcode, output.toString()] } // for (aSlave in hudson.model.Hudson.instance.slaves) { (recode, output) = Run("build-slave9", "xcopy /?"); println output; 

(Caveats: not tested for programs that read stdin. Pay attention to ByteArrayOutputStream , so do not run programs with very long output. Not indexed for output without ASCII.)

+3
source

Source: https://habr.com/ru/post/1443426/


All Articles