3
votes

I have a pipeline flow defined as:

node("linux_label") {
    println("hostname".execute().txt)
    def filename = "${WORKSPACE}/submoduleinfo.txt"
    stage("Submodule info") {
        def submoduleString = sh script: "git -C ${WORKSPACE} submodule status > ${filename}", returnStdout: true
    }
    String fileContents = new File("$filename}").text
    operateOnFile(fileContents)
}

At "new File" I will get an error saying no such file exists. after some troublehshooting I see that the hostname printout will output the jenkins master and not the node "linux_label" where the workspace resides.

Is this how Piepeline should work, i.e. all code that is not part of stage/steps/etc are executed on the jenkins master and not on the wanted node?

What would be a good workaround where I do an operation in one stage and want to operate on the file in the node {} domain?

2

2 Answers

2
votes

That is how pipeline works. You can use readFile to read file from a workspace. Since you are using just a content of the file for your processing, this will work.

From tutorial:

readFile step loads a text file from the workspace and returns its content (do not try to use java.io.File methods — these will refer to files on the master where Jenkins is running, not in the current workspace).

In one of our use case, we added some additional functions using Shared pipeline library.

1
votes

Try this:

if (env['NODE_NAME'].equals("master")) {
    return new hudson.FilePath(path);
} else {
    return new hudson.FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path);
}