Day 26 Task: Jenkins Declarative Pipeline
Streamlining CI/CD with Jenkins Declarative Pipelines
One of the most important parts of your DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins
🔶 Some terms for your Knowledge
🔸 What is a 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 several 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 {
//
}
}
}
}
🔶 Task-01: Create a Jenkins Declarative Pipeline
Create a New Job, this time select Pipeline instead of Freestyle Project.
1. Set Up Jenkins:
Install and configure Jenkins on your server or cloud platform.
2. Create a New Item:
Go to your Jenkins dashboard and click on "New Item."
Enter a name for your project, select "Pipeline," and click "OK."
3. Configure Pipeline:
In the project configuration, scroll down to the "Pipeline" section.
Select "Pipeline script" from the "Definition" dropdown.
4. Write Declarative Pipeline Script:
In the script area, write your Declarative Pipeline script.
Here's an example of a HelloWorld application:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building HelloWorld application...'
}
}
stage('Test') {
steps {
echo 'Testing HelloWorld application...'
}
}
stage('Deploy') {
steps {
echo 'Deploying HelloWorld application...'
}
}
}
post {
success {
echo 'Pipeline succeeded! HelloWorld application deployed.'
}
failure {
echo 'Pipeline failed! Please check the logs.'
}
}
}
5. Save and Run:
Click "Save" to save your pipeline configuration.
Click "Build Now" to run the pipeline.
6. Monitor Pipeline:
Monitor the progress of your Declarative Pipeline on the Jenkins dashboard.
Click on the pipeline run to view logs and details.
This example creates a simple Declarative Pipeline job that builds, tests, and deploys a HelloWorld application. Declarative Pipelines provide a structured and easy-to-read way to define your CI/CD processes in Jenkins.
Follow the Official Jenkins Hello World example.
Happy Learning:)
In conclusion, setting up a Declarative Pipeline job for a HelloWorld application in Jenkins offers a straightforward and organized approach to automating your CI/CD processes. The Declarative Pipeline script provided in this example demonstrates the sequential stages of building, testing, and deploying the application. By following this process, you can leverage Jenkins to efficiently handle the entire software development lifecycle, from source code integration to deployment, while maintaining a clear and concise pipeline definition. Declarative Pipelines simplify the configuration process, making it easier to manage, monitor, and maintain your CI/CD workflows, ultimately contributing to a streamlined and reliable development process.
Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins