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

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.

Related guides

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