If Jenkins handed you the charming error Expected a Step on Line you are not cursed. You just violated the strict rules of declarative pipeline grammar and the parser is doing its job with surgical rudeness. This guide will help you find the missing steps block fix syntax mistakes and get your CI CD pipeline back to behaving like an obedient robot.
There are two flavors of Jenkins pipelines. Declarative pipeline wants a top level pipeline block explicit stages and steps blocks. Scripted pipeline is raw Groovy and plays by different rules. Mixing the two or leaving out required blocks makes the parser complain with messages like Expected a Step on Line which sounds dramatic but usually points to a missing steps wrapper or stray code placed in the wrong scope.
Common triggers
pipeline {
agent any
stages {
stage('Build') {
// this will cause the parser to moan
sh 'mvn -B package'
}
}
}
The problem here is that in a declarative pipeline stage must contain a steps block. Placing sh directly inside a stage looks valid to a human but not to the parser.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn -B package'
}
}
}
}
Wrap the command in steps and the error usually disappears. If you are using Windows use bat 'dir' or bat 'mvn -B package' instead of sh.
Use the built in pipeline syntax tool in Jenkins or a linter for Jenkinsfile to catch mismatched braces and misplaced blocks. The simplest trick when stuck is to paste the smallest failing Jenkinsfile into the syntax validator and remove parts until the error goes away. That narrows the culprit fast.
Remember steps must be named functions like sh bat echo and so on. Do not paste raw shell scripts at top level. If you need complex Groovy logic use a script block or switch to scripted pipeline style but be explicit about which style you are using.
Old pipeline related plugins can produce confusing errors that look like syntax problems. Update pipeline plugin workflow plugin and any linters then rerun the job. If the error persists run the job with a minimal Jenkinsfile and expand from there.
Final tip when you feel like screaming paste the failing Jenkinsfile into the validator and delete lines until the error goes away. You will find the offending line faster than arguing with the error message. Now go fix that Jenkinsfile and pretend you meant to teach your team a lesson about strict grammar.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.