Trying to execute a sh step on a second node() is failing.
Minimal example:
node('windows') {
env.PATH = "C:\\some\\path;${env.PATH}"
// ...
}
node('linux') {
sh "echo 'Hello World!'" // this fails
}
The error message is:
nohup: failed to run command 'sh': No such file or directory
The PATH environment variable is set correctly to contain the sh command on both nodes.
Why does the sh step on the second node fail?
tl;dr: The problem was setting env.PATH with an assignment. Use withEnv() instead:
node('windows') {
withEnv('PATH+some=C:\\some\\path') {
// ...
}
}
node('linux') {
sh "echo 'Hello World!'"
}
In one of the later sections of the pipeline tutorial, it is said that
environment variable overrides are limited to being global to a pipeline run
This means that when setting an environment variable like this env.PATH = ... this variable is fixed for the rest of the pipeline script and will therefore overwrite the actual value of that environment variable on all following nodes. So it reverses the normal behavior where the env properties are set to the actual value of the environment variables of the node.
So our Linux node ended up with the PATH variable of the Windows node.
That's why sh wasn't found.
Note for the curious: nohup was still found because it is executed directly by the Jenkins client which uses the "real" environment variables of the node. But Jenkins then sets the modified environment for the nohup process which is why nohup could not find sh in the PATH.
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