![]() |
How to Set Up a CI/CD Pipeline Using Jenkins: A Complete Guide |
Setting up a CI/CD (Continuous Integration/Continuous Deployment) pipeline is a crucial step for modern software development, enabling faster releases and higher-quality code. Jenkins is a widely-used open-source automation server that simplifies the creation and management of CI/CD pipelines, making it an indispensable tool for modern development workflows. In this tutorial, we’ll walk you through the process of setting up a CI/CD pipeline using Jenkins, from installation to deployment.
1. Introduction to CI/CD and Jenkins
Continuous Integration (CI): This approach involves developers regularly committing their code to a shared repository. Each change triggers automated processes like building and testing, helping to identify and resolve issues early in the development cycle.
Continuous Deployment (CD): Automates the delivery of tested and validated code to production, reducing manual effort and speeding up the release cycle.
Jenkins serves as the backbone of this process by orchestrating tasks such as code fetching, building, testing, and deploying.
2. Prerequisites
To get started with Jenkins, you’ll need a few prerequisites:
- Java Development Kit (JDK): As Jenkins is built on Java, having the JDK installed on your system is essential for it to run.
- Source Code Management (SCM): A Git repository with your codebase.
- Build Tools: Tools like Maven, Gradle, or npm, depending on your application.
- Web Server/Cloud Platform: For deployment, such as Apache, Nginx, or AWS.
3. Installing Jenkins
Download Jenkins:
- Download the installer suited to your operating system from the official Jenkins website.
Install Jenkins:
- On Linux:
- On Windows:
- Run the installer and follow the setup wizard.
Start Jenkins:
- On Linux:
- On Windows:
- Jenkins starts automatically as a service.
Access Jenkins:
- Open your browser and navigate to
http://localhost:8080
. - Use the initial admin password located in
/var/lib/jenkins/secrets/initialAdminPassword
(Linux) or specified during installation (Windows).
4. Setting Up Jenkins
Install Plugins:
- When you launch Jenkins for the first time, it provides a step-by-step setup wizard to help you configure the essential settings and get your environment ready. One of the initial steps includes installing recommended plugins, which provide essential features and integrations for your pipeline.
Create an Admin User:
- Complete the setup by creating an admin username and password.
Configure Global Tools:
- Navigate to
Manage Jenkins > Global Tool Configuration
. - Configure JDK, Git, and your build tools (Maven/Gradle).
5. Configuring Your First Jenkins Pipeline
A pipeline in Jenkins defines the workflow of CI/CD tasks using a scripted or declarative syntax.
Create a New Pipeline:
- To define a pipeline in Jenkins, navigate to the dashboard, click on
New Item
, and select the "Pipeline" option. Give your pipeline a unique name and proceed to configure its stages.
Define Pipeline Script:
- Use the Jenkins Pipeline DSL (Domain-Specific Language) to write your pipeline.
- Example of a simple pipeline:
6. Integrating Source Control (Git)
Install Git Plugin:
- Navigate to
Manage Jenkins > Manage Plugins
and make sure the Git plugin is installed.
Link Your Repository:
- In the pipeline configuration, specify your Git repository:
7. Adding Build Steps
Build steps compile your code and package it into an executable or deployable artifact.
- Maven Example:
- Node.js Example:
8. Automating Testing
Automated tests ensure code quality and stability.
- Add a Testing Stage:
- View Test Results:
To visualize test results in Jenkins, just use the "JUnit Plugin"
9. Deploying Your Application
Deploying the application involves pushing the build to a server or a cloud environment.
Example Deployment Script:
Pipeline Deployment Stage:
Advanced Deployment:
- Use plugins like "AWS CodeDeploy" or "Kubernetes" for cloud-based deployment.
Setting up a CI/CD pipeline with Jenkins streamlines the software development process, enabling continuous integration, testing, and deployment. By following this guide, you now have a foundational pipeline that you can customize to suit your project's needs.
Experiment with Jenkins' vast ecosystem of plugins to enhance your pipeline, and keep iterating to make your development and deployment process seamless.