π Scenario (Real-World)
We will deploy an application using this flow:
Developer β Git β Jenkins β Build β Test β Package β Deploy
Below is Text Based Diagram.
Developer
|
v
Git Repository (GitHub / Bitbucket)
|
v
Jenkins (Pipeline)
|
βββ Build
βββ Test
βββ Package
βββ Deploy
|
v
Server / Cloud (EC2, VM, etc.)
- Jenkins Server
Jenkins installed on EC2 / VM / Local
Java installed
Jenkins running on port 8080
2 Required Jenkins Plugins
Install from Manage Jenkins β Plugins:
β Git
β Pipeline
β SSH Agent
β Credentials Binding
Step 2: Application Source Code
my-app/
βββ app.py
βββ requirements.txt
βββ Dockerfile
βββ Jenkinsfile
Step 3: Jenkins Credentials Setup
Add credentials in Jenkins
Manage Jenkins β Credentials
Git credentials
Server SSH key
Docker registry credentials (if needed)
Jenkins NEVER stores passwords in plain text.
Step 4: Create Jenkins Pipeline Job
Jenkins Dashboard β New Item
Select Pipeline
Name: my-app-pipeline
Pipeline Definition:
Pipeline script from SCM
SCM: Git
Repo URL
Branch: main
Script path: Jenkinsfile
Step 5: Jenkinsfile (Heart of Pipeline)
Complete End-to-End Jenkinsfile
pipeline {
agent any
environment {
APP_NAME = "my-app"
DEPLOY_SERVER = "ubuntu@10.0.0.10"
DEPLOY_PATH = "/var/www/my-app"
}
stages {
stage('Checkout Code') {
steps {
git branch: 'main',
url: 'https://github.com/your-org/my-app.git'
}
}
stage('Build') {
steps {
echo "Building application..."
sh 'pip install -r requirements.txt'
}
}
stage('Test') {
steps {
echo "Running tests..."
sh 'pytest || echo "Tests skipped"'
}
}
stage('Package') {
steps {
echo "Packaging application..."
sh 'tar -czf app.tar.gz *'
}
}
stage('Deploy') {
steps {
echo "Deploying application..."
sshagent(['server-ssh-key']) {
sh """
scp app.tar.gz ${DEPLOY_SERVER}:/tmp/
ssh ${DEPLOY_SERVER} '
mkdir -p ${DEPLOY_PATH} &&
tar -xzf /tmp/app.tar.gz -C ${DEPLOY_PATH} &&
systemctl restart my-app
'
"""
}
}
}
}
post {
success {
echo "β
Deployment Successful"
}
failure {
echo "β Deployment Failed"
}
}
}
Step 6: Deployment Server Setup
On target server (EC2 / VM):
sudo mkdir -p /var/www/my-app
sudo useradd my-app
sudo systemctl enable my-app
Example systemd service:
[Unit]
Description=My App Service
[Service]
ExecStart=/usr/bin/python3 /var/www/my-app/app.py
Restart=always
[Install]
WantedBy=multi-user.target
Step 7: Triggering the Pipeline
Option 1: Manual Trigger
Click Build Now
Option 2: Auto Trigger (Recommended)
Enable Git Webhook
Git Push β Jenkins Auto Build β Auto Deploy
Step 8: What Happens End-to-End (Interview Explanation)
π When developer pushes code:
Git notifies Jenkins
Jenkins pulls latest code
Jenkins builds application
Jenkins runs tests
Jenkins packages the app
Jenkins deploys to server
Application restarts
New version goes live π
Step 9: Common Interview Questions
β Why Jenkinsfile?
β Version controlled
β Reproducible
β Automated
β What if deployment fails?
β Pipeline stops
β Old version remains running
β Is Jenkins modifying infra?
β No, pipeline is read-only unless explicitly allowed
Step 10: Variations (Real Projects)
You can extend this pipeline with:
Docker build & push
Kubernetes deployment
AWS CodeDeploy
Blue-Green deployment
Rollback stage
Final Summary (One-Line)
Jenkins CI/CD pipeline automatically builds, tests, packages, and deploys application whenever code changes, ensuring fast, reliable, and repeatable deployments.
Top comments (0)