Petros Makris
Petros Makris

Reputation: 522

How to execute groovy/java code in the context of a jenkins-pipeline slave node block?

In this snippet:

stage('build') {
    node ('myslave') {
        git(url: 'git@hostname:project.git')
        println(InetAddress.getLocalHost().getHostName())
    }
}

The git step is executed correctly and checks out code into node's workspace. But why do I get Masters' hostname when executing the second command?

For example, this is not working also in the context of a node() {}

new File("${WORKSPACE}).listFiles()

Which does not actually iterate the ${WORKSPACE} folder

Upvotes: 1

Views: 2356

Answers (1)

Jon S
Jon S

Reputation: 16346

All Groovy code in an Pipeline script is executed on the master. I'm been unable to find any way to execute a generic groovy code on the slave, not due to lack of functionality in the Jenkins core, but problems with Pipeline groovy and serialisation of objects. Found this related question which addresses remoting in groovy.

It is however possible to do file operations on the slave side, see this answer for example how you can access files on the slave.

Upvotes: 1

Related Questions