Executing a Maven job using Jenkins declarative pipeline
In this article we will discuss Jenkins declarative pipeline, and see how we can build our own basic maven execution.
Breaking down the pipes:
I always use the following drawing to explain the general concept of a pipeline.
Close your eyes and imagine a pipe construction that represents your companies release/deployment flow.
There is an entrance and exit point, and the entire pipe construction consists of different parts/stages that make the pipe hold and contain the flow. If one of the stages will break, the flow will stop and leak out of that specific part, until it is fixed.
Now, open your eyes. That my friends, is a standard delivery pipeline.
In Jenkins, we have the possibility to build that pipe construction using something that is called the Jenkins Pipeline. At first, it was a script that described the different stages of the build, and later on it has evolved and grew in popularity and feature count.
The Declarative pipeline syntax is a relatively new addition in Jenkins. It was introduces in the Jenkins 2.0 release and added some functionality and features vs the traditional scripted syntax.
What is a declarative pipeline syntax?
Older Jenkins versions were supporting only the scripted pipeline syntax, which was based on the Groovy DSL, The declarative pipeline syntax introduced a more readable, user friendly syntax and structure, that features specific statements for defining the pipeline flow.
Each valid declarative pipeline starts with the "Pipeline" block, and includes the following sections.
As we know, Jenkins supports distributed builds that can be executed on different nodes. This sections allows us to delegate a build to a specific agent node. (We can also add a "Parallel" section that can execute our steps concurrently). Traditionally, nodes have clear naming conventions that can distinguish them one from another.
In the above example, we are basically telling Jenkins to execute the task on any of the available nodes.
This sections, allows you to declare and generate different stages to our pipeline. The "Stages" section must include at least one "Stage"
Stage is the actual part that is going to be executed by Jenkins pipeline. In the above example, we are seeing a specific stage that we called "Clone Master Branch". The name is a free text string that can later on be displayed and visualized by Jenkins.
The stages section must include at least one step. The step in the below example will clone a git repository.
In Test Automation...
One of the bigger added values test automation provides us, is the ability to go fast with bigger confidence. One of the key ingredients in producing that confidence is a good CI/CD process that incorporates test execution on different levels.
Now, let's see how we can execute our Maven test automation project using the Jenkins declarative pipeline syntax.
For this demonstration I have set up a fresh new Jenkins server instance and installed all of the necessary plugins.
Creating the job:
- Key assumption - Configuring Maven at the global tool configurations.
- I'm starting by creating a new Item(Job). I will name it Maven-Job and select the "Pipeline" option:
- Next, I need to write my Jenkinsfile. I can use an external file or just use the built in Pipeline-Script section.
- Now let's save our job and build it.
As we can see in the above screenshot, our job finished successfully. If you notice, our test steps were visualized in the Stage View according to what we defined in our script.
I hope you enjoyed this post and learned something new. See you at the next one. :)
Post a Comment