From 1eca7146809d882d958799e77de29236b7ea1c8a Mon Sep 17 00:00:00 2001 From: Bangara Raju Kottedi Date: Fri, 6 Mar 2026 08:03:03 +0530 Subject: [PATCH] refactor: enhance Jenkinsfile structure and improve stage conditions for better clarity and maintainability --- Jenkinsfile | 230 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 135 insertions(+), 95 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 440f3f0..64186b2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,102 +1,142 @@ pipeline { -agent none + agent none -options { - buildDiscarder(logRotator(numToKeepStr: '10')) - disableConcurrentBuilds() -} + options { + buildDiscarder(logRotator(numToKeepStr: '10')) + disableConcurrentBuilds() + } -stages { + stages { - stage('Build') { - agent { label getAgentLabel() } + stage('Build & Test') { + agent { label getAgentLabel() } - stages { + stages { - stage('Cleanup Workspace') { - steps { - cleanWs() - } - } - - stage('Checkout') { - steps { - checkout scm - } - } - - stage('Inject Environment File') { - steps { - configFileProvider( - [configFile( - fileId: getEnvFileId(), - targetLocation: 'src/environments/environment.ts', - replaceTokens: true - )] - ) { - echo "Injected environment for ${env.BRANCH_NAME}" + stage('Cleanup Workspace') { + steps { + cleanWs() } } - } - stage('Install & Build') { - steps { - sh ''' - npm ci - ng build --configuration production --base-href /admin/ - ''' + stage('Checkout') { + steps { + checkout scm + } } + + stage('Inject Environment File') { + when { + not { + anyOf { + branch pattern: "feature/.*", comparator: "REGEXP" + branch pattern: "bug/.*", comparator: "REGEXP" + } + } + } + steps { + configFileProvider( + [configFile( + fileId: getEnvFileId(), + targetLocation: 'src/environments/environment.ts', + replaceTokens: true + )] + ) { + echo "Injected environment for ${env.BRANCH_NAME}" + } + } + } + + stage('Install Dependencies') { + steps { + sh 'npm ci' + } + } + + stage('Run Tests') { + when { + anyOf { + branch pattern: "feature/.*", comparator: "REGEXP" + branch pattern: "bug/.*", comparator: "REGEXP" + branch 'develop' + branch 'prod' + } + } + steps { + sh 'npm run test -- --watch=false --browsers=ChromeHeadless' + } + } + + stage('Build Angular App') { + when { + anyOf { + branch 'develop' + branch 'prod' + } + } + steps { + sh ''' + ng build --configuration production --base-href /admin/ + ''' + } + } + } } - } - // 🚨 Production Approval Gate - stage('Production Approval') { - when { - branch 'prod' + stage('Production Approval') { + when { + branch 'prod' + } + steps { + input message: "Approve deployment to PRODUCTION?", ok: "Deploy" + } } - steps { - input message: "Approve deployment to PRODUCTION?", ok: "Deploy" - } - } - stage('Deploy & Verify') { - agent { label getAgentLabel() } + stage('Deploy & Verify') { - stages { - - stage('Deploy') { - steps { - sh """ - rsync -av --delete dist/portfolio-admin/browser/ ${getDeployPath()}/ - sudo /usr/bin/systemctl reload nginx - """ + when { + anyOf { + branch 'develop' + branch 'prod' } } - stage('Health Check') { - steps { - sh """ - sleep 3 - curl -f ${getHealthUrl()} - """ - } - } + agent { label getAgentLabel() } + stages { + + stage('Deploy') { + steps { + sh """ + rsync -av --delete dist/portfolio-admin/browser/ ${getDeployPath()}/ + sudo /usr/bin/systemctl reload nginx + """ + } + } + + stage('Health Check') { + steps { + sh """ + sleep 3 + curl -f ${getHealthUrl()} + """ + } + } + + } } } -} -post { - success { - echo "✅ Deployment successful for ${env.BRANCH_NAME}" + post { + success { + echo "Deployment successful for ${env.BRANCH_NAME}" + } + failure { + echo "Deployment failed for ${env.BRANCH_NAME}" + } } - failure { - echo "❌ Deployment failed for ${env.BRANCH_NAME}" - } -} - } // @@ -104,33 +144,33 @@ post { // def getAgentLabel() { -if (env.BRANCH_NAME == 'prod') { -return 'oracle-prod' -} else { -return 'built-in' -} + if (env.BRANCH_NAME == 'prod') { + return 'oracle-prod' + } else { + return 'built-in' + } } def getEnvFileId() { -if (env.BRANCH_NAME == 'prod') { -return 'admin-prod-properties' -} else { -return 'admin-uat-properties' -} + if (env.BRANCH_NAME == 'prod') { + return 'admin-prod-properties' + } else { + return 'admin-uat-properties' + } } def getDeployPath() { -if (env.BRANCH_NAME == 'prod') { -return "/var/www/bangararaju.kottedi.in/admin" -} else { -return "/var/www/bangararaju.kottedi.in/admin" -} + if (env.BRANCH_NAME == 'prod') { + return "/var/www/bangararaju.kottedi.in/admin" + } else { + return "/var/www/bangararaju.kottedi.in/admin" + } } def getHealthUrl() { -if (env.BRANCH_NAME == 'prod') { -return "https://bangararaju.kottedi.in/admin" -} else { -return "https://bangararaju-uat.kottedi.in/admin" -} -} + if (env.BRANCH_NAME == 'prod') { + return "https://bangararaju.kottedi.in/admin" + } else { + return "https://bangararaju-uat.kottedi.in/admin" + } +} \ No newline at end of file