Update Jenkinsfile with optimized pipeline using shared repository and distinct image tags
Some checks failed
DevSecOps-Multibranch/pipeline/head There was a failure building this commit

This commit is contained in:
2025-11-30 15:37:41 +05:30
parent 74f4364fd4
commit e934074647

65
Jenkinsfile vendored
View File

@@ -1,14 +1,16 @@
pipeline { pipeline {
// 1. Run on your specific agent
agent { label 'jenkins-agent' } agent { label 'jenkins-agent' }
environment { environment {
REGISTRY_URL = 'registry.digitalocean.com/kongseng' REGISTRY_URL = 'registry.digitalocean.com/kongseng'
// 2. Dynamic Naming: Image tag includes Branch Name to prevent conflicts // FIX: Use ONE shared repository name
// Example: registry.../backend:Dev-42 or registry.../backend:main-42 REPO_NAME = 'devsecops-lab'
BACKEND_IMAGE = "${REGISTRY_URL}/devsecops-backend:${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
FRONTEND_IMAGE = "${REGISTRY_URL}/devsecops-frontend:${env.BRANCH_NAME}-${env.BUILD_NUMBER}" // Create distinct tags for backend and frontend
// Result: registry.../devsecops-lab:backend-Dev-1
BACKEND_TAG = "backend-${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
FRONTEND_TAG = "frontend-${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
} }
stages { stages {
@@ -20,7 +22,8 @@ pipeline {
stage('Install Dependencies') { stage('Install Dependencies') {
steps { steps {
echo "Installing dependencies for ${env.BRANCH_NAME} branch..." // Check if folders exist to avoid errors
sh 'ls -la'
dir('backend') { sh 'npm install' } dir('backend') { sh 'npm install' }
dir('frontend') { sh 'npm install' } dir('frontend') { sh 'npm install' }
} }
@@ -29,9 +32,10 @@ pipeline {
stage('Build Docker Images') { stage('Build Docker Images') {
steps { steps {
script { script {
echo "Building Images for branch: ${env.BRANCH_NAME}..." echo "Building Images..."
sh "docker build -t ${BACKEND_IMAGE} ./backend" // Build both images using the SAME Repo URL but DIFFERENT Tags
sh "docker build -t ${FRONTEND_IMAGE} ./frontend" sh "docker build -t ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG} ./backend"
sh "docker build -t ${REGISTRY_URL}/${REPO_NAME}:${FRONTEND_TAG} ./frontend"
} }
} }
} }
@@ -40,43 +44,26 @@ pipeline {
steps { steps {
script { script {
echo "Pushing images to DigitalOcean..." echo "Pushing images to DigitalOcean..."
sh "docker push ${BACKEND_IMAGE}" sh "docker push ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG}"
sh "docker push ${FRONTEND_IMAGE}" sh "docker push ${REGISTRY_URL}/${REPO_NAME}:${FRONTEND_TAG}"
} }
} }
} }
// --- DYNAMIC DEPLOYMENT ---
stage('Deploy') { stage('Deploy') {
steps { steps {
script { script {
// Define ports and names based on the branch def appPort = "3000"
def appPort = "3000" // Fallback // Unique container name for this branch
def containerName = "backend-app" def containerName = "app-${env.BRANCH_NAME}"
if (env.BRANCH_NAME == 'Dev') { if (env.BRANCH_NAME == 'Dev') { appPort = "3001" }
appPort = "3001" else if (env.BRANCH_NAME == 'Release') { appPort = "3002" }
containerName = "backend-dev" else if (env.BRANCH_NAME == 'main') { appPort = "3003" }
echo "Deploying to DEV Environment (Port 3001)"
}
else if (env.BRANCH_NAME == 'Release') {
appPort = "3002"
containerName = "backend-release"
echo "Deploying to STAGING Environment (Port 3002)"
}
else if (env.BRANCH_NAME == 'main') {
appPort = "3003"
containerName = "backend-prod"
echo "Deploying to PRODUCTION Environment (Port 3003)"
}
else {
// Logic for any future feature branches
appPort = "3004"
containerName = "backend-feature-${env.BRANCH_NAME}"
echo "Deploying Feature Branch"
}
// 1. Clean up old container echo "Deploying Backend to Port ${appPort}..."
// Clean up old container
try { try {
sh "docker stop ${containerName} || true" sh "docker stop ${containerName} || true"
sh "docker rm ${containerName} || true" sh "docker rm ${containerName} || true"
@@ -84,13 +71,13 @@ pipeline {
echo "No container to stop" echo "No container to stop"
} }
// 2. Run new container on the assigned port // Run the specific BACKEND tag
sh """ sh """
docker run -d \ docker run -d \
--name ${containerName} \ --name ${containerName} \
--restart always \ --restart always \
-p ${appPort}:3000 \ -p ${appPort}:3000 \
${BACKEND_IMAGE} ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG}
""" """
} }
} }