What is Jenkins?
Jenkins is a Java-based open-source automation platform with plugins designed for continuous integration or continuous delivery. It is used to continually create and test software projects, making it easier for developers and DevOps engineers to integrate changes to the project and for consumers to get a new build. Plugins are the primary means of enhancing the functionality of a Jenkins environment to suit organization or user-specific needs.
Master-Slave Architecture of Jenkins
Jenkins uses master-slave architecture. The Jenkins master node distributes its workloads to the slave nodes, which work based on requests received from the Jenkins master.
Features of Jenkins
Jenkins offers many attractive features for developers:
Easy Installation: Jenkins is a platform-agnostic, self-contained Java-based program, ready to run with packages for Windows, Mac OS, and Unix-like operating systems.
Easy Configuration: Jenkins is easily set up and configured using its web interface, featuring error checks and a built-in help function.
Available Plugins: There are hundreds of plugins available in the Update Center, integrating with every tool in the CI and CD toolchain.
Extensible: Jenkins can be extended by means of its plugin architecture, providing nearly endless possibilities for what it can do.
Easy Distribution: Jenkins can easily distribute work across multiple machines for faster builds, tests, and deployments across multiple platforms.
Free Open Source: Jenkins is an open-source resource backed by heavy community support.
What is Pipeline in Jenkins?
A pipeline is a concept that introduces a series of events or tasks that are connected in a sequence to make quick software releases. For example, there is a task with five different stages, and each stage has several steps. All the steps in phase one must be completed to mark the latter stage as complete.
The Pipeline is responsible for building codes, running tests, and deploying new software versions. It executes the job in a defined manner by first coding it and then structuring it inside several blocks that may include several steps or tasks.
What is CI/CD in Jenkins?
CI/CD stands for Continuous Integration/Continuous Delivery (or Continuous Deployment), a set of practices and methodologies used in software development to achieve rapid and reliable delivery of software applications.
Continuous Integration (CI) is a practice that integrates code changes from multiple developers into a shared repository. It can automatically trigger builds and tests whenever changes are pushed to the repository, enabling quick feedback on code quality and preventing integration issues. It doesn't just eliminate bugs; it helps find and remove them quickly.
Continuous Delivery (CD) is the phase where the changes are made in the code before deploying. Jenkins enables the continuous delivery of software by automating the deployment process. It can deploy applications to various environments, such as development, staging, and production, based on predefined configurations. This helps ensure consistent and reliable deployments.
Continuous Deployment automates the release process, deploying changes to production automatically once tests pass, while Continuous Delivery focuses on ensuring software is always release-ready with manual approval.
Use Cases of Jenkins
Automated Testing: Jenkins can integrate with different testing frameworks, allowing automated tests to be executed as part of the build process. It helps identify bugs and issues early in the development cycle, ensuring the quality of the software.
Deployment Orchestration: Jenkins can be used to automate the deployment of applications to different servers and environments. It streamlines the process of deploying and managing software across multiple servers or cloud platforms.
Scheduled Jobs and Batch Processing: Jenkins can be configured to perform routine tasks at scheduled intervals, such as running backups, generating reports, or executing batch jobs. This helps automate repetitive tasks and frees up developers’ time for more critical activities.
What is a Project in Jenkins?
Jenkins uses projects (also known as "jobs") to perform its work. Projects are defined and run by Jenkins users. Jenkins offers several different types of projects, including:
Pipeline
Multibranch Pipeline
Organization folders
Freestyle
Multi-configuration (matrix)
Maven
External job
Different Types of Jenkins Projects:
Freestyle Project: This is the most basic project type in Jenkins. It provides a lot of flexibility and allows users to define build steps and configurations based on their specific requirements. It is suitable for simple or custom-build processes.
Pipeline Project: Jenkins Pipeline is a powerful and extensible way to define the build, test, and deployment workflows as code. It uses a domain-specific language (DSL) or a declarative syntax to define the entire build pipeline, including stages, steps, and conditions. Pipeline projects are highly versatile and recommended for complex build processes and CD workflows.
Multibranch Pipeline: This type of project is useful when you have multiple branches in your source code repository, and you want to build and test each branch separately. Jenkins automatically detects new branches and creates build pipelines for them, providing visibility into the status of different branches.
Benefits and Use Cases of Each Project Type
Freestyle Project:
Flexibility: Freestyle projects provide the highest level of flexibility in Jenkins. You have full control over the configuration and build steps, allowing you to customize the project to meet your specific requirements.
Easy setup: Freestyle projects are relatively simple to set up, making them ideal for small projects or quick automation tasks.
User-friendly interface: The user interface for configuring freestyle projects is intuitive and straightforward, making it easy for beginners to get started.
Pipeline Project:
Continuous Delivery as code: Jenkins Pipeline allows you to define your entire build, test, and deployment workflows as code. This provides several benefits:
Version control: Pipelines can be stored in a version control system, enabling easy collaboration, review, and rollback of changes.
Reusability: Pipeline code can be shared and reused across multiple projects, ensuring consistency and reducing duplication.
Scalability: Pipelines are highly scalable and can handle complex workflows with multiple stages, parallel execution, and conditional logic.
Visualization and monitoring: Pipeline projects provide a visual representation of the entire workflow, making it easy to track the progress of builds and deployments. Jenkins provides detailed logs and reports, helping identify issues and bottlenecks in the pipeline.
Multibranch Pipeline:
Independent branch testing: With multibranch pipelines, each branch in your source code repository can have its own build and test pipeline. This allows for independent testing and validation of changes made in different branches, ensuring branch-specific issues are caught early.
Automatic branch detection: Jenkins automatically detects new branches and creates build pipelines for them, reducing the manual effort required to set up pipelines for each branch.
Streamlined management: Multibranch pipelines provide a consolidated view of the build status across all branches, making it easy to monitor and manage multiple branches simultaneously.
Setting up Jenkins
Launch an EC2 instance based on Ubuntu and connect to it.
Prerequisites
Minimum hardware requirements:
256 MB of RAM
1 GB of drive space (although 10 GB is a recommended minimum if running Jenkins as a Docker container)
Jenkins requires Java 11 or 17 since Jenkins 2.357 and LTS 2.361.1.
Installing Jenkins
Update Packages: Run the following command to update the package lists for upgrades and new installations:
sudo apt-get update
Install Java: Jenkins requires Java to run. Install Java using the following command:
sudo apt install openjdk-11-jre
Check whether Java is installed:
java --version
Add Jenkins Repository Key: Add the Jenkins repository key to the system using the following command:
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
Add Jenkins Repository: Add the Jenkins repository to the package sources:
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
Update Package Lists and Install Jenkins: Update the package lists and install Jenkins:
sudo apt-get update sudo apt-get install jenkins
Start Jenkins: The Jenkins service should start automatically after installation. If it's not running, start it with the following command:
sudo systemctl start jenkins
Check Jenkins Status: Check whether Jenkins is active:
systemctl status jenkins
Open Port 8080: Jenkins runs on port 8080. Add this port to the security group on your instance.
Access Jenkins Web Interface: Open a web browser and enter the public IP address or DNS name of your EC2 instance followed by the Jenkins port (e.g., http://<your_public_ip>:8080). You should see the Jenkins setup wizard.
Find Admin Password: You can find the Admin password in the location given by Jenkins.
Customize Jenkins: After entering your password, you can now customize Jenkins by installing suggested plugins and creating the first admin user.
Congratulations! You have successfully installed Jenkins on your host and connected to the Jenkins web server.
Task 1: Create a freestyle pipeline to print "Hello World!"
- Click on "New Item" to create a new Jenkins job.
Enter the name for the job "HelloWorldPipeline" and select "Freestyle project", then click "OK".
In the configuration page that appears, scroll down to the "Build" section. Click on the "Add build step" dropdown and select "Execute shell" as the host machine's OS is Linux.
Once you select Execute shell, the Execute shell window opens. In the command section, enter the following:
Click on Save, after entering the command to save the pipeline configuration.
Run the pipeline, to see the output in the console log.
To run the pipeline, go to your Pipeline dashboard and click on Build Now.
View the output in the Console output.
Conclusion
In conclusion, Jenkins
and pipelines
play crucial roles in modern software development, enabling efficient and automated workflows.
Jenkins
, as a widely-used open-source automation server, provides a robust platform for continuous integration and continuous delivery (CI/CD). The flexibility and extensibility of Jenkins make it a valuable tool for organizations aiming to achieve agility and automation in their software development lifecycle.
Pipelines
in Jenkins serve as a structured and programmable way to define, orchestrate, and automate the software delivery process.
Together, Jenkins and pipelines
empower development teams to streamline their build, test, and deployment processes, fostering faster and more reliable software delivery.
~ Tushar Ranjan🙂