Some terms for your Knowledge
What is Pipeline - A pipeline is a collection of steps or jobs interlinked in a sequence.
Declarative: Declarative is a more recent and advanced implementation of a pipeline as a code.
Scripted: Scripted was the first and most traditional implementation of the pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy.
Why you should have a Pipeline
The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile
) which in turn can be committed to a project’s source control repository.
This is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code.
Creating a Jenkinsfile
and committing it to source control provides a number of immediate benefits:
Automatically creates a Pipeline build process for all branches and pull requests.
Code review/iteration on the Pipeline (along with the remaining source code).
Pipeline syntax
pipeline {
agent any
stages {
stage('Build') {
steps {
//
}
}
stage('Test') {
steps {
//
}
}
stage('Deploy') {
steps {
//
}
}
}
}
Creating your first Pipeline
Provide a name for your new item (e.g. Dec-Pipeline) and select Pipeline
Now Select Pipeline and enter the below pipeline to validate the execution steps
pipeline { agent any stages { stage('Initalisation Phase') { steps { script { echo 'Running Stage 1...' } } } stage('Staging') { steps { script { echo 'Running Stage 2...' } } } stage('Execution Phase') { steps { script { echo 'Running Stage 3...' } } } } post { success { echo 'Pipeline executed successfully!' } } }
Let's break down the provided declarative Jenkins pipeline:
pipeline { agent any
pipeline
: This keyword marks the beginning of the pipeline block.agent any
: This specifies that the pipeline can run on any available agent (Jenkins node). Theany
keyword means it is not restricted to a specific agent.
stages {
stage('Initialization Phase') {
steps {
script {
echo 'Running Stage 1...'
}
}
}
stage('Staging') {
steps {
script {
echo 'Running Stage 2...'
}
}
}
stage('Execution Phase') {
steps {
script {
echo 'Running Stage 3...'
}
}
}
}
stages
: This block defines the different stages of the pipeline. In this example, there are three stages:Initialization Phase
,Staging
, andExecution Phase
.steps
: Each stage can have one or more steps defined in thesteps
block. In this case, there is a single step in each stage.script
: Thescript
block allows you to execute arbitrary Groovy script. In each stage, it's used to print a message using theecho
command.
post {
success {
echo 'Pipeline executed successfully!'
}
}
post
: This block is used to define post-build actions or conditions. In this case, there is asuccess
block, which will be executed if the pipeline completes successfully.echo 'Pipeline executed successfully!'
: Inside thesuccess
block, it prints a success message using theecho
command.
In summary, this pipeline has three stages with corresponding messages, and it prints a success message if all the stages complete successfully.
Click the Save button and watch your first Pipeline run
Conclusion
To sum it up, the exploration of Jenkins Multibranch Pipelines has uncovered a world of possibilities in continuous integration and delivery. With the power to automate the building and deployment of multiple branches, Jenkins proves to be a valuable asset.
As we continue the DevOps journey, mastering Jenkins Multibranch Pipelines opens the door to building resilient and scalable software solutions. Cheers to more exploration and learning in the world of DevOps!