A Pipeline is a definition of a job that has some stages executed respectively. Each stage has a steps section that includes commands which should be executed respectively to complete that stage. With the pipeline, we define how Jenkins should complete the CI/CD process for our applications. So to speak, the pipeline runs all processes formerly run manually by human hands.
Scripted vs. Declarative Pipelines:
Jenkins supports two types of pipeline definitions. 1- Scripted pipelines which are written in Groovy language. 2- Declarative pipelines, which are written in Jenkins DSL language. We intend to talk about the Jenkins DSL language, which is more readable and easy to learn. Furthermore, within this DSL language, we can write Groovy scripts too.
Jenkins Pipeline Syntax Introduction:
Here is a basic pipeline example. Let’s describe it.
Each pipeline starts with a pipeline, and each of them needs two mandatory sections agent and stages, which define where to run pipeline stages and what should be run to complete the pipeline. Each stage should be defined with a unique name, and the steps sections may contain one or more commands within that stage.
So, let’s describe pipeline directives more deeply…
Pipeline directive: agent
This directive specifies where to run the entire pipeline or a specific stage.
agent any execute the pipeline or a particular stage on any available agent.
agent none is used to disable the global agent for the whole pipeline. Instead, we should define the agent for every stage. Here is an example:
agent label is used to execute the entire pipeline or a specific stage on a node with a particular label. Here is an example:
agent docker is used to execute the entire pipeline or a specific stage inside a docker container. Here is an example:
All stages of the above pipeline are executed within an instance of an alpine container.
agent dockerfile this directive is a special Docker-related directive that is used to build a docker image from an existing Dockerfile within the source of the project and run the entire pipeline or a specific stage inside an instance of this created image. To use this special directive, you should connect Jenkins to your source code repository to be able Jenkins to fetch the project source in addition to Dockerfile. Here is an example:
Many other agents may exist, but at this moment, these described agents should be enough. Another agent like Kubernetes will describe later.
Pipeline directive: stages
stages section contains one or more stage directives. Each stage should be defined with a unique name. Inside each stage, we should define steps a section that contains one or more steps to be executed.
Pipeline directive: steps
The steps section contains one or more steps that should be executed in this stage. All commands needed to automate the CI/CD process are defined in this section.
You can find all tutorial materials in the following GitHub repository: