Jenkins readFile "NoSuchFileException" in a pipeline in CI
The readFile step resolves paths relative to the current workspace (or dir block). A NoSuchFileException means the file is not there: a wrong relative path, a different agent than the one that wrote it, or a step that never created the file.
What this error means
The build fails with java.nio.file.NoSuchFileException: /home/jenkins/workspace/app/config.json raised from the readFile step.
Jenkins console
java.nio.file.NoSuchFileException: /home/jenkins/workspace/app/build/version.txt
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
at ...workflow.steps.ReadFileStepCommon causes
The path is wrong or relative to the wrong directory
Paths resolve from the workspace root unless inside a dir(...) block; a path assuming a subdirectory will miss.
The file was produced on a different agent
A file written on one node is not present on another. Without stash/unstash or a shared workspace, the read fails.
How to fix it
Verify the path and stash across agents
- Use
fileExistsandsh 'ls -la'to confirm where the file actually is. - Fix the relative path, or wrap the read in the matching
dir(...)block. - When the writer and reader run on different agents,
stashthe file andunstashit before reading.
Jenkinsfile
if (fileExists('build/version.txt')) {
echo readFile('build/version.txt').trim()
}How to prevent it
- Resolve file paths from the workspace root or an explicit
dirblock. - Stash artifacts that must cross agents.
- Guard reads with
fileExistswhen a file is optional.
Related guides
Jenkins "none of the test reports contained any results" (junit) in CIFix Jenkins junit "ERROR: Step ‘Publish JUnit test result report’ failed: none of the test reports contained…
Jenkins dir step "no such file or directory" / wrong CWD in CIFix Jenkins shell failures inside a `dir(...)` block - the directory does not exist in the workspace yet, so…
Jenkins "Cannot run program ... No such file or directory" in CIFix Jenkins "java.io.IOException: error=2, No such file or directory" / "Cannot run program" - the agent trie…