What is a Jenkins Pipeline?
A Jenkins Pipeline is a suite of plugins supporting the implementation and integration of continuous delivery pipelines. A pipeline has an extensible automation server for creating simple or complex delivery pipelines "as code." The defining feature of a Jenkins Pipeline is that its definition is written in a text form (usually the "Jenkinsfile") which can be checked into a project's source control repository.
Key benefits:
Versioning: Since the pipeline script (Jenkinsfile) is stored in a version control system, it brings versioning to your pipelines, making it easier to rollback, audit, and collaborate.
Code review: The pipeline code can be reviewed and iterated just like any other codebase.
Reusability: Commonly used parts can be reused across different projects.
Better visualization: The Jenkins UI provides a visualization of the pipeline’s execution.
Types of Jenkins Pipelines:
Declarative Pipeline: Introduced for simplicity, it uses a structured syntax with pre-defined sections. This makes it easier for beginners.
Scripted Pipeline: Written in Groovy DSL, it offers more flexibility but requires a deeper understanding of the Groovy syntax.
Declarative Pipeline Skeleton
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "Build"
}
}
stage('Test'){
steps {
echo "Test"
}
}
stage('Deploy') {
steps {
echo "Deploy"
}
}
}
}
Create Declarative Pipeline
Select New Item and follow as below
Make a healthy habit to always mention Description of the pipeline as below
Go to Pipeline and write pipeline code; save it.
Once the job is done. You can see job dashboard like below.
Click on Build now and here is the output.
Your Pipeline is working fine! So, you understood the how to create the configuration right? The basics are done!
Now let's dive into some serious yut exciting examples.
GitHub URL : https://github.com/kshitijabartakke/calculategrowth.git
Pipeline :
pipeline {
agent any
stages{
stage("Code"){
steps{
git url: "https://github.com/kshitijabartakke/calculategrowth.git", branch: "main"
}
}
stage("Build & Test"){
steps{
sh "docker build -t kshitibartakke/calculategrowth ."
}
}
stage("Docker Run"){
steps{
sh"docker run -p 8501:8501 kshitibartakke/calculategrowth"
}
}
stage("Deploy"){
steps{
sh "docker-compose down && docker-compose up -d"
}
}
}
}
Jenkins Pipeline Output:
URL: Public IP address of instance:8501
The app is now running at the localhost address provided by the Docker interface.