First taste to “Jenkins Process Automation”

Arati AK Bhandare
7 min readFeb 11, 2023

--

Process automation is defined as the use of software and technologies to automate business processes and functions in order to accomplish defined organisational goals, such as producing a product, hiring and onboarding an employee, or providing customer service.

Process automation tool that we are going to explore in today’s article is, “Jenkins”.

Jenkins is the most popular open source CI/CD tool on the market today and is used in support of DevOps, alongside other cloud native tools.

By using Jenkins, software companies can accelerate their software development process, as it automates build and test at a rapid rate.

Jenkins integrates various DevOps stages

What is Jenkins?

  • Jenkins is a continuous integration server (continuous integration server is the practice of running tests on non-developer machine automatically every time when new code is pushed into source repository).
  • It is written in Java.
  • It provides many plugins that help to support building, deploying and automating any project.
  • It can be installed through native system packages, Docker or even run standalone by any machine with the help of JRE (Java Runtime Environment).
  • Jenkins can be used to automate,
    - Building an application.
    - Running tests.
    - Deploying the application.
    - Delivering the software.

Official Definition

Jenkins — is an open source automation server which enables developers around the world to reliably build, test, and deploy their software.

How Jenkins works?

Jenkins Workflow

Why Jenkins?

  • Ease of use — Intuitive web interface, ease to navigate, documentation & examples included
  • Free & OpenSource — MIT license use, modify & share s/w for free. Code is available on GitHub anyone can submit enhancements.
  • Extensibility — New features can be developed via Plugins which can add functionality.
  • Community — Millions of users available for help.
  • By automating different phases of the SDLC, Jenkins enables developers to learn about errors in their code at an early stage and deploy quickly

Jenkins Terminology

  • Project/Job — User configured task that Jenkins will perform. They are interchangeable terms.
  • Plugin — Extension to core Jenkins functionality.
Build Process

Jenkins Installation Guide

Detailed step by step guide for Jenkins installation can be found here 👈🏻

Overview of Jenkins Interface

Jenkins User Interface sample
  • New item — Creating jobs / resources
  • Manage Jenkins — Access to various configurations to Jenkins server
    - Global configuration tool — To install tools that all jobs can use. Configure Jenkins to manage several tools like JAVA, Git, Maven etc.
    - Manage Plugins — Update installed plugins or to install new plugins from ‘available’ tab or to uninstall existing plugins from “installed” tab.
  • Build queue — Shows list of builds waiting to be run
  • Build executor status — What builds are running at the moment.

Create your first Jenkins Job

Avoid using “space” in name, instead use hyphen or underscore
  • Select “New item” from LHS menu on Jenkins welcome screen.
  • Add “Hello-World” name.
  • Choose Freestyle project option and click ok.
Configure “Add Build” under Build Tab
  • For Windows select, “Execute Windows batch command”
  • For Ubuntu / MacOS / Docker system select, “Execute shell”

In form add below code,

echo "Hello, Jenkins!"
  • Click “Apply” to change configurations and finally click “Save”.
  • To run the job click “Build Now” link from LHS menu.
  • Under “Build History” you will see, green tick, #1 & timestamp
Build is successful, clicking on tick will take you to “Console Output” where you can see results

Jenkins Job Configuration

How you can efficiently do Jenkins jobs configuration is explained in detail in a separate article, you can click here 👈🏻 to read more.

Run & Monitor Jobs in Jenkins

Let us now understand how Jenkins interacts and monitors a running job and also about some of the information we can get once job is completed.

  • Create a freestyle job named “long-running-job”. This job runs for 30 seconds.
  • We add 1 build step with bash script.
This job runs a script that counts from 1 to 30, sleeping for 1 second between each count.
Job build in progress
  • Lets build the job,
    - Blue circle let you know job is not run before, so there is no status report on the history of job.
    - 1 is build ID. It is unique and it lets Jenkins organise builds associated with job.
    - Progress bar is a visual indicator of how long job has been running and how much long job has to go before completing.
    - Stripe pattern means no data has been collected.
    - Once Jenkins has run this job few times, bar gives a better indication of how long job will take to run.
    - Once job finishes blue circle turns into green tick.
