From d3410c8ca190a8d560ec28091a4bf04eced9e605 Mon Sep 17 00:00:00 2001 From: giteaadmin Date: Sun, 30 Nov 2025 09:48:27 +0000 Subject: [PATCH] updated jenekinsfile2 more updates --- Jenkinsfile | 97 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 19 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 300ce53..275201a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,40 +1,99 @@ pipeline { - agent any + // 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}" + } + stages { stage('Checkout') { steps { checkout scm } } - + stage('Install Dependencies') { steps { - sh 'npm install' // or mvn install / pip install + echo "Installing dependencies for ${env.BRANCH_NAME} branch..." + dir('backend') { sh 'npm install' } + dir('frontend') { sh 'npm install' } } } - - stage('SAST - Semgrep') { + + stage('Build Docker Images') { steps { - sh 'semgrep scan --config auto --json > semgrep-report.json || true' - } - post { - always { - archiveArtifacts artifacts: 'semgrep-report.json' + script { + echo "Building Images for branch: ${env.BRANCH_NAME}..." + sh "docker build -t ${BACKEND_IMAGE} ./backend" + sh "docker build -t ${FRONTEND_IMAGE} ./frontend" } } } - stage('Unit Tests') { + stage('Push to Registry') { steps { - sh 'npm test || true' + script { + echo "Pushing images to DigitalOcean..." + sh "docker push ${BACKEND_IMAGE}" + sh "docker push ${FRONTEND_IMAGE}" + } + } + } + + // --- DYNAMIC DEPLOYMENT --- + stage('Deploy') { + steps { + script { + // Define ports and names based on the branch + def appPort = "3000" // Fallback + def containerName = "backend-app" + + 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" + } + + // 1. Clean up old container + try { + sh "docker stop ${containerName} || true" + sh "docker rm ${containerName} || true" + } catch (Exception e) { + echo "No container to stop" + } + + // 2. Run new container on the assigned port + sh """ + docker run -d \ + --name ${containerName} \ + --restart always \ + -p ${appPort}:3000 \ + ${BACKEND_IMAGE} + """ + } } } } - - post { - always { - echo "Dev pipeline finished" - } - } -} +} \ No newline at end of file