I have a piece of code to list all files in the /tmp
directory on two nodes, like so (scripted syntax):
stage('Demo') {
node('node1') {
println new File('/tmp/').listFiles().toList()
}
node('node2') {
println new File('/tmp/').listFiles().toList()
}
}
However, the results from both nodes are identical. It looks like the code is executed on the master, only the println function is executed on the 2 nodes.
The question is: is it true? and if so, how do I know if the code is executed on master or slaves?
All Jenkins steps that use the node
context will execute on those agents (from the node
blocks). For example, a sh 'ls -1 /tmp/'
would run the ls -1 /tmp/ command
on that agent that is in the block. But, the actual Groovy sh
method and JVM code execute on the Jenkins master.
All of the Groovy inside of a Jenkins pipeline is executed on the master in a special source-transformed fashion from the Pipeline Groovy Plugin. Hence, why new File('/tmp')
executes on the master rather than the agent. If you were running your pipeline using the security sandbox, you would get a security exception because new File
by default is disallowed. In fact, any of the normal JVM style methods are disallowed. For example, wouldn't it be terrible if a pipeline could call System.exit(0)
and shut down Jenkins for all of the users?
Similar questions:
Pipeline DSL context runs on master node even that your write node('someAgentName')
in your pipeline. new File
will work only on master.
But you can read data from file via sh(). Something like:
def list = sh(returnStdout: true, script: 'ls').trim()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With