How to validate your Jenkinsfile locally before committing
Tags: vscode, jenkins, devops, jenkinsfile
Published: 2021-03-20 18:58:52

---
title: How to validate your Jenkinsfile locally before committing
published: true
description: 
tags: vscode, jenkins, devops, jenkinsfile
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kbxdz8e2yx6ogn9a5iw.png
---

## Background
Continuous integration and deployment is a fun topic. I've learned a lot recently working on some projects at my actual job.
The task can be a little intimidating sometimes, specially if you work with Jenkins. Don't get me wrong! Jenkins is an amazing tool. I just find the learning curve a little bit steeper compared to other solutions I've tried ([Github Actions](https://github.com/features/actions), [Gitlab CI/CD Pipelines](https://docs.gitlab.com/ee/ci/pipelines/), [Buddy.works](https://buddy.works/), etc).

If you're not familiar with [Jenkins](https://www.jenkins.io/), it's an open-source CI/CD automation suite really popular in enterprise environments.
To execute instructions, Jenkins uses an especial file called *Jenkinsfile*. You can use two flavors:

* **Declarative Syntax**: A relatively new way to write pipeline instructions (first added in version 2.5 of "Pipeline Plugin"). More information [here](https://www.jenkins.io/doc/book/pipeline/syntax/).
* **Scripted Syntax**: The traditional way to write pipelines. This option strictly uses [Apache Groovy Lang](https://groovy-lang.org/) syntax to declare every step of the pipeline.

## The Jenkinsfile
When you work with Jenkins you have two choices to automate your pipelines:

* Working with an inline pipeline
* Working with a Jenkinsfile.

This is a very summarized example of how a Jenkinsfile looks:

```groovy
pipeline {
  agent any

  stages {
    stage("This is stage 1") {
      steps {
        // Cool stuff goes here!
      }
    }

    stage("This is stage 2") {
      steps {
        // Cool stuff goes here!
      }
    }
  }
}
```
> I'm not going to explain the sections of the file here, but you can check this amazing video by TechWorld by Nana: [Complete Jenkins Pipeline Tutorial | Jenkinsfile explained](https://www.youtube.com/watch?v=7KCS70sCoK0)

The recommendation is to work with a standalone Jenkinsfile because the steps to build, test and deploy your code will belong to the repository and can be versioned.

![Build mode selector](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kihyzmzvhgbv60pzxliq.png)

## The problem
Every time that I'm working on a Jenkinsfile I make mistakes! :sweat:. The problem is: you need to commit and push those changes every time, run the pipeline and check if (this time) the pipeline will run. This adds unnecessary git history with syntax fixes (personally, I really hate writing 5 commits messages like `"fix(jenkins): syntax typo"`). How you can avoid it? Well, the solution comes to use an extension of [VS Code](https://code.visualstudio.com/) :smile:.

## Validating Jenkinsfile with Visual Studio Code

You will need:
* A Jenkins Server
* User credentials to Jenkins Server
* Visual Studio Code installed on your PC.
* Jenkins Pipeline Linter Connector for Visual Studio Code.

First, you need to install **Jenkins Pipeline Linter Connector** extension in your **Extension** panel (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>X</kbd> in Windows and <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>X</kbd> in macOS)

![Jenkins Pipeline Linter Connector](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1e383677jjhfm66euoy.png)

Once the installation process is finished, you need to configure it. Go to the **Settings** section in VS Code (<kbd>Ctrl</kbd> + <kbd>,</kbd> in Windows and <kbd>Cmd</kbd> + <kbd>,</kbd> in macOS). In the **"Search settings"** field write **"Jenkins"**.
Complete the fields below:

* **Connector Pass**: Enter your Jenkins user password here.
* **Connector URL**: Enter your Jenkins server URL here as the format below shows:

```bash
https://<JENKINS SERVER URL>/pipeline-model-converter/validate
```

* **Connector User**: Enter your Jenkins username here.

![Configuring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39li7iz1jkj8sxephynb.png)

And that's it! Now you can lint your Jenkinsfile syntax by checking it against your remote Jenkins Server Linter :smile:. To use it, open your Jenkinsfile and open the **Command Pallette** and write "Validate Jenkins"

![Validating](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6skr1ja3k4zn9nuyg8k.png)

If the syntax of your file is valid, you'll have a message similar to this in the **Output** tab.

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysxl3xlop5n2r9xahdlc.png)

## Conclusions
Validating your Jenkinsfiles in your local environment can really improve the time you spent on writing pipelines. You can avoid common mistakes by simply validate the syntax regularly using the Jenkins Pipeline Linter Connector Extension for VS Code.

## Closing
Thank you for reading my post. You can follow me on [Twitter](https://twitter.com/nicoavila_a) or if you like to support me you can also [buy me a coffee](https://www.buymeacoffee.com/nicoavila) :coffee:.

# References
* [Jenkins](https://www.jenkins.io)
* [Apache Groovy Language](https://groovy-lang.org/documentation.html)
* [Jenkinsfile Syntax](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/)
* [Jenkins Pipeline Linter Connector Extension in VS Marketplace](https://marketplace.visualstudio.com/items?itemName=janjoerke.jenkins-pipeline-linter-connector)