Updated April 20, 2023
Introduction to Jenkins Pipeline
Jenkins pipeline is a collection of Jenkins features, which is installed as a plugin. Jenkins pipeline is a combination of Jenkins features and installed plugins, which help in enabling the implementation of no. of continuous delivery pipelines. Jenkins pipelines provide a process for getting source code from any source code management tools through deployment to end-users. In Jenkins, pipeline jobs are suitable for complex activates that do not easily fit in a free-style job type. Jenkins pipeline supports help us to achieve Continuous Delivery and continuous integrations through the scripted Jenkins file or Jenkins UI.
Ways of Developing Jenkins Pipeline
Jenkins provides two ways of developing a pipeline code:
1. Scripted Pipeline
It is based on Groovy script as their Domain Specific Language. One or more node blocks do the core work throughout the entire pipeline. Please find the below syntax for Scripted Pipeline
Jenkinsfile (Scripted Pipeline)
Node{
stage(build) {
} stage(test) {
}
stage(deploy) {
}
}
Explanation of above syntax:
Node{ -> Execute his pipeline or any of its stages on any of the available agent
stage(build) { -> Defines the build stage
perform the step related to build stage
stage(test)
performs steps related to test stage
stage(deploy) {
define the deploy stage and perform the step related to deploy stage
2. Declarative Pipeline
It provides a simple and friendly syntax to define a pipeline without the need for a groovy script. Here pipeline block defines the work done throughout the pipeline. Please find the syntax below:-
Jenkins file (Declarative Pipeline)
Pipeline{
agent any
stages
stage('build') {
steps{
}
}
stage('test') {
steps{
}
}
stage('deploy') {
steps{
}
}
Explanation of above syntax:
Execute this pipeline or any of its stages on any available agent
Defines the Build stage
Performs steps related to Build stage
Defines the Test stage
Performs steps related to Test stage
Defines the Deploy stage
Performs stops related to Deploy stage
Examples of Jenkins Pipeline
Here are the following examples mention below
Example #1 – Scripted Pipeline
Let’s suppose in any organization want to automate their build process and deploying the code in multiple servers. Please find the step by step procedure for defining the job configuration and deployment.
Log in to Jenkins and create the new pipeline job.
In the pipeline section, we are defining the source code management tool information like git repository url, branch details, and the file which contains the script to be executed.
From this Jenkins dashboard, we have an option for the build now. Once we clicked on the build now, the Jenkins job triggered.
Please find the below pipeline script mentioned in Jenkinsfile. This Jenkins file was configured with the job. We can see there are four stages defined with different instructions. The first job does the checkout from the source code management tool similarly the second job defines the maven build which is responsible for packaging.
The third job defines the deployment where the war file is moving from Jenkins directory and deploy into Tomcat server. On the successful execution of the third job, it will trigger the email which is defined under the fourth job.
The scripted pipeline is used as a block labeled ‘node
node {
// This is to demo github action
def mvn = tool (name: 'maven-3', type: 'maven') +'/bin/mvn'
stage('SCM Checkout'){
// Clone repo
git branch: 'master',
credentialsId: 'github',
url:'https://github.com/Smaheshwar85/Devops_basic_step'
}
stage('Mvn Package'){
// Build using maven
sh "${mvn} clean package"
}
stage('deploy-dev'){
def tomcatDevIp ='172.31.59.249'
def tomcatHome ='/opt/tomcat/'
def webApps = tomcatHome+'webapps/'
def tomcatStart ="${tomcatHome}bin/startup.sh"
def tomcatStop ="${tomcatHome}bin/shutdown.sh"
sshagent (credentials: ['tomcat-dev']) {
sh "scp -o StrictHostKeyChecking=no target/myweb*.war ubuntu@${tomcatDevIp}:${webApps}/"
sh "ssh ubuntu@${tomcatDevIp}${tomcatStop}"
sh "ssh ubuntu@${tomcatDevIp}${tomcatStart}"
}
}
stage('Email Notification'){
mail bcc: '', body: """Hi Team, You build successfully deployed
Job URL : ${env.JOB_URL}
Job Name: ${env.JOB_NAME}
Thanks,
DevOps Team""", cc: '', from: '', replyTo: '', subject: "${env.JOB_NAME} Success", to: '[email protected]'
}
}
Whenever any maven based job triggered by Jenkins, it starts looking at the POM.xml.This POM file has all the information about the environment and project related information. Please find the below pom file:-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>in.cba</groupId>
<artifactId>myweb</artifactId>
<packaging>war</packaging>
<version>0.0.7-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
</plugin>
</plugins>
</build>
</project>
Example #2 – Declarative pipeline
The declarative pipeline is used as a block labeled ‘pipeline’. We have a workflow with different stages that we have configured using the declarative pipeline. Please find the below code for the declarative pipeline which define the series of instruction like scm checkout,mvn packages,deploy-dev.
pipeline{
agent any
environment{
PATH = "/opt/maven3/bin:$PATH"
}
stages{
stage("SCM Checkout"){
steps{
git credentialsId: 'github', url: 'https://github.com/Smaheshwar85/Devops_basic_step'
}
}
stage("'Mvn Package"){
steps{
sh "mvn clean package"
sh "mv target/*.war target/myweb.war"
}
}
stage("deploy-dev"){
steps{
sshagent(['tomcat-new']) {
sh """
scp -o StrictHostKeyChecking=no target/myweb.war [email protected]:/opt/tomcat8/webapps/
ssh [email protected] /opt/tomcat8/bin/shutdown.sh
ssh [email protected] /opt/tomcat8/bin/startup.sh
"""
}
}
}
}
}
Post configuration into the go-to Jenkins dashboard and click on to the build now option. Once the build started we can go to console by clicking on the currently running job and monitor accordingly.
In below screenshot, we can see the three stages where the first job do the checkout from source code management tool git the next job define the maven build and do the package
The third job defines the deployment where the war file is moving from Jenkins directory and deploys into the Tomcat server.
Always by default, the war is generated under the target folder and deployed to the webapps in the tomcat folder.
Difference between the Declarative pipeline and Scripted pipeline
- The main difference between the Declarative pipeline and the scripted pipeline is their syntaxes and their flexibility.
- The Declarative pipeline is used pipeline as code for easier to read and write whereas pipeline as code is used normal script way.
- The declarative pipeline is used as a block labeled ‘pipeline’ whereas the scripted pipeline is used as a ‘node’.
Recommended Articles
We hope that this EDUCBA information on “Jenkins Pipeline” was beneficial to you. You can view EDUCBA’s recommended articles for more information.