Day 24–101 Days of DevOps -Jenkins Pipeline(Scripted vs Declarative)
Welcome to Day 24 of 101 Days of DevOps. The topic for today is Jenkins Pipeline(Scripted vs Declarative). Before we dig deeper into this topic let first discuss what Jenkinsfile is.
To view the complete course, please check the below url.
For more info, register via the below link
YouTube Channel link
What is a Jenkinsfile?
A Jenkinsfile is simply a text file that contains the steps needed to run the Jenkins pipeline.
Some of the advantages of using Jenkinsfile
- You can check it in the source control.
- Perform code review.
- Become a single source of truth, which can be viewed and edited by multiple members of the project.
Jenkins pipeline support two syntaxes
- Declarative Pipeline: Declarative pipeline is the new addition to Jenkins which supports pipeline as code. It makes pipeline code easier to write and read.
- Scripted Pipeline: Scripted Pipeline is the traditional way of writing the code and it offers a tremendous amount of flexibility and extensibility to Jenkins users.
Where they differ however is in syntax and flexibility. Declarative limits what is available to the user with a more strict and pre-defined structure, making it an ideal choice for simpler continuous delivery pipelines. Scripted provides very few limits, insofar that the only limits on structure and syntax tend to be defined by Groovy itself, rather than any Pipeline-specific systems, making it an ideal choice for power-users and those with more complex requirements. As the name implies, the Declarative Pipeline encourages a declarative programming model. Whereas Scripted Pipelines follow a more imperative programming model.
For more info please refer to https://www.jenkins.io/doc/book/pipeline/syntax/#compare
Declarative Pipeline Example
- The declarative pipeline started with a pipeline block.
- Agent directive is a must for declarative pipeline and it allocates executor and workspace to the pipeline. It also makes sure the source repository is checked out and made available for steps in the subsequent stages.
- Stages and steps directive instructs Jenkins what to execute and in which stage it needs to be executed.
# Official Declarative Pipeline example
pipeline {
agent anystages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
Scripted Pipeline Example
- The declarative pipeline started with a node block.
# Official Scripted Pipeline example
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'M3'
}
stage('Build') {
// Run the maven build
withEnv(["MVN_HOME=$mvnHome"]) {
if (isUnix()) {
sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
} else {
bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archiveArtifacts 'target/*.jar'
}
}
For more info please check the youtube video
https://www.youtube.com/watch?v=P3dC8YKIt4o
If you like to dig deeper into the AWS concept, please feel free to check my book.
Looking forward to you guys joining this journey and spend a minimum of an hour every day for the next 101 days on DevOps work and post your progress using any of the below mediums.
- Website: https://101daysofdevops.com/
- Linkedin: https://www.linkedin.com/in/prashant-lakhera-696119b/
- Twitter: @100daysofdevops OR @lakhera2015
- Facebook: https://www.facebook.com/groups/795382630808645/
- Medium: https://medium.com/@devopslearning
- GitHub: https://github.com/100daysofdevops/100daysofdevops
- YouTube Channel: https://www.youtube.com/user/laprashant/videos
- Slack: https://join.slack.com/t/100daysofdevops/shared_invite/zt-au03logz-YfDUp_FJF4rAUeDEbgWmsg
- Reddit: r/101DaysofDevops
- Meetup: https://www.meetup.com/100daysofdevops/