pipeline { agent none options { buildDiscarder(logRotator(numToKeepStr: '10')) disableConcurrentBuilds() } stages { stage('Build & Test') { agent { label getAgentLabel() } environment { CHROME_BIN = "${getChromeBin()}" } stages { stage('Cleanup Workspace') { steps { cleanWs() } } 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' changeRequest() } } steps { sh 'npm run test -- --watch=false --browsers=ChromeHeadless' } } stage('Build Angular App') { when { anyOf { branch 'develop' branch 'prod' changeRequest() } } steps { sh 'ng build --configuration production --base-href /admin/' } } } } stage('Production Approval') { when { branch 'prod' } steps { input message: 'Approve deployment to PRODUCTION?', ok: 'Deploy' } } stage('Deploy & Verify') { when { anyOf { branch 'develop' branch 'prod' } } 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}" } failure { echo "Deployment failed for ${env.BRANCH_NAME}" } } } // // -------- Helper Methods -------- // def getAgentLabel() { return env.BRANCH_NAME == 'prod' ? 'oracle-prod' : 'built-in' } def getEnvFileId() { return env.BRANCH_NAME == 'prod' ? 'admin-prod-properties' : 'admin-uat-properties' } def getDeployPath() { return env.BRANCH_NAME == 'prod' ? "/var/www/bangararaju.kottedi.in/admin" : "/var/www/bangararaju.kottedi.in/admin" } def getHealthUrl() { return env.BRANCH_NAME == 'prod' ? "https://bangararaju.kottedi.in/admin" : "https://bangararaju-uat.kottedi.in/admin" } def getChromeBin() { return env.BRANCH_NAME == 'prod' ? "/snap/bin/chromium" : "/usr/bin/chromium" }