Can Hudson slaves run plugins?

大兔子大兔子 提交于 2019-11-30 03:57:22
Christopher Orr

Firstly, go Jenkins! ;)

Secondly, you are correct — the code is being executed on the master. This is the default behaviour of a Hudson/Jenkins plugin.

When you want to run code on a remote node, you need to get a reference to that node's VirtualChannel, e.g. via the Launcher that's probably passed into your plugin's main method.

The code to be run on the remote node should be encapsulated in a Callable — this is the part that needs to be serialisable, as Jenkins will automagically serialise it, pass it to the node via its channel, execute it and return the result.

This also hides the distinction between master and slave — even if the build is actually running on the master, the "callable" code will transparently run on the correct machine.

For example:

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
                       BuildListener listener) {
    // This method is being run on the master...

    // Define what should be run on the slave for this build
    Callable<String, IOException> task = new Callable<String, IOException>() {
        public String call() throws IOException {
            // This code will run on the build slave
            return InetAddress.getLocalHost().getHostName();
        }
    };

    // Get a "channel" to the build machine and run the task there
    String hostname = launcher.getChannel().call(task);

    // Much success...
}

See also FileCallable, and check out the source code of other Jenkins plugins with similar functionality.

I would recommend making your plugin work properly rather than using the network share solution.. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!