Jenkins Basics: A Beginner’s Guide

Sandhya BabuSandhya Babu
8 min read

Introduction to Jenkins

What is Jenkins?

  • Jenkins is a Java-based open-source automation tool for automating DevOps tasks such as code building, testing, and deployment.

  • It’s widely used for continuous integration (CI) and continuous delivery (CD).

  • Jenkins is highly extensible and relies on plugins to integrate with other tools, add features, and enhance functionality.

Why Jenkins in DevOps?
Jenkins streamlines the CI/CD pipeline, allowing DevOps teams to integrate code frequently, detect issues early, and deliver software faster.

Key Features of Jenkins: "What can Jenkins do?"

  1. Easy Installation: Jenkins is platform-independent and easy to install.

  2. Extensible: Supports over 1,800 plugins for integrating various DevOps tools.

  3. Distributed Builds: Jenkins can distribute work across multiple machines for faster execution.

  4. User-Friendly Interface: Provides a web-based interface for easy configuration and monitoring.

  5. Scalable: Can be extended to large projects and teams.

  6. Free and Open Source: No cost with strong community support.

How does Jenkins work?

Jenkins is like a control center that pulls code from your repository (like GitHub), runs builds, tests the code, and deploys it—all without human intervention. Here’s how it works in a simple flow:

  1. Developer Pushes Code: A developer pushes new code to a repository (e.g., Git).

  2. Jenkins Detects Change: Jenkins automatically detects the change (via a webhook or scheduled check).

  3. Jenkins Builds the Code: It compiles the code, checks for errors, and packages it if everything is fine.

  4. Jenkins Tests the Code: Jenkins runs automated tests to ensure the new code doesn’t break anything.

  5. Jenkins Deploys the Code: If the tests pass, Jenkins deploys the code to a staging or production environment.

Key Concepts in Jenkins:

  • Job/Project:
    A task or automation process Jenkins runs, like building code, running tests, or deploying an application.

  • Build:
    The process of compiling and testing code. A build can be triggered manually, automatically, or on a schedule.

  • Pipeline:
    A series of automated steps (stages) to build, test, and deploy code. Pipelines are defined using a Jenkinsfile.

  • Jenkinsfile:
    A text file that defines a pipeline as code. It can be stored in the project’s source code repository.

  • Node/Agent:
    A machine where Jenkins runs tasks. Jenkins can have multiple agents to distribute workloads.

  • Master/Controller:
    The central server controls job scheduling, manages agents, and monitors builds.

  • Plugins:
    Extensions that add extra features or integrations to Jenkins, allowing it to work with different tools and technologies.

  • Workspace:
    A directory on the agent where Jenkins checks out the code and runs builds.

  • SCM (Source Code Management):
    Jenkins integrates with SCM tools (e.g., Git, SVN) to pull code for builds.

  • Triggers:
    Rules that define when to start a build, like after a code commit, at a scheduled time, or after another build finishes.

Jenkins Architecture

Jenkins uses a master-slave architecture.

  • Master: Handles scheduling, monitoring builds, and delegating tasks to agents.

  • Agents (slaves): Execute tasks assigned by the master.

    • It is useful for parallel execution across different environments.

    • Efficient for workload distribution and faster processing.

Jenkins Pipeline

  • What is a Pipeline?
    A Jenkins pipeline is a set of automated processes for building, testing, and deploying code. It’s defined using either a declarative or scripted syntax.

  • Types of Pipelines:

    1. Declarative Pipeline: User-friendly, with a simpler syntax (preferred for beginners).

    2. Scripted Pipeline: More flexible and powerful but requires knowledge of Groovy scripting.

  • Stages and Steps:
    A pipeline consists of multiple stages (e.g., Build, Test, and deploy), each containing steps that define specific tasks.

What is CI/CD in Jenkins?

CI/CD in Jenkins refers to the automated processes of Continuous Integration (CI) and Continuous Delivery/Deployment (CD):

  • Continuous Integration (CI):
    CI is the practice of automatically integrating code changes from multiple developers into a shared repository several times a day. Jenkins automates this by running tests and building the code after every commit, ensuring that new changes don’t break the existing codebase.

  • Continuous Delivery (CD):
    CD ensures that the code is automatically tested and ready for deployment to production at any time. Jenkins automates packaging and preparing the application for release after successful builds and tests.

  • Continuous Deployment (CD):
    This takes it a step further by automatically deploying code to production once it passes all tests, without manual intervention.

Jenkins makes CI/CD seamless by automating the entire workflow, from integrating code to deploying it in a live environment.

Use Cases of Jenkins: "How you use Jenkins in real-life scenarios"

  • CI/CD Automation: Automates building, testing, and deploying code across different environments.

  • Code Integration: Merges code from multiple developers and runs tests to detect issues early.

  • Automated Testing: Triggers automated tests after each build to ensure code quality.

  • Continuous Monitoring: Monitors and reports the health of the application during the CI/CD pipeline.

  • Docker Integration: Builds and manages Docker containers as part of the deployment process.

  • Infrastructure as Code (IaC): Automates deployment and configuration management using tools like Ansible, Terraform, etc.

