Skip to content
Latchkey

Jenkins "script returned exit code 1" in a sh Step

A shell command in an sh step exited with a non-zero status, so Jenkins marked the step and stage as failed.

What this error means

A stage prints command output, then ends with "ERROR: script returned exit code 1". The real cause is in the command output just above the error line.

jenkins
+ npm run build
npm ERR! Missing script: "build"
ERROR: script returned exit code 1

Diagnose it: agent, workspace, or sandbox?

Declarative pipeline failures usually come from the environment rather than the script: no matching agent, a dirty reused workspace, or the Groovy sandbox rejecting a method.

Jenkinsfile
// print what the agent actually is
sh 'hostname && whoami && pwd && java -version'
sh 'env | sort | head -40'

// workspaces are REUSED between builds by default
cleanWs()

Common causes

The command itself failed

A test, build, or lint command returned non-zero; Jenkins only reports the exit code, not the underlying reason.

A pipe or subcommand failed

With set -e or pipefail, an early failure in a chain aborts the whole sh step.

Missing tool or file

The command, script, or file referenced does not exist on the agent.

How to fix it

Read the output above the exit line

  1. Scroll up from the exit-code line to the actual command error.
  2. Reproduce the command locally to confirm the fix.

Capture status without failing when expected

  1. Use returnStatus to handle expected non-zero exits instead of aborting.
  2. Add diagnostics around the failing command.
Jenkinsfile
def rc = sh(script: 'make test', returnStatus: true)
if (rc != 0) { echo "tests failed with ${rc}" }

How to prevent it

  • Fail fast on the specific failing command and surface its real error in logs rather than treating exit code 1 as the root cause.

Frequently asked questions

What causes Jenkins "script returned exit code 1" in a sh step?
There are 3 common causes: the command itself failed, a pipe or subcommand failed, and missing tool or file. A test, build, or lint command returned non-zero; Jenkins only reports the exit code, not the underlying reason.
How do I fix Jenkins "script returned exit code 1" in a sh step?
There are 2 fixes depending on which cause you have: read the output above the exit line and capture status without failing when expected. Work through them in order, since the first is the most common.
What does Jenkins "script returned exit code 1" in a sh step actually mean?
A stage prints command output, then ends with "ERROR: script returned exit code 1".
How do I stop Jenkins "script returned exit code 1" in a sh step happening again?
Fail fast on the specific failing command and surface its real error in logs rather than treating exit code 1 as the root cause.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card