# Jenkins Pipeline Syntax Error: Complete Debugging Guide
You're writing a Jenkinsfile, you commit it, and Jenkins immediately rejects it with a syntax error. The message points to line 42, but you've stared at that line for ten minutes and it looks perfectly fine.
Jenkins pipelines use Groovy syntax, which has some quirks that can trip you up. Let me walk through the most common syntax errors and how to fix them.
The Dreaded "WorkflowScript" Error
When you see something like this:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 15: expecting '}', found 'stage' @ line 15, column 5.The error points to a location that might not be the actual problem. Groovy's parser often reports errors at the line after the actual mistake.
Rule of thumb: Look at the line before the reported line number.
Missing or Extra Braces
The most common syntax error in declarative pipelines is mismatched braces. Each stage block needs its own braces, and forgetting to close one can cascade errors throughout the file.
// WRONG - missing closing brace
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn package'
// Missing } here!
}
stage('Test') { // Error reported here
steps {
sh 'mvn test'
}
}
}
}Fix: Count your braces. Every opening { needs a closing }. Use an editor with bracket matching.
A properly formatted pipeline:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
}
}Incorrect Stage Block Structure
Declarative pipelines have strict structure requirements. You can't put steps inside post, or environment inside steps.
// WRONG - environment inside steps
stage('Deploy') {
steps {
environment {
API_KEY = credentials('api-key')
}
sh 'deploy.sh'
}
}Fix: Move environment to the correct location:
// CORRECT - environment at stage level
stage('Deploy') {
environment {
API_KEY = credentials('api-key')
}
steps {
sh 'deploy.sh'
}
}String Interpolation Problems
Groovy string interpolation can cause unexpected errors, especially with shell commands:
// WRONG - single quotes don't interpolate
environment {
VERSION = '1.0.0'
}
steps {
sh 'echo "Building version ${VERSION}"' // Prints literal ${VERSION}
}// CORRECT - use double quotes for interpolation
steps {
sh "echo 'Building version ${VERSION}'"
}But be careful with dollar signs in shell commands:
```groovy // WRONG - Groovy tries to interpolate $PWD sh "echo Current directory: $PWD"
// CORRECT - escape the dollar sign or use single quotes sh 'echo Current directory: $PWD' sh "echo Current directory: \$PWD" ```
Invalid Agent Configuration
The agent directive must be specified at the top level:
// WRONG - missing agent
pipeline {
stages {
stage('Build') {
steps {
sh 'build.sh'
}
}
}
}
// Error: Missing required section 'agent'Fix: Add an agent declaration:
pipeline {
agent any // or: agent none, agent { label 'linux' }, etc.
stages {
stage('Build') {
steps {
sh 'build.sh'
}
}
}
}Script Block Syntax Errors
When using script blocks for imperative logic:
// WRONG - assignment inside script block without def
stage('Process') {
steps {
script {
result = sh(script: 'cat version.txt', returnStdout: true).trim()
}
}
}This works but might cause issues with Groovy sandboxing. Better approach:
// CORRECT - explicit variable declaration
stage('Process') {
steps {
script {
def result = sh(script: 'cat version.txt', returnStdout: true).trim()
echo "Result: ${result}"
}
}
}When Directive Syntax
Conditional stage execution uses the when directive:
// WRONG - when inside steps
stage('Deploy') {
steps {
when {
branch 'main'
}
sh 'deploy.sh'
}
}Fix: when goes before steps:
// CORRECT
stage('Deploy') {
when {
branch 'main'
}
steps {
sh 'deploy.sh'
}
}Parallel Stage Syntax
Parallel stages have specific syntax requirements:
// WRONG - incorrect parallel structure
stage('Test') {
parallel {
stage('Unit Tests') {
steps {
sh 'mvn test'
}
}
stage('Integration Tests') {
steps {
sh 'mvn verify -Pintegration'
}
}
}
}Fix: Parallel stages must be inside a stage with failFast option or directly at stages level:
// CORRECT - parallel at stages level
stages {
stage('Test') {
parallel {
stage('Unit Tests') {
steps {
sh 'mvn test'
}
}
stage('Integration Tests') {
steps {
sh 'mvn verify -Pintegration'
}
}
}
}
}Using the Jenkins Linter
Jenkins has a built-in pipeline linter. Use it before committing:
```bash # Via Jenkins CLI java -jar jenkins-cli.jar -s http://localhost:8080 declarative-linter < Jenkinsfile
# Via curl with Jenkins API token curl -X POST -H "Authorization: Bearer YOUR_API_TOKEN" \ -F "jenkinsfile=<Jenkinsfile" \ http://localhost:8080/pipeline-model-converter/validate ```
Or use the "Replay" feature in Jenkins UI to test syntax changes without committing.
Common Syntax Error Checklist
When you hit a syntax error:
- 1.[ ] Check the line *before* the reported error
- 2.[ ] Verify all braces
{}are matched and properly nested - 3.[ ] Ensure
agentis declared at the pipeline level - 4.[ ] Confirm directives are in correct order (when, environment, steps)
- 5.[ ] Check string interpolation—use double quotes for variables
- 6.[ ] Validate
whenconditions are outsidesteps - 7.[ ] Run the declarative linter before committing
- 8.[ ] Use proper indentation (2 or 4 spaces, be consistent)
Recovery from Broken Pipelines
If your pipeline is so broken Jenkins won't even load it:
- 1.Go to the job configuration page
- 2.Scroll to "Pipeline script" instead of "Pipeline script from SCM"
- 3.Paste your Jenkinsfile content
- 4.Click "Pipeline Syntax" at the bottom for reference
- 5.Fix syntax errors, then copy back to your repository
This interactive approach is faster than commit-push-wait-fix cycles.