Day 22: Getting Started with Jenkins

Day 22: Getting Started with Jenkins

90DaysOfDevOps

·

5 min read

In this blog, we'll dive into what Jenkins is, and why it's important. Also, I will create a Jenkins freestyle project to understand the working of Jenkins.

But let us understand what is CI/CD first.

CI/CD

CI stands for Continuous Integration and CD stands for Continuous Delivery/Deployment.

Continuous integration (CI) helps developers merge their code changes back to a shared branch, more frequently—sometimes even daily

CD is a development practice that follows Continuous Integration by automating the process of testing, preparing, and packaging software for deployment. It ensures that code changes meeting quality criteria are consistently and automatically moved to staging environments for further testing and potential deployment to production.

Continuous Deployment is an extension of Continuous Delivery where the deployment process is fully automated.

There are several tools available for setting up and managing CI/CD pipelines:

  • Jenkins

  • GitLab

  • Travis CI

  • CircleCI

  • Bamboo

Jenkins is one of the most widely used, mainly as it is open source

Jenkins

Jenkins is an open-source automation server that helps developers automate repetitive tasks in their software development process. It allows teams to create automated pipelines for building, testing, and deploying code changes, making development more efficient and reliable. J enkins is based on Java programming language.

Key Benefits of Jenkins:

  1. Automation and Efficiency

  2. Continuous Integration and Continuous Delivery (CI/CD)

  3. Flexibility

  4. Scalability

  5. Monitoring and Insights

Jenkins achieves Continuous Integration with the help of plugins. Plugins allow the integration of various DevOps stages. If you want to integrate a particular tool, you need to install the plugins for that tool. For example Git, Maven 2 project, Amazon EC2, HTML publisher etc.

Jenkins Architecture

Jenkins is based on the Master-Slave architecture.

Master is the central server that coordinates and manages the overall Jenkins system. It is responsible for scheduling and assigning tasks, managing jobs, and maintaining the configuration.

Slave in Jenkins is a worker or agent node that performs tasks assigned to it by the master. Slaves are responsible for executing builds, running tests, and other automation tasks.

Installation of Jenkins

Installing Jenkins is a straightforward process, and you have a few options depending on your operating system and preferences. Here, I'll outline the steps for installing Jenkins on a common setup: Ubuntu Linux.

Step 1: Update System Packages Open a terminal window and run the following commands to update your package repositories and upgrade existing packages:

sudo apt update
sudo apt upgrade

Step 2: Install Java Jenkins requires Java to run. You can install OpenJDK using the following command:

sudo apt install openjdk-11-jdk

To check the version installed, use the following command:

java --version

Step 3: Add Jenkins Repository Add the Jenkins repository to your package sources list:

curl -fsSL https://pkg.jenkins.io/debian/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

Step 4: Install Jenkins Update your package list again and install Jenkins:

sudo apt-get update
sudo apt-get install jenkins

Step 5: Start Jenkins Jenkins is installed as a system service. Use the following commands to start and enable the service:

sudo systemctl start jenkins
sudo systemctl enable jenkins

Step 6: Access Jenkins Web Interface Jenkins runs on port 8080 by default. Open a web browser and navigate to:

http://your_server_ip_or_domain:8080

You'll be prompted to enter an initial admin password. Retrieve this password from your server using the following command:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy and paste the password into the web interface.

You will be led to the “Customize Jenkins” page. Select “Install suggested plugins”

Step 7: Complete Setup Follow the on-screen instructions to complete the Jenkins setup. You can choose to install suggested plugins or select specific plugins according to your needs. Once the installation is complete, you'll be prompted to create an admin user.

Step 8: Jenkins Installation Complete After setting up the admin user, you'll be redirected to the Jenkins dashboard. From here, you can start creating your pipelines and jobs.

That's it! You now have Jenkins installed and ready to automate your development workflows on your Ubuntu system.

Tasks:

Create a freestyle pipeline to print "Hello World!

Creating a freestyle pipeline in Jenkins involves setting up a job that performs a series of steps. In this case, you want to print "Hello World!" as a simple example. Here's how you can do it:

Step 1: Create a New Freestyle Job

  1. Click on "New Item" to create a new job.

  2. Enter a name for your job, such as "HelloWorldJob."

  3. Select "Freestyle project" and click "OK."

Step 2: Configure the Job

  1. In the job configuration page, you'll find various sections. Scroll down to the "Build" section.

  2. Click on "Add build step" and select "Execute shell" (or "Execute Windows batch command" if you're on Windows).

Step 3: Add Shell Command

For Linux/macOS:

echo "Hello World!"

Step 4: Save and Run

  1. Click "Save" to save the job configuration.

  2. On the job dashboard, click "Build Now" to trigger a build.

Jenkins will execute the shell command you provided in the build step. In this case, it will print "Hello World!" to the console output.

This is a simple example of creating a freestyle pipeline in Jenkins

What have you understood in Jenkins? Write a small article in your own words.

For me, Jenkins is like an automation tool where you want to automate the code build and deployment using CI/CD

Jenkins is based on Master-Slave architecture. Jenkins is installed ONLY on the Master node. There is no Jenkins on slave nodes. Java should be installed on all nodes.

To conclude, Jenkins is an open-source tool, that is helping developers to automate the SDLC and easily implement the CI/CD pipelines.