DevOps & Automation

What is CI/CD with Jenkins

What is CI/CD with Jenkins

Introduction CI/CD with Jenkins
What is Continuous Integration (CI) (CD) ?

Continuous Integration (CI) and Continuous Deployment/Delivery (CD) are practices in software development that aim to unify and automate the process of building, testing, and deploying code changes to different environments, such as staging, testing, and production. They are leading components of modern software development methodologies and DevOps practices.

Continuous Integration (CI):
Continuous Integration (CI) is a development technique in which code alterations from several developers are included into a common mainline repository several times per day. The major objective of CI is to identify and resolve integration issues early in the development process, promoting collaboration and elevating the quality of software.

Developers constantly commit their code changes to a version control system (like Git) in a CI workflow. An automated build process is started as soon as a commit is made, compiling the code, running tests, and carrying out different checks to make sure the changes are consistent with the existing codebase. Developers are alerted right away if any bugs are found during the automated testing so they can address them right away.

Continuous Deployment/Delivery (CD):

Following the CI process, continuous deployment and continuous delivery are practices that advance automation. They concentrate on automating the deployment of code modifications to production or staging environments, increasing the frequency, dependability, and effectiveness of software delivery.

Continuous Deployment (CD): Using this method, each piece of code that passes the automated tests in the CI process is instantly and automatically deployed to the production environment. In order to send new features and problem fixes to end customers as soon as they are prepared and tested, this enables a quick and effective release cycle.

Popular tools for CICD [Jenkins, bamboo, AWS Code Pipeline etc]

Following are the numerous well-known tools that efficiently support CI/CD processes.

  • Jenkins: A widely used open-source automation server that supports the whole CI/CD workflow and connects with a wide range of tools.
  • GitLab CI/CD: A component of GitLab’s all-in-one DevOps platform, GitLab CI/CD offers powerful CI/CD features with intuitive pipeline configuration.
  • Travis CI: A cloud-based continuous integration solution that easily interacts with GitHub repositories to make setup easier.
  • CircleCI: A scalable, high-performance, cloud-based CI/CD platform with an intuitive user interface.
  • GitHub Actions: Developers can define CI/CD workflows directly in their repositories thanks to GitHub’s built-in functionality.
  • Bamboo is an Atlassian CI/CD server that integrates with their other products, including Jira and Bitbucket.
  • Teamcity: JetBrains’ TeamCity is a potent CI server that supports a variety of build configurations and integrations.
  • Azure Devops: Microsoft’s CI/CD platform offers complete DevOps solutions with close connectivity to Azure services.
  • AWS CodePipeline: Amazon Web Services offers AWS CodePipeline, a fully managed service for continuous integration and delivery.

Jenkins Overview

Jenkins, an open-source automation server. It gives programmers the ability to automate many processes involved in the Software Development lifecycle, such as creating, testing, and delivering programs. Jenkins enables teams to work together more productively by ensuring that code changes are often merged and verified, which minimizes integration problems. It offers extension and customization possibilities to match certain project requirements and supports a wide variety of plugins.

Jenkins’ primary feature is the creation of jobs, which specify particular tasks like writing code, doing tests, or deploying applications. When changes are pushed to a version control system, these jobs can be started manually or automatically. Jenkins provides a web-based interface for simple task and pipeline configuration and monitoring.

Jenkins is a well-liked option among developers and DevOps teams for optimizing their development workflows and guaranteeing the consistent delivery of high-quality software due to its flexibility, scalability, and strong community support.

Why Jenkins? [Used with multiple clouds, Automation, Other benefits]

Jenkins is used with several clouds because it is cloud-independent, collaborating easily with various cloud service providers and offering flexibility and adaptation.

  • Automation: Jenkins streamlines repetitive operations, lowering the chance of human mistake while accelerating and optimizing development.
  • Flexibility: Jenkins is appropriate for a variety of applications because it supports a wide range of programming languages and operating systems.
  • Community Support: Because Jenkins is open-source, it has a vibrant community that offers assistance and ongoing updates.
  • Jenkins is cost-effective because there are no high Licensing Related Fees because it is an open-source technology.
  • Scalability: Because of its distributed nature, Jenkins can manage projects of any scale.
  • Integration: Its vast ecosystem of plugins allows for easy integration with a range of development tools and services.

Getting Started with Jenkins

Jenkins Installation and Setup

Jenkins requires very little code and is easy to install. Jenkins offers the following features depending on the environment you choose to install it in.

  • Linux
  • macOS
  • Windows
  • Other Systems
  • WAR file
  • Other Servlet Containers
  • Docker
  • Kubernetes
  • Offline Installations
  • Initial Settings

Jenkins Configuration and Customization

  • Jenkins Configuration

The post-installation setup process launches once Jenkins has been downloaded, installed, and launched via one of the methods mentioned earlier

  • Jenkins’s Locking
    A new Jenkins instance requires you to enter an automatically created password in order to access it for the first time.
  1. Browse to https://localhost:8080 (or the port you specified when installing Jenkins) and watch for the Unlock Jenkins page to appear.

 Jenkins Agile Conductor of CI/CD

2. The command cat /var/jenkins_home/secrets/initialAdminPassword will print the password at the console. (Please follow the command which show at the unlock jenkins screen)

command

  • Jenkins Plugin Customization

After pasting the password, we will now customize the Jenkins page. For simplicity’s sake, click “Install suggested plugin.”

customize Jenkins

The necessary plugin that Jenkins requires will then start to be installed.
Following the completion of the plugin installation, the installer will direct you to the following page, where you must create an Admin user and click the “Save and Continue” button.

