Jenkins Pipeline (or simply "Pipeline" with a capital "P") is a suite of plugins
which supports implementing and integrating continuous delivery pipelines into
Jenkins.
A continuous delivery pipeline is an automated expression of your process for
getting software from version control right through to your users and customers.
Every change to your software (committed in source control) goes through a
complex process on its way to being released. This process involves building the
software in a reliable and repeatable manner, as well as the progression of the
built software (called a "build") through multiple stages of testing and
deployment.
Typically, the definition of a Jenkins Pipeline is written into a text file
(called a Jenkinsfile) which in turn is checked into a
project’s source control repository.
This is the foundation of "Pipeline-as-Code"; treating the continuous delivery
pipeline a part of the application to be versioned and reviewed like any other code.
Creating a Jenkinsfile provides a number of immediate benefits:
-
Automatically create Pipelines for all Branches and Pull Requests
-
Code review/iteration on the Pipeline
-
Audit trail for the Pipeline
-
Single source of truth
for the Pipeline, which can be viewed and edited by multiple members of the project.
While the syntax for defining a Pipeline, either in the web UI or with a
Jenkinsfile, is the same, it’s generally considered best practice to define
the Pipeline in a Jenkinsfile and check that in to source control.
Here’s an example of a Jenkinsfile:
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any (1)
stages {
stage('Build') { (2)
steps { (3)
sh 'make' (4)
}
}
stage('Test'){
steps {
sh 'make check'
junit 'reports/**/*.xml' (5)
}
}
stage('Deploy') {
steps {
sh 'make publish'
}
}
}
}
Jenkinsfile (Scripted Pipeline)
node {
stage('Build') {
sh 'make'
}
stage('Test') {
sh 'make check'
junit 'reports/**/*.xml'
}
stage('Deploy') {
sh 'make publish'
}
}
| 1 |
agent indicates that Jenkins should allocate an executor and workspace for
this part of the Pipeline. |
| 2 |
stage describes a stage of this Pipeline. |
| 3 |
steps describes the steps to be run in this stage |
| 4 |
sh executes the given shell command |
| 5 |
junit is a Pipeline step provided by the
JUnit plugin
for aggregating test reports. |