What is a project in Jenkins?

A project in Jenkins (also known as a job) is a collection of tasks that Jenkins will execute. It defines what needs to be done, like pulling code from a repository, running tests, building the application, and deploying it.

Types of projects in Jenkins:

  1. Freestyle Project: The simplest type, offering basic build and automation options. Suitable for straightforward tasks.

  2. Pipeline Project: Uses a Jenkinsfile to define a CI/CD pipeline as code. Ideal for complex workflows.

  3. Multi-Branch Pipeline: Automatically creates and runs pipelines for all branches in a repository.

Folder Project: Organizes multiple related projects/jobs into a single folder.

Jenkinsfile

  • What is a Jenkinsfile?
    It’s a text file that defines the CI/CD pipeline as code. Stored in the repository, it’s version-controlled alongside your code.

  • Key Sections:

    1. pipeline {}: Defines the pipeline structure.

    2. agent {}: Defines where the pipeline should run (e.g., any, none).

    3. stages {}: Contains multiple stages like Build, Test, and Deploy.

    4. steps {}: Defines individual tasks in each stage.

Setting Up Jenkins

Prerequisites:

  • Hardware Requirements:

    • RAM: Minimum 256 MB

    • Drive Space: 1 GB (10 GB recommended if using Docker)

  • Software Requirements:

    • Java Version: Java 11 or 17

Steps to Set Up Jenkins on an EC2 Instance:

  1. Launch EC2 Instance:

    • Choose an appropriate instance type (e.g., t2.micro for basic use).

    • Select an operating system (Ubuntu or Amazon Linux are common choices).

    • Connect to your EC2 instance via SSH.

Installing Jenkins::

Update Packages

 sudo apt-get update

Install Java:

sudo apt install openjdk-17-jdk

Check Java Installation:

 java --version

Add Jenkins Repository Key:

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

This command adds Jenkins' official key to your system so you can safely download and verify Jenkins packages:

  1. curl -fsSL <URL>: Download the Jenkins key file securely.

  2. sudo tee /usr/share/keyrings/jenkins-keyring.asc: saves the key file in a trusted location.

  3. > /dev/null: hides unnecessary output.

  4. https://pkg.jenkins.io/debian/jenkins.io-2023.key: This key is used to verify the authenticity of Jenkins packages when you download and install them on a Debian-based system (like Ubuntu). By adding this key to your system, you ensure that the packages you install from Jenkins are legitimate and haven’t been tampered with.

Add a Jenkins repository:

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

The command adds the Jenkins package repository to your system's sources list, allowing you to install and update Jenkins using apt, while ensuring package authenticity with a GPG key.

Update Package Lists and Install Jenkins:

 sudo apt-get update
 sudo apt-get install jenkins

Start Jenkins:

 sudo systemctl start jenkins

Check Jenkins Status:

 systemctl status jenkins

Open Port 8080: Ensure that port 8080 is open in your security group settings

Access the Jenkins Web Interface: Open a web browser and enter the public IP address or DNS name of your EC2 instance, followed by :8080 (http://<your_public_ip>:8080).

Admin Password: The admin password is located in the file path provided on Jenkins's getting started page.

Install the suggested plugins on the Customize Jenkins page. Once the plugins are installed, begin by creating the first admin user.

Congratulations! You have successfully installed Jenkins on your server and accessed the Jenkins web interface.

Task: Create a freestyle pipeline to Print "Hello World!"

Click New Item on your Jenkins home page, and enter a name for your job.

Enter the name "Hello-World" and select "Freestyle Project," then click "OK.".

Scroll down to the "Build" section. Click on "Add build step" and select "Execute shell."

Save and Build:

  • Click "Save" to save the configuration.

  • Click "Build Now" on the project page to run the job.

Verify the output:

Click on "Console Output" to see the result, where you should see "Hello World" printed.

Conclusion

In this post, we explored Jenkins, covering its features, real-life use cases, and how to set it up on an EC2 instance. We discussed key concepts like the Jenkins architecture, pipelines, and how Jenkins streamlines CI/CD processes. Additionally, we set up a simple freestyle project to print "Hello World" to get hands-on experience.

Stay tuned for more advanced topics and practical use cases of Jenkins in upcoming posts!

1
Subscribe to my newsletter

Read articles from Sandhya Babu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sandhya Babu
Sandhya Babu

🌟 Aspiring DevOps Engineer | Cloud & DevOps Enthusiast🌟 Hello! I’m Sandhya Babu, deeply passionate about DevOps and cloud technologies. Currently in my exploring phase, I’m learning something new every day, from tools like Jenkins, Docker, and Kubernetes to the concepts that drive modern tech infrastructures. I have hands-on experience with several Proof of Concept (POC) projects, where I've applied my skills in real-world scenarios. I love writing blogs about what I've learned and sharing my experiences with others, hoping to inspire and connect with fellow learners. With certifications in Azure DevOps and AWS SAA-C03, I’m actively seeking opportunities to apply my knowledge, contribute to exciting projects, and continue growing in the tech industry. Let’s connect and explore the world of DevOps together!