Skip to content
Latchkey

Jenkins "WorkflowScript: unexpected token" - Fix Jenkinsfile Syntax

Jenkins could not even parse your Jenkinsfile. The Groovy compiler hit a token it did not expect - usually an unbalanced brace, a bad quote, or a directive in the wrong place - and the pipeline failed before any stage ran.

What this error means

The build fails immediately at "Compiling pipeline" / "Loading pipeline" with a WorkflowScript compilation error pointing at a line and column. No stage executes because the script never compiled. The same Jenkinsfile fails identically every time.

Jenkins console
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 18: unexpected token: } @ line 18, column 1.
   }
   ^

1 error

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

Unbalanced braces or parentheses

A missing or extra }/) from an edited stage, steps, or script block shifts the parser, and it reports the error at the line where the mismatch becomes unrecoverable - often a few lines after the real mistake.

Broken string quoting

An unterminated "..." or a ${...} interpolation inside single quotes (which Groovy does not interpolate) confuses the lexer and produces an unexpected-token error.

A declarative directive in the wrong place

In a declarative pipeline, putting a step outside steps {}, or environment/when in an invalid position, breaks the grammar the declarative parser expects.

How to fix it

Validate the Jenkinsfile against the controller

The linter compiles the file with the same parser the build uses, so it reports the exact line without burning a build.

Terminal
# CLI linter (needs a crumb on CSRF-protected controllers)
curl -X POST -F "jenkinsfile=<Jenkinsfile" https://jenkins.example.com/pipeline-model-converter/validate

Balance braces and fix quoting

  1. Open the reported line, then scan upward for the unclosed {, (, or string.
  2. Use double quotes when you need ${...} interpolation; single quotes are literal in Groovy.
  3. Re-indent the file so block boundaries are visible - most brace errors are obvious once indentation is correct.

Keep declarative structure strict

Declarative pipelines only allow specific directives at each level. Put executable steps inside steps {}, and wrap arbitrary Groovy in a script {} block.

Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'make build'
        script { def v = readFile('VERSION').trim() }
      }
    }
  }
}

How to prevent it

  • Lint the Jenkinsfile in a pre-merge check with the pipeline-model-converter validator.
  • Use the Jenkins VS Code/IDE Groovy support or "Replay" to iterate on syntax.
  • Keep the Jenkinsfile small; move logic into a shared library that can be unit-tested.

Frequently asked questions

Why does it point at a line that looks fine?
A brace or quote error earlier in the file shifts the parser, so the first token it truly cannot accept may be lines later. Start at the reported line and scan upward for the unbalanced construct.

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