Table of contents
Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. This process involves building the software in a reliable and repeatable manner, as well as progressing the built software (called a "build") through multiple stages of testing and deployment.
The continuous delivery pipeline starts when a developer makes any changes and commits it to the source code, the pipeline then builds the artifact, tests the application, and releases it to deliver to the customer.
In Jenkins, we can write the pipeline as a code either in the UI or in a text file called Jenkinsfile which can be committed to the repository with the source code.
There are two ways of defining a pipeline in Jenkins -
Scripted pipeline
Declarative pipeline
In Declarative Pipeline syntax, the pipeline block defines all the work done throughout your entire Pipeline. agent is Declarative Pipeline-specific syntax that instructs Jenkins to allocate an executor (on a node) and workspace for the entire Pipeline.
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Build') {
steps {
//
}
}
stage('Test') {
steps {
//
}
}
stage('Deploy') {
steps {
//
}
}
}
}
In a Scripted pipeline, one or more 'node' blocks do the core work throughout the entire pipeline, however, this block is not mandatory but having the block does 2 things -
Schedules the steps contained within the block to run by adding an item to the Jenkins queue. As soon as an executor is free on a node, the steps will run.
Creates a workspace (a directory specific to that particular Pipeline) where work can be done on files checked out from the source control.
Jenkinsfile (Scripted Pipeline)
node {
stage('Build') {
//
}
stage('Test') {
//
}
stage('Deploy') {
//
}
}
Task:
- Create a New Job, this time select Pipeline instead of Freestyle Project.
- Follow the Official Jenkins Hello World example.
\> Scripted Pipeline to print "Hello world".
\> Output
- Complete the example using the Declarative pipeline.
\> Defined a Jenkinsfile in the repo.
\> Configure the pipeline accordingly.
\> Provide the path to Jenkinsfile.
\> The pipeline builds successfully.
Thank you for reading! 📘