Observe differences in progress bar, status icon when job is run 2nd time
  • You can view job o/p by clicking on green tick or by selecting “console output” from dropdown on build number.
  • In console o/p section you can see who ran the build, where it was run and o/p build produced.
  • If you wish to see o/p without HTML or graphics, click on “view as plaintext” link from LHS panel.
“Long running job” build o/p as plain text
  • If you wish to see build o/p on the fly while job is running
    - Go to job page
    - click on “Build Now”
    - Click on tick mark next to build to open console o/p
    - You will be able to see how Jenkins is updating console o/p while job runs
    - Page scrolls automatically to show most recent o/p without scrolling
Run and monitor job output on the fly
  • Let’s look at what happens when job doesn’t run successfully but goes into error
    - Go to job page
    - Click “configure”
    - Go to build section and edit script
    - By default Jenkins will consider build failed if any command in build step finishes with exit value other than zero. Let’s add below code in Execute shell, after our loop code.
for i in {1...30}; do echo $i; sleep 1; done;
exit 1

Now save and run the job. At the end we see that job is failed. In build history you will see the red cross next to build number indicating that build failed.

Build trends history
  • Build Trends
    - Click “trend” link next to build history
    - It has timeline & build time trend. You see recent run jobs & their statuses in relation to where they run.
    - If you click on top lighter grey field, you can scroll by day.
    - If you click on bottom grey field, you can scroll by hour.
    - Below timeline is report showing how long each build took to run and status(success/failure) of each build.
    - Build time trend helps you track time a build needed to complete.
Clicking on link on timeline gives report on that build with link to log and the time build started and finished.

Jenkins “Jobs” detail overview

How you can do “more” with Jenkins jobs is explained in detail in a separate article, you can click here 👈🏻 to read more.

Organise your Jenkins setup

To know more as to, how you can neatly organise your Jenkins Dashboard manage hundreds of Jobs in seamless manner, click here 👈🏻

Introduction to Jenkins “Pipeline”

So far we have been configuring Jenkins jobs by manually clicking certain options and typing scripts, but Jenkins allows us to configure jobs as code, these types of jobs are Pipelines.

  • Pipeline is written into a text file called, Jenkinsfile
  • Since it’s just text it can be versioned and stores in a repo along with other code or configuration used in a project.
  • Pipelines can configure all aspects of Jenkins jobs, it also contains stages & steps
  • Stages — represent sections of the pipeline.
# Sample Jenkins Pipeline code for testing purpose

pipeline {
agent any
options {
buildDiscarder(logRotator(daysToKeepStr: '10', numToKeepStr: '10'))
timeout(time: 12, unit: 'HOURS')
timestamps()
}
triggers {
cron '@midnight'
}
stages {
stage('Initialize') {
steps {
echo 'Initializing..'
}
}
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
  • Steps
    - They are actions taken in each stage for eg: build stage might have 2 steps, checkout & compile code.
    - They are configured inside a stage, each stage must at least have 1 step.

Creating a sample Jenkins Pipeline

Choose “pipeline” option to configure new item
Configuration options are similar to that in free-style project
pipeline script — provides option to copy+paste script
  • You can also pull pipeline code from repo by using “Pipeline script from SCM” option
Copy+Paste pipeline config script, click save and build
  • When job completes we will have a nice graphical representation of the flow of project.
  • Pipeline views can be beneficial to see logs at one specific section instead of console output.

Conclusion

Perspective of this article series was to introduce you to Jenkins and to get familiarise with basic setup and configuration settings. I wanted to introduce you to the basics of using Jenkins and its main feature

It is an ideal read for those who are planning to start their Jenkins learning journey.

In future articles we will going in depth and explore advance topics like version control in Jenkins, pipelines in detail, agents and distributed builds, testing and security controls and much more.

Hope you enjoyed 🤓💡👍🏻 learning Jenkins with me. You can also refer to getting started guide from Jenkins official website.

#happycoding

--

--