Skip to content
Latchkey

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.ReadFileStep

Common 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

  1. Use fileExists and sh 'ls -la' to confirm where the file actually is.
  2. Fix the relative path, or wrap the read in the matching dir(...) block.
  3. When the writer and reader run on different agents, stash the file and unstash it 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 dir block.
  • Stash artifacts that must cross agents.
  • Guard reads with fileExists when a file is optional.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →