From e934074647fe5220282c6f7d0d6feaeffd98424e Mon Sep 17 00:00:00 2001 From: dev-1 Date: Sun, 30 Nov 2025 15:37:41 +0530 Subject: [PATCH] Update Jenkinsfile with optimized pipeline using shared repository and distinct image tags --- Jenkinsfile | 65 +++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a486af1..2569f8a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,14 +1,16 @@ pipeline { - // 1. Run on your specific agent agent { label 'jenkins-agent' } environment { REGISTRY_URL = 'registry.digitalocean.com/kongseng' - // 2. Dynamic Naming: Image tag includes Branch Name to prevent conflicts - // Example: registry.../backend:Dev-42 or registry.../backend:main-42 - BACKEND_IMAGE = "${REGISTRY_URL}/devsecops-backend:${env.BRANCH_NAME}-${env.BUILD_NUMBER}" - FRONTEND_IMAGE = "${REGISTRY_URL}/devsecops-frontend:${env.BRANCH_NAME}-${env.BUILD_NUMBER}" + // FIX: Use ONE shared repository name + REPO_NAME = 'devsecops-lab' + + // 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 { @@ -20,7 +22,8 @@ pipeline { stage('Install Dependencies') { 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('frontend') { sh 'npm install' } } @@ -29,9 +32,10 @@ pipeline { stage('Build Docker Images') { steps { script { - echo "Building Images for branch: ${env.BRANCH_NAME}..." - sh "docker build -t ${BACKEND_IMAGE} ./backend" - sh "docker build -t ${FRONTEND_IMAGE} ./frontend" + echo "Building Images..." + // Build both images using the SAME Repo URL but DIFFERENT Tags + 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 { script { echo "Pushing images to DigitalOcean..." - sh "docker push ${BACKEND_IMAGE}" - sh "docker push ${FRONTEND_IMAGE}" + sh "docker push ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG}" + sh "docker push ${REGISTRY_URL}/${REPO_NAME}:${FRONTEND_TAG}" } } } - // --- DYNAMIC DEPLOYMENT --- stage('Deploy') { steps { script { - // Define ports and names based on the branch - def appPort = "3000" // Fallback - def containerName = "backend-app" + def appPort = "3000" + // Unique container name for this branch + def containerName = "app-${env.BRANCH_NAME}" - if (env.BRANCH_NAME == 'Dev') { - appPort = "3001" - containerName = "backend-dev" - 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" - } + if (env.BRANCH_NAME == 'Dev') { appPort = "3001" } + else if (env.BRANCH_NAME == 'Release') { appPort = "3002" } + else if (env.BRANCH_NAME == 'main') { appPort = "3003" } - // 1. Clean up old container + echo "Deploying Backend to Port ${appPort}..." + + // Clean up old container try { sh "docker stop ${containerName} || true" sh "docker rm ${containerName} || true" @@ -84,13 +71,13 @@ pipeline { echo "No container to stop" } - // 2. Run new container on the assigned port + // Run the specific BACKEND tag sh """ docker run -d \ --name ${containerName} \ --restart always \ -p ${appPort}:3000 \ - ${BACKEND_IMAGE} + ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG} """ } }