Jenkins Basics

Jenkins Basics

Jenkins is an open-source automation server that is used to build, test and deploy software applications. It provides a way to automate the entire software development process, from code integration to testing and deployment. Jenkins can be integrated with a variety of tools and platforms, such as Git Docker, AWS and more, to create a complete DevOps pipeline.

What is CI/CD Pipeline?

A CI/CD pipeline is a series of automated processes that are used to build, test and deploy software applications.

CI stands for Continuous Integration and it refers to the practice of regularly integrating new code changes into a shared code repository and running automated tests to detect issues early in the development cycle.

CD stands for Continuous Deployment or Delivery it refers to the practice of automating the process of deploying new code changes to the production environment.

What is JenkinsFile?

A Jenkinsfile is a text file written in Groovy that defines the steps and stages of a Jenkins Pipeline, enabling the automation of software build, test and deployment processes.

Key Steps to Develop a CI/CD Pipeline for Any Project

Jenkins works on the Master and Slave concept and Jenkins always be installed only in Master Node but Java should be installed on every Node.

Install all the Tools in Master and Slave Instances

We need to install the tool and software for the particular if any project needed to be deployed on Kubernetes so we need to install everything related to Instances. Such as

  • GIT

  • Maven

  • SonarQube

  • Selenium

  • Kubernetes

  • Docker

  • Ansible

We need to install only the software or tool required for the project and also we need to install this software or tools on our Slaves

Commands that need to install Jenkins

sudo apt update
sudo apt install openjdk-11-jdk -y

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

sudo apt update
sudo apt install jenkins

Jenkins is a Web Application and it uses the Port 8080

Edit Inbound Rule of the Master Instance

  • Open the security group of the Instance

  • Edit Inbound Rule

  • Add Custom TCP, Port 8080 and allow access IPv4 anywhere 0.0.0.0/0

To access the Jenkins <Public IP of the Master Node>:8080

  • Setup basics details over there

  • Install suggested plugins

5 Steps to Setup a CICD Pipeline

First Step

  • Manage Jenkins

  • Inside Manage Plugins

  • Install all the plugins that you need for the project such as Docker, GIT, Kubernetes, etc.

Second Step

  • Manage Jenkins

  • Global Tool Configuration

  • Set up the configuration between the other tools or software such as GIT, Maven, JAVA, etc.

Third Step

  • Create a Job or New item

    1. Freestyle project

    2. Maven project

    3. External Job

    4. Multi-configuration project

    5. Folder

    6. GitHub Organization

    7. Multi-branch project

    8. Pipeline

  • In Source Code Management select GIT and paste the GitHub Repository URL

  • Create things according to the Pre and Post Build steps as per the project need

Fourth Step

  • Manage Jenkins

  • Manage Nodes and Cloud > Setup Nodes/Slave

  • Give the name and description

  • Executor should be 1

  • Remote Root Directory (/home/ubuntu/Jenkins)

  • Launch Agent via SSH

  • In Host add the Private IP of the Slave Instance

  • Add Credentials > SSH Username and Private Key > ID (any name) > Description (any) > Username (Ubuntu) > Private Key (Copy paste the .pem file there)

  • Host Key verification strategy (Non verifying verification strategy)

Fifth Step

  • Create Upstream and Downstream according to the project

  • Existing Job Click to the configure

Upstream

  • Build Steps (Execute Window shell) > Write your script

  • Post Build Action > Build Other Project > Select your other Job

Downstream

  • Build Steps (Execute Window shell) > Write your script

  • Build Triggers > Build after other project are built> Select your other job

Other things that you should keep in your mind as an extra knowledge

Build Periodically

It will auto building the builds withing the given time whether you made or not any changes in the code repository.

To make a build in every 1 min so we need to use (* * * * *)

Poll SCM

It is the same as Build Periodically but with Poll SCM it only makes build whenever we had a change in the code repository.

It will make a build after 1 minute for that we need to use (* * * * *)

View

In the dashboard, all jobs are shown with the help of a view that can categorise our job by name such as Freestyle Job, Company X Job, etc.

User Management in Jenkins

We can create multiple users for the multiple tasks

  • Manage Jenkins

  • Manage Users

Create a user (add details)

Created user (by default newly created user has all the admin rights)

To Give and Set Permission

  • Manage Jenkins

  • Manage Plugins

  • Install a Role-based Authorization Strategy

  • Authorize Project

Now time to set up the roles and permission

  • Manage Jenkins

  • Configure global security > Allow users to signup

  • Authorization > Project-based Matrix Authorization Strategy

  • Add users

    • Enter the same username

    • Select permissions

Manage Jenkins again

  • Manage and Assign Role

  • Manage Role (create)

    • Item Role > Role to add (develop) (tester)

    • Pattern > (dev*) any job starts with dev count for the developer

    • Pattern > (test*) any job starts with test count for the tester

  • Assign Role

    • Item Role > Give a username such as Sagar

    • Assign as developer or tester

Global Roles > give ther username and marked as employee