Friday 16 October 2020

Validate a Jenkinsfile before running the Pipeline

 


Jenkins can validate a Declarative Pipeline from the command line before actually running it. This can be done using a Jenkins CLI command or by making an HTTP POST request with appropriate parameters.

Example

Suppose you have a simple Jenkinsfile-test like this:

pipeline {
    agent any
    stages {
        stage('List files') {
            steps {
            sh 'ls -al'
            }
        }
    }
}

And you want to check if the sintax is correct before using it.

Validation

All you have to do is to execute this command:
curl -s -X POST -F "jenkinsfile=<Jenkinsfile-test" \
https://user:password@jenkins.example.com/pipeline-model-converter/validate
Note that user and password are the credentials you usually use to authenticate in Jenkins web console.

Output

✅If it's all ok, you should get:
Jenkinsfile successfully validated.

❌If there are some errors, then you should get an error message.
For example, let's remove the steps section in the stage.

Trying to validate again Jenkinsfile-test, we get:

Errors encountered validating Jenkinsfile:
WorkflowScript: 13: Unknown stage section "sh". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 13, column 9.
           stage('List files') {
           ^

WorkflowScript: 13: Expected one of "steps", "stages", or "parallel" for stage "List files" @ line 13, column 9.
           stage('List files') {
           ^


4 comments: