How to Lint Jenkins Declarative Pipelines Using REST API and VS Code Plugin
This article explains two practical methods—using a custom linter.sh script that calls Jenkins's REST API and employing the Jenkins Pipeline Linter Connector VS Code extension—to validate declarative pipeline syntax before committing, reducing repeated build failures caused by syntax errors.
When developing Jenkins declarative pipelines, syntax errors often surface only after committing code, causing repeated submissions.
To avoid this, pre‑commit validation can be performed by calling Jenkins’s REST API that validates a Jenkinsfile.
One method is to create a linter.sh script (placed in a Shared Libraries repository and ignored by Git) that sends the Jenkinsfile to the /pipeline-model-converter/validate endpoint using curl. Example script and sample output are shown.
# 如何使用
# sh linter.sh your-jenkinsfile-path
# 替换为你的 Jenkins 用户名
username=admin
# 替换为你的 Jenkins 密码
password=admin
# 替换为你的 Jenkins URL
JENKINS_URL=http://localhost:8080/
PWD=`pwd`
JENKINS_FILE=$1
curl --user $username:$password -X POST -F "jenkinsfile=<$PWD/$JENKINS_FILE>" $JENKINS_URL/pipeline-model-converter/validateRunning the script:
$ sh linter.sh Jenkinsfile
Errors encountered validating Jenkinsfile:
WorkflowScript: 161: Expected a stage @ line 161, column 9.
stages {
^After fixing the errors and re‑running, the script reports successful validation:
Jenkinsfile successfully validated.Another method uses the “Jenkins Pipeline Linter Connector” extension for VS Code. After installing the extension, configure the Jenkins URL, username, and password in VS Code settings, then invoke “Validate Jenkinsfile” via the command palette or the shortcut Shift + Alt + V. Screenshots illustrate the configuration and validation results.
Both approaches work only for declarative pipelines; scripted pipelines are not supported. Using either the shell script or the VS Code plugin provides immediate feedback on pipeline syntax before a Jenkins build runs.
DevOps Engineer
DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
