The problem with the Jenkins pipeline and java.nio.file. * Methods

I am trying to use methods from java.nio.file. * to perform some basic file operations in the Jenkins pipeline. Regardless of the node block in which the code exists, the code runs on the main node. In the pipeline, I checked that the various node blocks are correct - they uniquely identify certain nodes. However, pathExists (and other code that moves, copies, or deletes files) are always executed on the main node. Any ideas what is 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 } } 
+6
source share
1 answer

This is a script pipeline specification. It is written in a tutorial .

  • readFile step loads the text file from the workspace and returns it (do not try to use the java.io.File methods - this will mean the files on the host computer where Jenkins is working, and not in the current workspace).

  • There is also a writeFile step to save contents to a text file in the Workspace

  • fileExists to check if a file exists without downloading it.

You can use these Jenkins steps in node instead of java.io.File or java.nio.file.Files , as shown below.

 String slavePath = 'C:\\Something\\only\\on\\slave\\node' String masterPath = 'D:\\Something\\only\\on\\master\\node' stage('One') { node ('slave') { bat returnStatus: true, script: 'set' println fileExists(slavePath) // Should be true println fileExists(masterPath) // Should be false } node ('master') { bat returnStatus: true, script: 'set' println fileExists(slavePath) // false println fileExists(masterPath) // true } } 
+4
source

All Articles