CI/CD with Jenkins: A Step-by-Step Guide
๐ CI/CD with Jenkins: A Step-by-Step Guide
๐ What is Jenkins?
Jenkins is an open-source automation server used to build, test, and deploy software. It's one of the most popular tools for implementing Continuous Integration and Continuous Deployment (CI/CD).
๐งฐ Pre-requisites
Basic knowledge of Git and programming
A working application (e.g., a Node.js, Java, or .NET app)
A GitHub or GitLab repository
Java (JDK) installed
Internet access for installing Jenkins plugins
๐งฉ Step-by-Step Jenkins Setup for CI/CD
๐ ️ 1. Install Jenkins
On Ubuntu:
bash
Copy
Edit
sudo apt update
sudo apt install openjdk-11-jdk
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
Access Jenkins at:
http://localhost:8080
๐ 2. Unlock Jenkins
Open browser: http://localhost:8080
Enter the admin password (found at /var/lib/jenkins/secrets/initialAdminPassword)
Choose Install Suggested Plugins
Create an Admin user
๐ฆ 3. Install Required Plugins
Install these Jenkins plugins:
Git
GitHub Integration
Pipeline
Blue Ocean (nice UI for pipelines)
SSH Agent (for remote deployment)
Docker (if using Dockerized builds)
๐งฑ 4. Create a New Job (Freestyle or Pipeline)
Option A: Freestyle Project
Go to New Item
Name your job (e.g., my-app-build)
Choose Freestyle project
In Source Code Management, connect your Git repo
In Build Triggers, enable Poll SCM or Build when a change is pushed to GitHub
In Build, add your build commands:
bash
Copy
Edit
npm install
npm run test
npm run build
Option B: Pipeline Project
Create a new Pipeline job and add a Jenkinsfile to your repo:
groovy
Copy
Edit
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-username/your-repo.git'
}
}
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Run Tests') {
steps {
sh 'npm test'
}
}
stage('Build') {
steps {
sh 'npm run build'
}
}
stage('Deploy') {
steps {
// example: deploy to server or Docker
sh './deploy.sh'
}
}
}
}
๐ 5. Configure GitHub Webhook (Optional but Recommended)
To trigger Jenkins builds automatically on code push:
Go to your GitHub repo → Settings → Webhooks → Add webhook
Payload URL: http://your-jenkins-domain/github-webhook/
Content type: application/json
Select “Just the push event”
⚙️ 6. Run the Pipeline
Click Build Now in Jenkins
Or push to your Git repo (if webhook configured)
Monitor the build stages, logs, and results
๐งช 7. Add Testing & Code Quality Tools (Optional)
Enhance your CI pipeline:
Unit Tests: Mocha, JUnit, NUnit, etc.
Linting: ESLint, SonarQube, StyleCop
Static Analysis: SonarQube or CodeQL
๐ข 8. Deployment Options
Depending on your project, you can:
Deploy to a Linux server via SSH
Push Docker images to DockerHub
Deploy to cloud services like Azure App Service, AWS EC2, or Heroku
Example SSH deployment script:
bash
Copy
Edit
scp -i your-key.pem dist/* user@yourserver:/var/www/html/
✅ Benefits of Jenkins CI/CD
Automates tedious tasks
Reduces human error
Faster feedback and deployment
Supports microservices and monoliths
Customizable and extensible
๐ Example Folder Structure with Jenkinsfile
bash
Copy
Edit
my-app/
├── src/
├── test/
├── Jenkinsfile
├── package.json
├── deploy.sh
๐ง Tips for Real-World Projects
Always include rollback scripts or backup options.
Keep secrets out of Jenkinsfiles – use Credentials or secret managers.
Use agent labels for different build environments (e.g., Docker, Linux, Windows).
Integrate with Slack or Email for notifications.
Learn DevOps Course in Hyderabad
Read More
Building Your First CI/CD Pipeline
Infrastructure as Code: What and How?
Integrating GitHub Actions into Your Workflow
Comments
Post a Comment