“Pipeline-as-code” is the principle that allows Jenkins to treat pipelines like regular files. There are two ways to describe pipelines: scripted and declarative. In this article, we will talk about Jenkins Scripted Pipeline: we will analyze its structure and analyze the use cases.
What is Scripted Pipeline in Jenkins
Jenkins Scripted Pipeline is the first version of the “Pipeline-as-code” principle. It is a Groovy script using the Jenkins Pipeline DSL and provides an outstanding level of power and flexibility.
Scripted pipelines have fewer structural restrictions. They have only two basic blocks: “node” and “stage”. The node specifies the machine on which the specific pipeline is running. A stage is used to group atomic steps that represent a single operation.
Steps for a scripted pipeline can be generated using the Snippet Generator . Since this type of pipeline does not contain directives, all logic is displayed in steps. For simple pipelines, this reduces the overall amount of code.
How to Create a Jenkins Scripted Pipeline
To get started, log in and select “New Item” from the panel on the left:
Enter a name for the pipeline and select “Pipeline” from the options provided. Click “OK” to proceed to the next step:
Now you can start working with the pipeline script:
The red box is where you can start writing the script.
Jenkins Pipeline Script
Pipelines contain specific clauses or elements for defining sections of a script that follow the Groovy syntax.
Node Blocks
The first section to define is “node”:
node {
}
“Node” is part of the Jenkins distributed mode architecture, where the workload can be delegated to multiple agent nodes. The master node handles all tasks in the environment. Jenkins agent nodes unload builds from the master node, thereby doing all the work in the pipeline. This is covered in detail in Jenkins Distributed builds .
Stage Blocks
The next section is “stage”:
stage {
}
A pipeline consists of several steps that can be grouped into build steps. Among the stages are:
- extracting code from the repository;
- project creation;
- application deployment;
- performing functional tests;
- running performance tests.
Each of them can include more than one action. For example, the deployment phase of an application might consist of copying files to a specific environment for functional tests and to a dedicated server for performance tests. Once the files have been successfully copied, you will proceed with the deployment.
Each build step defines the tasks it needs to complete. For example, a complete script pipeline might look like this:
node {
stage ('Build') {
bat "msbuild ${C:\\Jenkins\\my_project\\workspace\\test\\my_project.sln}"
}
stage('Selenium tests') {
dir(automation_path) { //changes the path to “automation_path”
bat "mvn clean test -Dsuite=SMOKE_TEST -Denvironment=QA"
}
}
}
Such a script has the following steps:
- Build stage : bat “msbuild…”. You create a project by specifying a Visual Studio solution file.
- Selenium tests stage : dir(automation_path). Change the current directory to the value set in the Automation_path variable.
- Selenium tests stage : bat “mvn clean test …”. Call maven to run the tests specified in “SMOKE_TEST” using the values defined in “QA”. Also the “clean” flag cleans the build.
Jenkins provides an interface that generates pipeline suggestions for actions that can be added to any of the build steps. Click “Pipeline Syntax” to open the following page:
Let’s say you want to create script commands to execute a Windows batch file:
By clicking on “Generate Pipeline Script”, you will create the desired sentence, which can be immediately added to the script.
The Jenkinsfile
Jenkinsfile is a simple text file with Groovy code that is used to configure the pipeline. You can edit it through the Jenkins web interface or with a text editor.
You can set up Jenkins to automatically poll the repository and start new builds when updates are found. This option is available on your project’s configuration screen under “Build triggers”:
Enabling “Poll SCM” allows you to enter a cron-like expression in the Schedule text box. However, configuring Jenkins to automatically poll the repository is not a clean and efficient way to get updates – it’s better to use Git Hooks instead.
Jenkins Scripted Pipeline Security
The “Use Groovy Sandbox” option, available on the Jenkins Scripted Pipeline tab, allows scripts to be run by any user without administrator privileges. In this case, the script is only run using the internal available APIs.
If the checkbox is cleared and the script has operations that require approval, the administrator must grant them. This method is known as “Script approval”. By default, all Jenkins pipelines run in the Groovy sandbox. When this option is set and unauthorized operations occur, the script fails on startup.
Jenkins Scripted Pipeline vs. Declarative Pipeline
Declarative pipelines are a more recent approach to the “Pipeline-as-code” principle. It is designed to make code easier to develop and maintain by providing a more meaningful syntax.
What the structure of a declarative pipeline looks like:
- pipeline – definition of a declarative pipeline;
- agent – the agent on which the assembly will be carried out;
- stages – build stage;
- steps – atomic steps to build.
If you describe it in code:
pipeline {
agent any
stages {
stage('Build') {
steps {
//…
}
}
stage ('Test') {
steps {
//…
}
}
}
}
One of the advantages of a declarative pipeline is that it allows you to define actions after each stage or after the entire build. It is recommended to use it when describing pipelines at the top level.
Another important feature is directives . They allow you to add tools to the pipeline, and also help with setting up triggers, environment variables, and parameters.
Much of the power of declarative pipelines comes from directives. For example, with the script directive , declarative pipelines can take advantage of scripted pipelines. This directive is executed as a line within a script pipeline.
Briefly about the main
Despite the differences in syntax, scripted and declarative pipelines serve the same purpose. In fact, these are two different tools for solving the same problem, so they can be considered interchangeable.
The short syntax of declarative pipelines provides a faster and smoother entry into CI/CD. However, scripted pipelines offer more options for advanced users. To get the best of both worlds, you can use declarative pipelines with script directives.