Table of Contents
About Jenkins
Jenkins is an Open source, Java-based automation tool. This tool automates the Software Integration and delivery process called Continuous Integration and Continuous Delivery. Jenkins support various popular Version control system, Software build, and delivery tools. Over the Years, Jenkins become giant in CICD process, especially new features like Jenkins Pipelines (Scripted and Declarative Pipeline) makes the delivery process very easy and help Team to adopt DevOps easily.
Jenkins, From Zero To Hero: Become a DevOps Jenkins Master
Become a DevOps Master learning Jenkins & integrations with powerful tools like Docker, Ansible, AWS, GIT & more!
Limited time 97% offer on Udemy.
Jenkins pipeline
Jenkins Pipeline is a stack of Jenkins plugins and other tools which helps implementing and continuous integration and delivery pipelines. In Jenkins, Pipelines are written in DSL code which implements this continuous integration and delivery pipeline jobs.
Get the pipeline plugin from the Jenkins plugin market place and install into the Jenkins instance. For the same, Go to Manage Jenkins > Manage Plugins. > Available tab, search for “build pipeline”.
This Jenkins pipeline can be built using Web UI or Scripted Jenkinsfile. This Jenkinsfile is written with Groovy DSL and updated in configuration page on the Jenkins job. Overall, Jenkinsfile can also be called pipeline as code.
Types of Jenkins pipeline
There are two type of Jenkins pipeline based on which format the Jenkinsfile is written.
- Declarative pipeline
- Scripted pipeline
Declarative Pipeline
The Declarative pipeline is a new feature that is added to create the pipeline. This is basically written in a
Lets see the structure and syntax of the Declarative pipeline.
The Agent is where the whole pipeline runs. Example, Docker. The
- any – Which mean the whole pipeline will run on any available agent.
- none – Which mean all the stages under the block will have to declared with agent separately.
- label – this is just a label for the Jenkins environment
- docker – this is to run the pipeline in Docker environment.
The Declarative pipeline code will looks like this:
pipeline {
agent { label 'node-1' }
stages {
stage('Source') {
steps {
git 'https://github.com/digitalvarys/jenkins-tutorials.git''
}
}
stage('Compile') {
tools {
gradle 'gradle4'
}
steps {
sh 'gradle clean compileJava test'
}
}
}
}
Scripted Pipeline
The scripted pipeline is a traditional way of writing the Jenkins pipeline as code. Ideally, Scripted pipeline is written in Jenkins file on web UI of Jenkins. Unlike Declarative pipeline, the scripted pipeline strictly uses groovy based syntax. Since this, The scripted pipeline provides huge control over the script and can manipulate the flow of script extensively. This helps developers to develop advance and complex pipeline as code.
Let us see the structure and syntax of the scripted pipeline
Node Block:
Node is the part of the Jenkins architecture where Node or agent node will run the part of the
node {
}
Stage Block:
Stage block can be a single stage or multiple as the task goes. And it may have common stages like
- Cloning the code from SCM
- Building the project
- Running the Unit Test cases
- Deploying the code
- Other functional and performance tests.
So the stages can be written as mentioned below:
stage {
}
So, Together, the scripted pipeline can be written as mentioned below.
stage {
}
So, Together, the scripted pipeline can be written as mentioned below.
node ('node-1') {
stage('Source') {
git 'https://github.com/digitalvarys/jenkins-tutorials.git''
}
stage('Compile') {
def gradle_home = tool 'gradle4'
sh "'${gradle_home}/bin/gradle' clean compileJava test"
}
}
Run the pipeline
Now lets configure the Jenkins pipeline. Following are the steps to create Jenkins Pipeline.
Step 1:
Select “New Item” from the dashboard of the Jenkins instance
Step 2:
Name the item and select pipeline as the style of the job item.
Step 3:
Scroll down and select the definition of the Pipeline. Here we get two options.
- One is the Pipeline script from SCM which is for Declarative Pipeline
- Another is the Pipeline script which is for Scripted Pipeline that we will write directly on UI.
If it is the
If we are selecting the Pipeline script from SCM definition, then we need to select the Git > SCM Repository URL > Credentials. (Declarative Pipeline).
In the Declarative pipeline, We need to select the repository of the
Now let us see how these jobs are executed.
Scripted Pipeline Execution
For testing, we are going to use the following code for the pipeline.
node {
for (i=0; i<2; i++) {
stage "Stage #"+i
print 'This is the stage #' +i
if (i==0)
{
echo 'Running on Stage #0'
}
else {
build 'Tutorial-pipeline'
echo 'Running on Stage #1'
}
}
}
Basically,
- This Script will run two stages as we mentioned in the “For loop”.
- Next, the Condition will execute the stages mentioned in the block when i’ = 0.
- Next condition will execute another job from the Jenkins when ‘i’ not equal to 0.
So once we run the job we will get output in the dashboard (As mentioned in the screenshot)
Declarative Pipeline Execution
Let us take the
pipeline {
agent any
stages {
stage('One') {
steps {
echo 'I am executing stage 1'
}
}
stage('Two') {
steps {
input('In pipeline, We take decision. Can we proceed?')
}
}
stage('Three') {
steps {
echo "Running Stage Three"
}
}
stage('Four') {
steps {
echo "Running another test job"
}
}
}
}
So basically, this script is having 4 stages. And each stage is running in any defined agent. For the Demo purpose, I made it so simple. Once we execute this, we can see the result in the dashboard as shown in the screenshot.
Ideally, we can connect any Build job with these scripts to upstream or downstream the pipelines. So this pipelines will provide continuous delivery right from the small changes from the major changes that are happening from the source code. Especially the pipeline is very useful when we connect multiple environments together and deliver the software. Like, Build – Test – Deploy on every environment. say Dev environment to QA or UAT to Stage to Prod.
Stay tuned. We will see the detailed tutorial of the Scripted and Declarative pipelines and tools like Blue oceans in upcoming articles.
Certified Cloud Automation Architect and DevSecOps expert, skilled in optimizing IT workflows with Six Sigma and Value Stream Management. Proficient in both technical and leadership roles, I deliver robust solutions and lead teams to success.
Pingback: scripted sign up login - loginen.com