What is a Pipeline in Jenkins?☑️
A Pipeline is a series of automated steps or jobs linked together in a specific order to achieve continuous integration, continuous delivery, or continuous deployment in software development. Pipelines allow developers to automate building, testing, and deploying applications, ensuring a consistent and reliable workflow.
Types of Jenkins Pipelines⚙️
Declarative Pipeline:
The Declarative Pipeline is a newer and more organized way to write a pipeline as code in Jenkins. It has a simpler and clearer syntax, making it easier to understand and maintain. This style uses a set structure, making it more readable and ideal for most users, especially beginners.Syntax:
pipeline { agent any stages { stage('Build') { steps { echo 'Building...' // Insert build steps here } } stage('Test') { steps { echo 'Testing...' // Insert test steps here } } stage('Deploy') { steps { echo 'Deploying...' // Insert deploy steps here } } } }
Scripted Pipeline:
The Scripted Pipeline is the older, more flexible way to define a pipeline. It uses a special language called DSL (Domain Specific Language) built with Groovy. While it offers more flexibility and control, it can be more complex and harder to maintain. Scripted Pipelines are useful when you need more complicated logic that the Declarative approach might not easily handle.Syntax:
node { // Define environment variables def myVar = 'Hello World!' // Checkout code from Git stage('Checkout') { echo 'Checking out code...' checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]]) } // Build stage stage('Build') { echo 'Building the application...' sh 'echo "Building..."' } // Test stage stage('Test') { echo 'Running tests...' sh 'echo "Running tests..."' } // Deploy stage stage('Deploy') { echo 'Deploying the application...' sh 'echo "Deploying..."' } // Print environment variable echo "Message: ${myVar}" }
Why We Should Use a Pipeline❓
Version Control Integration:
The pipeline is defined in a text file called a Jenkinsfile, which can be added to a project's source control repository (e.g., Git). This allows versioning and tracking of changes over time. It also makes the pipeline part of the application's code, which can be reviewed and improved just like any other code.Pipeline-as-Code:
Pipeline definition as an integral part of the software project. This approach ensures that the deployment process is consistent, repeatable, and reliable. It allows the pipeline to be versioned, tested, and stored alongside the source code.Automatic Builds for Branches and Pull Requests:
Using a Jenkinsfile, Jenkins automatically sets up pipeline build processes for all branches and pulls requests in the repository. This makes sure that all changes, no matter the branch, are built, tested, and verified, which helps reduce integration problems.Improved Collaboration:
Having the pipeline definition in the same repository as the codebase encourages teamwork and communication among developers. Code reviews can include pipeline changes, and making sure all team members understand the CI/CD process.
Task: 🎯
➤Create a New Job, this time select Pipeline instead of Freestyle Project.
➤Complete the example using the Declarative pipeline
➤Create a new Agent/Node for running the job
Steps:📶
╰┈➤Open Master Machine & Copy public key:
ubuntu@ip-172-31-45-42:~$ ssh-keygen
ubuntu@ip-172-31-45-42:~$ cd .ssh
ubuntu@ip-172-31-45-42:~/.ssh$ ls
authorized_keys id_ed25519 id_ed25519.pub
ubuntu@ip-172-31-45-42:~/.ssh$ cat id_ed25519.pub
╰┈➤Paste the public key into the agent machine
ubuntu@ip-172-31-26-86:~$ cd .ssh
ubuntu@ip-172-31-26-86:~/.ssh$ ls
authorized_keys
ubuntu@ip-172-31-26-86:~/.ssh$ vim authorized_keys
╰┈➤Update the package of the Agent Machine
sudo apt-get update
╰┈➤Install Java into the Agent Machine
sudo apt install fontconfig openjdk-17-jre
╰┈➤Create a new node
╰┈➤Copy agent directory path
╰┈➤Paste into Jenkin node
╰┈➤Add the agent IP address
╰┈➤Add Credentials
╰┈➤Copy the private key from the master
╰┈➤Paste into Jenkins Node
╰┈➤Save the node configuration
╰┈➤Now node is created and connected to the EC2 - Agent machine
╰┈➤Create a new Declarative pipeline
╰┈➤Add description
╰┈➤Write a groovy script
╰┈➤Click on Build to run the job
╰┈➤The job runs successfully on the agent now by using the pipeline
Conclusion:💡
By following these steps, We can create a simple Jenkins pipeline using Declarative syntax. This example shows how easy and powerful Jenkins Pipelines are for automating the CI/CD process, ensuring consistent and reliable software delivery.