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, Gitlab CI/CD Pipelines, Buddy.works, etc).
If you're not familiar with Jenkins, 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.
- Scripted Syntax: The traditional way to write pipelines. This option strictly uses Apache Groovy Lang 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:
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
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.
The problem
Every time that I'm working on a Jenkinsfile I make mistakes! 😓. 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 😄.
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 (Ctrl + Shift + X in Windows and Cmd + Shift + X in macOS)
Once the installation process is finished, you need to configure it. Go to the Settings section in VS Code (Ctrl + , in Windows and Cmd + , 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:
https://<JENKINS SERVER URL>/pipeline-model-converter/validate
- Connector User: Enter your Jenkins username here.
And that's it! Now you can lint your Jenkinsfile syntax by checking it against your remote Jenkins Server Linter 😄. To use it, open your Jenkinsfile and open the Command Pallette and write "Validate Jenkins"
If the syntax of your file is valid, you'll have a message similar to this in the Output tab.
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 or if you like to support me you can also buy me a coffee ☕.
Top comments (2)
I followed the same exact steps but I am getting an error .
html>
Error 401 Unauthorized
HTTP ERROR 401 Unauthorized
Powered by Jetty:// 9.4.41.v20210516
Hi Tanuj! Maybe you need to check the
jenkins.pipeline.linter.connector.crumbUrl
. Check this URL for more information: jenkins.io/doc/book/pipeline/devel...