Are you a DevOps Enthusiast looking to explore different tools? If yes, then you have landed at the right place. DevOps Engineers are the most demanded and payed professionals all around the world. With this in mind let me introduce you to the most popular DevOps Tool, Jenkins. This Jenkins cheat sheet is for beginners who have prior knowledge about how a software development process occurs.

Jenkins Cheat Sheet

Continuous Integration is a software development practice in which developers are required to frequently commit changes to the source code in a shared repository. Each commit is then continuously pulled & built. Jenkins is an open source, Continuous Integration (CI) tool, written in Java. It continuously pulls, builds and tests any code commits made by a developer with the help of plugins.

Installation

Let’s start by installing Jenkins. This installation is specific to systems operating on Ubuntu. Follow the below steps:

Jenkins Pipeline

Jenkins pipeline is a single platform that runs the entire pipeline as code. Instead of building several jobs for each phase, you can now code the entire workflow and put it in a Jenkinsfile.

Jenkinsfile is a text file that stores the pipeline as code. It is written using the Groovy DSL. It can be written based on two syntaxes:

Scripted pipeline

Code is written on the Jenkins UI instance and is enclosed within the node block

node {
scripted pipeline code
}

Declarative pipeline

Code is written locally in a file and is checked into a SCM and is enclosed within the pipeline block

pipeline {
declarative pipeline code
}

Pipeline Concepts

The below fundamentals are common to both, scripted and declarative pipeline:

Pipeline: A user defined block which contains all the stages. It is a key part of declarative pipeline syntax.

Node: A node is a machine that executes an entire workflow. It is a key part of the scripted pipeline syntax.

Agent: instructs Jenkins to allocate an executor for the builds. It is defined for an entire pipeline or a specific stage.

It has the following parameters:

Any: Runs pipeline/ stage on any available agent

None: applied at the root of the pipeline, it indicates that there is no global agent for the entire pipeline & each stage must specify its own agent

Label: Executes the pipeline/stage on the labelled agent.

Docker: Uses docker container as an execution environment for the pipeline or a specific stage.

Stages: It contains all the work; each stage performs a specific task.

Steps: steps are carried out in sequence to execute a stage

Create your first Jenkins Pipeline

After installing Jenkins, building jobs using the Build pipeline and briefly discussing about pipeline concepts, let’s see how to create a Jenkins pipeline.

Follow the below steps to create both, a scripted pipeline and a declarative pipeline:

Jenkins Pipeline syntax example

Step 1: Log into Jenkins and select ‘New Item from the Dashboard'
Step 2: Next, enter a name for your pipeline and select ‘Pipeline project’. Click ‘ok’ to proceed
Step 3: Scroll down to the pipeline and choose if you want a Declarative or Scripted pipeline
Step 4a: If you want a Scripted pipeline, then choose ‘pipeline script’ and start typing your code
Step 4b: If you want a Declarative Pipeline, select ‘Pipeline script from SCM’ and choose your SCM and enter your repository URL
Step 5: Within the Script path is the name of the Jenkinsfile that is going to be accessed from your SCM to run. Finally click on ‘apply’ and ‘save’

Schedule a build periodically

Jenkins uses a cron expressions to schedule a job. Each line consists of 5 fields separated by TAB or whitespace:

Syntax: (Minute Hour DOM Month DOW)
MINUTE: Minutes in one hour (0-59)
HOURS: Hours in one day (0-23)
DAYMONTH: Day in a month (1-31)
MONTH: Month in a year (1-12)
DAYWEEK: Day of the week (0-7) where 0 and 7 are sunday
Example: H/2 * * * * (schedule your build for every 2 minutes)

Try this example:

H/2 * * * * (schedules your build for every 2 minutes)

Snippet Generator

A tool that lets users generate code for individual steps in a scripted pipeline. Let’s look at an example:

With this, we come to an end to Jenkins Cheat Sheet. Check out the DevOps Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s DevOps certification Training is designed to provide you with the knowledge and skills that are required to become a successful DevOps Engineer.