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

CI/CD Pipeline

Infrastructure as Code: What and How?

Integrating GitHub Actions into Your Workflow

Visit Our IHub Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Installing Tosca: Step-by-Step Guide for Beginners

Why Data Science Course?