Create First Admin

The default URL for your Jenkin’s instance will be set in the following step. Clicking ‘Save and Finish’ is all that is necessary.

Instance Configuration

Finally, to access Jenkins, click the ‘Start using Jenkins’ option.

Jenkins is ready

  • Understanding Jenkins Dashboard

Jenkins dashboard

1. New Item: Users can create new jobs or projects using the Jenkins dashboard’s “New Item” option. In order to select task specifics, source code repositories, build triggers, and other information, users are guided via a wizard.

2. People: The “People” link takes you to a page that lists user data and permissions. Administrators have authority over security settings, job access, and user account management.

3. Manage Jenkins: Access to global configuration options and settings is provided through this link. Administrators can set up system preferences, manage worker nodes, and install and manage plugins.

4. Build History: The dashboard shows a history of earlier builds along with each build’s outcomes, including success, failure, and unstable condition. Users can look over previous build information and look into any problems that may have occurred.

5. My Views: “My Views” enables users to make custom work groupings based on filters or folders and is fully customisable. It aids users in concentrating on pertinent subgroups of jobs.

Creating Your First Jenkins Job

Defining a Basic Jenkins Job

1. Create New Item:
Click “New Item” in the upper left-hand corner of your Jenkins dashboard.

Jenkins

2. Insert “Item details” inside.

Give the item you want to create a name. For this demo, we’ll utilise the “Build-myproject” message.
Choose the Freestyle project.
Select “OK”

Details

3. Enter details regarding the project.

project details

Configuring Source Code Management (SCM)

  1. Type the repository URL inside

Enter the URL for your repository under Source Code Management. My repository for our software can be found at https://github.com/dutta249/crate (Please use your own repo here as this one is a private repo)

Note: Ignore the error message as of now, if you are entering private repositories URL

Repositories

2. Credentials to Add (Optional)

Add Credentials of my repo (Not needed in case of public repository)

(1) Click on Add and then click on Jenkins

add jenkins

(2) Click the Add button after entering your git username and password and filling up the other fields as shown in the screenshot.

add user & password

(3) Your credentials will then be listed in the credentials section and then choose the newly created credentials.

credientials

Setting Up Build Triggers

Choose a GitHub hook trigger for GITScm polling to have the pipeline execute automatically whenever there is a change to the code in your repository.

Build Triggers

Building and Testing Your Project
It’s time to begin compiling the code, since all the information has been provided. To build the code when you want, modify the options in the build section.

  1. Click “Add build step” to start.
  2. Click on Execute Shell a

Build steps

3.  Here, I have added the node commands to compile my code. Please use your project command to compile your code.

node commands

4. Then click on apply and save.

apply

5. Build the source code
To build the source code, click the Build Now button on the left side of the main screen.

permalinks

6. After choosing Build Now, you can view the status of the last build you ran in the Build History section.

build history

Analyzing Build Results and Console Output

Then, after clicking the build number To view the status of the build you ran, click on the console output link. If you followed the configuration as shown in the Jenkins create build job example below, it should display a success message.

console Output

Jenkins and Continuous Delivery (CD)

  • Extending CI to CD
  1. Create New Item:
    Click “New Item” in the upper left-hand corner of your Jenkins dashboard.

jenkins

2. Insert “Item details” inside.

  • Give the item you want to create a name. For this demo, we’ll utilise the “Deploy-myproject” message.
  • Choose the Freestyle project.
  • Select “OK”

CI/CD with Jenkins

3.Enter details regarding the project.

General details

4. Click on Execute Shell.

Execute shell

5. Here, I have added the pm2 commands to deploy my code. Please use your project command to deploy your code

My Deploy Commands

cd /home/ubuntu/jenkins/workspace/Build-myproject/code/web

echo ‘#!/bin/bash
PROCESS_NAME=”myproject”
if pm2 describe $PROCESS_NAME &>/dev/null; then
pm2 restart $PROCESS_NAME
echo “Process already running. Restarting it…”
else
pm2 start ecosystem.js –name “$PROCESS_NAME”
echo “Process not found. Starting a new process…”
fi’ > pm2_process_management.sh

chmod +x pm2_process_management.sh
bash pm2_process_management.sh

Deploy code

6. Then click on apply and save.

save

Automating Deployment with Jenkins

Deployment goes properly if all previous steps are followed.

Console

Setting Up Continuous Delivery Pipelines

In our software development workflow, a Jenkins pipeline is a set of interconnected events or tasks that produce continuous delivery. Here, we will establish a pipeline job that will call the build and deploy job we just generated previously. Let’s have a look at the pipeline job’s Groovy script.

Jenkinsfile

pipeline {
agent any
stages {
stage(‘Build’) {
steps {
build job: “Build-myproject”, wait: true
}
}
stage(‘Deploy’) {
steps {
build job: “Deploy-myproject”, wait: true
}
}
}
}

In the pipeline job, the above script has two stages: build and deploy. The first stage will internally trigger the Build-myproject job, while the second stage will internally trigger the Deploy-myproject task.

Let’s now use the above Groovy script to construct a pipeline job:

In the Jenkins dashboard, click New Item.
Change the “Job Name” to Myproject-pipeline.
Choose “Job Type” as “Pipeline Project.”
Save the job after adding the Groovy script as described previously.

Pipeline

A Jenkins pipeline task can now trigger a freestyle job.

Pipeline project

In the above output, we can see that both stages were successfully built, and deploy was executed.