<p>Here is some documentation on the Jenkins pipeline linter and its commands. Do you need to validate <strong>before</strong> a commit? If not, it would be really trivial to run the linting command prior to your pipeline running, and simply fail if it does not pass. </p>

<p>From <a href="https://jenkins.io/doc/book/pipeline/development/#linter" rel="noreferrer">Command-line Pipeline Linter</a>:</p>

<blockquote>
  <p>Jenkins can validate, or "<a href="https://en.wikipedia.org/wiki/Lint_(software)" rel="noreferrer">lint</a>", 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. We recommended using the <a href="https://jenkins.io/doc/book/managing/cli/#ssh" rel="noreferrer">SSH interface</a> to run the
  linter. See the <a href="https://jenkins.io/doc/book/managing/cli/" rel="noreferrer">Jenkins CLI documentation</a> for details on how to
  properly configure Jenkins for secure command-line access.</p>
  
  <p><em>Linting via the CLI with SSH</em></p>

<pre><code># ssh (Jenkins CLI)
# JENKINS_SSHD_PORT=[sshd port on master]
# JENKINS_HOSTNAME=[Jenkins master hostname]
ssh -p $JENKINS_SSHD_PORT $JENKINS_HOSTNAME declarative-linter &lt; Jenkinsfile
</code></pre>
  
  <p><em>Linting via HTTP POST using <code>curl</code></em></p>

<pre><code># curl (REST API)
# Assuming "anonymous read access" has been enabled on your Jenkins instance.
# JENKINS_URL=[root URL of Jenkins master]
# JENKINS_CRUMB is needed if your Jenkins master has CRSF protection enabled as it should
JENKINS_CRUMB=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`
curl -X POST -H $JENKINS_CRUMB -F "jenkinsfile=&lt;Jenkinsfile" $JENKINS_URL/pipeline-model-converter/validate
</code></pre>
  
  <p><strong>Examples</strong></p>
  
  <p>Below are two examples of the Pipeline Linter in action. This first
  example shows the output of the linter when it is passed an invalid
  <code>Jenkinsfile</code>, one that is missing part of the <code>agent</code> declaration.</p>
  
  <p><em>Jenkinsfile</em></p>

<pre><code>pipeline {
  agent
  stages {
    stage ('Initialize') {
      steps {
        echo 'Placeholder.'
      }
    }
  }
}
</code></pre>
  
  <p><em>Linter output for invalid Jenkinsfile</em></p>

<pre><code># pass a Jenkinsfile that does not contain an "agent" section
ssh -p 8675 localhost declarative-linter &lt; ./Jenkinsfile
Errors encountered validating Jenkinsfile:
WorkflowScript: 2: Not a valid section definition: "agent". Some extra configuration is required. @ line 2, column 3.
     agent
     ^

WorkflowScript: 1: Missing required section "agent" @ line 1, column 1.
   pipeline &amp;#125;
   ^
</code></pre>
  
  <p>In this second example, the <code>Jenkinsfile</code> has been updated to include
  the missing <code>any</code> on <code>agent</code>. The linter now reports that the Pipeline
  is valid.</p>
  
  <p><em>Jenkinsfile</em></p>

<pre><code>pipeline {
  agent any
  stages {
    stage ('Initialize') {
      steps {
        echo 'Placeholder.'
      }
    }
  }
}
</code></pre>
  
  <p><em>Linter output for valid Jenkinsfile</em></p>

<pre><code>ssh -p 8675 localhost declarative-linter &lt; ./Jenkinsfile
Jenkinsfile successfully validated.
</code></pre>
</blockquote>

CC BY-SA 3.0
2017-09-27T08:34:23.137Z
Could you explain how to use the client locally? `java -jar jenkins-cli.jar [-s JENKINS_URL] [global options...] command [command options...] [arguments...]`
030
CC BY-SA 3.0
2017-09-27T08:50:08.330Z
https://devops.stackexchange.com/q/2164/210
030
CC BY-SA 3.0
2017-09-27T08:53:05.873Z
Thank you for posting an answer. Is there an online tool as well? It would be great if one could navigate to a UI, post a jenkinsfile and check whether there are syntax issues. I have updated the question.
030
CC BY-SA 3.0
2017-09-28T14:40:41.357Z
@030 I doubt that there is an online linter tool, but it sounds like a fun project to take on :) To use the linter locally, you need to login to your Jenkins server with your credentials, then "ask" Jenkins to lint your jenkinsfile. http://www.pierre-beitz.eu/2017/01/17/Writing-a-Jenkinsfile-Tips-and-Tricks.html
Preston Martin
CC BY-SA 3.0
2017-10-03T10:22:01.710Z
Could you add some snippets from that link to this answer. If the link is deprecated, the information would be gone.
030
CC BY-SA 3.0
2017-10-05T20:22:57.287Z
Good point. Added.
Preston Martin
CC BY-SA 4.0
2021-02-11T12:58:37.943Z
Unfortunately this doesn't work for complex pipelines which use shared library. In particular, it strictly requires your Jenkinsfile to have `pipeline` block which we have encapsulated in some shared library call...
The Godfather
CC BY-SA 3.0
2017-09-26T14:50:55.053Z
2017-10-05T21:04:44.417Z
2017-10-05T21:04:44.417Z
Preston Martin
<p>At the moment I am editing a Jenkinsfile and then let it run unless Jenkins reports a issue. This approach costs a lot of time. I prefer to validate the syntax before committing the Jenkinsfile. Is there a tool that solves this issue?</p>

<p>In <a href="https://docs.gitlab.com/ee/ci/yaml/#validate-the-gitlab-ci-yml" rel="noreferrer">gitlab</a> there is a URI, i.e. <code>/ci/lint</code> that makes it possible to submit a gitlab file, click on the check button and then the UI will indicate whether the syntax is correct or not.</p>

<jenkins-blueocean>
Is there a tool that makes it possible to validate Jenkinsfiles online?
030
# Is there a tool that makes it possible to validate Jenkinsfiles online?

(Asked by: username_0 on 2017-09-26)

At the moment I am editing a Jenkinsfile and then let it run unless Jenkins reports a issue. This approach costs a lot of time. I prefer to validate the syntax before committing the Jenkinsfile. Is there a tool that solves this issue?

In [gitlab](https://docs.gitlab.com/ee/ci/yaml/#validate-the-gitlab-ci-yml) there is a URI, i.e. `/ci/lint` that makes it possible to submit a gitlab file, click on the check button and then the UI will indicate whether the syntax is correct or not.

**Tags:** jenkins-blueocean


---

## 1 answers

### Answer by username_1 on 2017-09-26. Score: 13 | Accepted answer

Here is some documentation on the Jenkins pipeline linter and its commands. Do you need to validate before a commit? If not, it would be really trivial to run the linting command prior to your pipeline running, and simply fail if it does not pass. 
From Command-line Pipeline Linter:

Jenkins can validate, or "lint", 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. We recommended using the SSH interface to run the
  linter. See the Jenkins CLI documentation for details on how to
  properly configure Jenkins for secure command-line access.
Linting via the CLI with SSH
# ssh (Jenkins CLI)
# JENKINS_SSHD_PORT=[sshd port on master]
# JENKINS_HOSTNAME=[Jenkins master hostname]
ssh -p $JENKINS_SSHD_PORT $JENKINS_HOSTNAME declarative-linter < Jenkinsfile

Linting via HTTP POST using curl
# curl (REST API)
# Assuming "anonymous read access" has been enabled on your Jenkins instance.
# JENKINS_URL=[root URL of Jenkins master]
# JENKINS_CRUMB is needed if your Jenkins master has CRSF protection enabled as it should
JENKINS_CRUMB=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`
curl -X POST -H $JENKINS_CRUMB -F "jenkinsfile=
Examples
Below are two examples of the Pipeline Linter in action. This first
  example shows the output of the linter when it is passed an invalid
  Jenkinsfile, one that is missing part of the agent declaration.
