Skip to content
Open

Main #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:20-alpine as builder

WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY . .

# Stage 2 — Final image
FROM node:20-alpine

WORKDIR /app

COPY --from=builder /app .

EXPOSE 4000

CMD ["node", "server.js"]
104 changes: 104 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
pipeline {
agent any

environment {
SCANNER_HOME = tool 'SonarScanner'
DOCKER_IMAGE = 'flipkart-mern'
DOCKER_REGISTRY = 'your-docker-registry' // Replace with your registry URL
}

stages {
stage('Git Checkout') {
steps {
git url: 'https://github.com/Hamza844/flipkart-mern.git'

withCredentials([
file(credentialsId: 'backend-env', variable: 'BACKEND_ENV'),
file(credentialsId: 'frontend-env', variable: 'FRONTEND_ENV')
]) {
sh '''
echo "Injecting environment files..."
cp "$BACKEND_ENV" .env
cp "$FRONTEND_ENV" frontend/.env
'''
}
}
}

stage('SonarQube Scan') {
steps {
withSonarQubeEnv('My Sonar') {
withCredentials([string(credentialsId: 'sonar-token', variable: 'SONAR_TOKEN')]) {
sh '''
${SCANNER_HOME}/bin/sonar-scanner \
-Dsonar.projectKey=flipkart-mern \
-Dsonar.sources=. \
-Dsonar.host.url=http://34.202.228.82:9000 \
-Dsonar.login=$SONAR_TOKEN
'''
}
}
}
}

stage('Quality Gate') {
steps {
timeout(time: 2, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}

stage('Trivy FS Scan') {
steps {
sh 'trivy fs .'
}
}

stage('Docker Build Image') {
steps {
script {
docker.build("${DOCKER_IMAGE}:${env.BUILD_ID}")
}
}
}

stage('Trivy Scan Image') {
steps {
sh "trivy image --exit-code 0 --severity HIGH,CRITICAL ${DOCKER_IMAGE}:${env.BUILD_ID}"
}
}

stage('Docker Tag Image & Push Image') {
steps {
script {
docker.withRegistry('https://${DOCKER_REGISTRY}', 'docker-credentials') {
docker.image("${DOCKER_IMAGE}:${env.BUILD_ID}").push()
docker.image("${DOCKER_IMAGE}:${env.BUILD_ID}").push('latest')
}
}
}
}

stage('Verify Deployment') {
steps {
script {
// Example: Verify by checking running containers or making HTTP requests
sh "docker ps | grep ${DOCKER_IMAGE}"
// Or use curl to verify endpoint
// sh 'curl -sSf http://your-deployment-url/health'
}
}
}
}

post {
always {
echo '🧹 Cleaning up injected .env files...'
sh 'rm -f .env frontend/.env'

// Clean up Docker images to save disk space
sh "docker rmi ${DOCKER_IMAGE}:${env.BUILD_ID} || true"
}
}
}
31 changes: 31 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
mongo:
image: mongo
container_name: mongo
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db

backend:
build: .
container_name: backend
ports:
- "4000:4000"
depends_on:
- mongo
env_file:
- .env # <-- Jenkins injects this before build

frontend:
build: ./frontend
container_name: frontend
ports:
- "3000:3000"
stdin_open: true
tty: true
env_file:
- ./frontend/.env # <-- Jenkins injects this too

volumes:
mongo-data:
Loading