Day 33: Jenkins Declarative pipeline
Streamline CI/CD: Mastering Declarative Jenkins Pipelines
What is Declarative Pipelines?
In Jenkins, a declarative pipeline is a way to define your continuous integration and continuous delivery (CI/CD) pipelines using a structured and human-readable syntax.
Declarative Pipelines VS Freestyle Projects:
Declarative Pipelines
Structured Syntax: Organized, predefined structure for pipelines.
Ease of Use: Simple and easy to understand, even for non-scripters.
Best Practices: Encourages separation of concerns and code reusability.
Stage Visualization: Offers graphical view of pipeline stages.
Limited Customization: May lack flexibility for very complex workflows.
Version Control: Pipeline configurations can be versioned alongside code.
Freestyle Projects:
Flexible Configuration: More free-form configuration of build steps and actions.
No Scripting Required: Configuration through Jenkins UI, no scripting needed.
Simplicity for Basic Tasks: Suitable for straightforward build and deploy tasks.
Reproducibility Challenges: Reproducing consistent configurations can be harder.
Less Version Control: Configurations are stored within Jenkins, less version control.
Setting Up Job: Declarative pipeline
With Jenkins all set up, it's time to create another job. Let's take a look at how you can do that:
Create a New Job:
From the Jenkins dashboard, click "New Item."
Name your job, e.g. "simple-declarative"
Select "pipeline" and click "OK."
Job Configuration:
In the job configuration page under "general", enter a description for the job, e.g. "This is a simple builds"
Pipeline:
In this section, we have to write pipeline syntax (also called groovy syntax) with the following stages
Code
Build
Test
Deploy
Building Stages:
In one single click using the above command we have now built the pipeline with stages in it.
A successful Jenkins pipeline:
If every stage is built correctly without any errors then this means that the whole pipeline is built successfully.
A failed Jenkins pipeline:
It is also equally important to see how does a failed pipeline looks like and if any of the stages fails then all the next stages will fail.
Conclusion:
Declarative pipelines offer a balance between simplicity and flexibility, and they are recommended for most use cases. However, for more complex or custom workflows, you might need to use a Scripted Pipeline, which provides a more extensive scripting capability using Groovy.
Hope you like my post. Don't forget to like, comment, and share.