Jenkinsfile
pipeline {
  agent
  stages {
    stage ('Initialize') {
      steps {
        echo 'Placeholder.'
      }
    }
  }
}

Linter output for invalid Jenkinsfile
# pass a Jenkinsfile that does not contain an "agent" section
ssh -p 8675 localhost declarative-linter < ./Jenkinsfile
Errors encountered validating Jenkinsfile:
WorkflowScript: 2: Not a valid section definition: "agent". Some extra configuration is required. @ line 2, column 3.
     agent
     ^

WorkflowScript: 1: Missing required section "agent" @ line 1, column 1.
   pipeline }
   ^

In this second example, the Jenkinsfile has been updated to include
  the missing any on agent. The linter now reports that the Pipeline
  is valid.
Jenkinsfile
pipeline {
  agent any
  stages {
    stage ('Initialize') {
      steps {
        echo 'Placeholder.'
      }
    }
  }
}

Linter output for valid Jenkinsfile
ssh -p 8675 localhost declarative-linter < ./Jenkinsfile
Jenkinsfile successfully validated.

> **Comments:**
> **username_0**: Could you explain how to use the client locally? `java -jar jenkins-cli.jar [-s JENKINS_URL] [global options...] command [command options...] [arguments...]`  
> **username_0**: https://devops.stackexchange.com/q/2164/210  
> **username_0**: Thank you for posting an answer. Is there an online tool as well? It would be great if one could navigate to a UI, post a jenkinsfile and check whether there are syntax issues. I have updated the question.  
> **username_1**: @username_0 I doubt that there is an online linter tool, but it sounds like a fun project to take on :) To use the linter locally, you need to login to your Jenkins server with your credentials, then "ask" Jenkins to lint your jenkinsfile. http://www.pierre-beitz.eu/2017/01/17/Writing-a-Jenkinsfile-Tips-and-Tricks.html  
> **username_0**: Could you add some snippets from that link to this answer. If the link is deprecated, the information would be gone.  
> **username_1**: Good point. Added.  
> **username_2**: Unfortunately this doesn't work for complex pipelines which use shared library. In particular, it strictly requires your Jenkinsfile to have `pipeline` block which we have encapsulated in some shared library call...  

---

