Today will deep dive into the declarative pipeline.
An integral part of your DevOps and CI/CD journey involves mastering the Declarative Pipeline Syntax within Jenkins.
Key Concepts to know:
Pipeline: It's a sequence of interconnected steps or jobs.
Declarative: A newer, advanced way to define pipelines as code.
Scripted: The initial and more traditional approach for writing pipeline code in Jenkins using Groovy, designed as a versatile DSL (Domain Specific Language).
Why you should have a Pipeline?
A Jenkins Pipeline's definition is scripted within a text file known as a Jenkinsfile, which can be committed to the project's source control repository. This practice forms the basis of 'Pipeline-as-code,' considering the Continuous Delivery pipeline as an integral part of the application, subject to versioning and review just like any other code.
When a Jenkinsfile is created and added to source control, it offers immediate advantages:
Automatically sets up a Pipeline build process for all branches and pull requests.
Enables code review and iteration on the Pipeline, treated similarly to the rest of 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.
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.
Task-01
- Create a New Job, this time select Pipeline instead of Freestyle Project.
To create a Declarative pipeline in Jenkins, go to Jenkins UI and click on New item.
Enter the pipeline name and select the type as Pipeline, and then click on ok.
- Go to project configuration page
Now go to the pipeline session, In definition select Pipeline script and add the script.
Declarative Pipeline: The Declarative pipeline must commence with the mandatory 'pipeline' block.
Agent: Defines where the Jenkins build job should execute. In this instance, we've set the agent to 'any.'
Stages: The 'stages' block contains various executable 'stage' blocks. At least one 'stage' block is required within the 'stages' block. Here, we've named the stage 'Hello.'
Steps: The 'steps' block comprises the actual operations performed within Jenkins. In the provided example, we're printing 'Hello World.'
Build the project: You can initiate a manual build by clicking on the 'Build Now' link located on the main page of the project.
After a build is completed, you can view the console output by clicking on the "Console Output" link in the build page.
Let's explore more.
Now we create a Jenkins file in todo-app node.js project with pipeline syntax and keep it in the repository.
Jenkinsfile
we also write Docker-compose.yaml
Let's build a pipeline for above repository using jenkinsfile and docker-compose file as below.
Configure the below steps as important steps for pipeline building.
Build the pipeline to check error/success.
Console output
we can navigate deployment using puplic-ip:8080
I'm optimistic that today's task will provide even more valuable real-time project deployement experience
Thanks,
Kishor Chavan