I am trying to use methods from java.nio.file.* to perform some basic file operations in a Jenkins pipeline. Regardless of the node block in which the code exists, the code executes on the master node. In the pipeline, I have verified that the various node blocks are correct--they uniquely identify specific nodes. However, pathExists (and other java.nio.file and java.nio.filesystem methods that move, copy or delete files) always executes on the master node. Any ideas what's happening or how to fix it?
import java.nio.file.* String slavePath = 'C:\\Something\\only\\on\\slave\\node' String masterPath = 'D:\\Something\\only\\on\\master\\node' def pathExists (String pathName) { def myPath = new File(pathName) return (myPath.exists()) } stage('One') { node ('slave') { bat returnStatus: true, script: 'set' println (pathExists(slavePath)) // Should be true but is false. println (pathExists(masterPath)) // Should be false but is true. } node ('master') { bat returnStatus: true, script: 'set' println (pathExists(slavePath)) // false println (pathExists(masterPath)) // true } }
Read the documentation. You may not use any file operations from Pipeline script.