Jenkins Pipeline as Code

Reddeppa S
4 min readOct 16, 2021

--

Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins.

The steps required for CICD will be written in a text file called Jenkinsfile. This file should be present in your git repository root directory. This file can be written in two different types of syntaxs.

  1. Declarative Pipeline is a more recent feature of Jenkins Pipeline which provides richer syntactical features over Scripted Pipeline syntax. It is designed to make writing and reading Pipeline code easier.
  2. Scripted Pipeline starts with node block and is expressed in groovy language. so programming flexibilities are available in the scripted pipeline
Declarative Pipeline
Scripted Pipeline

Writing your first Pipeline:

For the demo application, you can follow the code from the GitHub link https://github.com/reddeppas/jenkins_pipeline_as_code

Install Jenkins

Windows:

Download windows Jenkins installer and follow the onscreen instructions
https://www.jenkins.io/download/#downloading-jenkins

Linux: Debian/Ubuntu:

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > \
/etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

Mac:

brew install jenkins-lts

Jenkins can be accessed from http://localhost:8080. For the first time, Jenkins will generate a password and input that password as shown below

We can now Customize Jenkins and install some plugins. For now, we’re going to choose Install suggested plugins.

create an admin account in the next step

Login Screen

Install Vagrant

Windows: Download windows installer and follow onscreen instructions https://releases.hashicorp.com/vagrant/2.2.18/vagrant_2.2.18_x86_64.msi

Linux:

ubuntu:
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install vagrant

Mac:

brew install vagrant

Install Oracle Virtual Box

Windows:

download windows installer and follow onscreen instructions
https://download.virtualbox.org/virtualbox/6.1.26/VirtualBox-6.1.26-145957-Win.exe

Mac:

Download https://download.virtualbox.org/virtualbox/6.1.26/VirtualBox-6.1.26-145957-OSX.dmg and install.

After installations are done run the below commands. Vagrant will spin up Unix Ubuntu VM

vagrant up

vagrant ssh

Add vagrant machine as a node in Jenkins

Click on Manage Jenkins

Select Manage Nodes and Clouds

Click on New Node and give a name to the node and Click Ok

In Next screen update

home directory -> /home/vagrant labels

stage launch method -> launch agents via ssh

host -> localhost

credntials -> Add creditnitals as vagrant user name and password as vagrant. click on Advanced and update ssh port from 22 to 22222

and save. Jenkins will try to connect to the node. We will be using this node in the pipeline to run our builds, test, and deploy

Create a Pipeline

Select Pipeline script from Scm select scm -> git, input gitrepository url -> https://github.com/reddeppas/jenkins_pipeline_as_code.git

update branches from master -> main

step6: Click on Build Now

Plugins :

Cobertura, code coverage API, Post Build Task, SSH Build Agents, Gradle

--

--

No responses yet