refactor: reorganize Jenkinsfile stages and improve environment variable handling
Some checks failed
bangararaju.kottedi.in/portfolio-admin/pipeline/head There was a failure building this commit

This commit is contained in:
Bangara Raju Kottedi 2026-03-06 07:16:33 +05:30
parent 17d2289974
commit 8de5d29a4d

185
Jenkinsfile vendored
View File

@ -1,113 +1,138 @@
pipeline { pipeline {
agent none ```
agent none
options { options {
buildDiscarder(logRotator(numToKeepStr: '10')) buildDiscarder(logRotator(numToKeepStr: '10'))
} disableConcurrentBuilds()
}
environment { stages {
DEPLOY_PATH = getDeployPath()
ENV_FILE_ID = getEnvFileId()
TARGET_URL = getHealthUrl()
}
stages { stage('Build') {
agent { label getAgentLabel() }
stage('Checkout') { stages {
agent { label getAgentLabel() }
steps { stage('Cleanup Workspace') {
checkout scm steps {
cleanWs()
}
} }
}
stage('Inject Environment File') { stage('Checkout') {
agent { label getAgentLabel() } steps {
steps { checkout scm
configFileProvider( }
[configFile( }
fileId: ENV_FILE_ID,
targetLocation: 'src/environments/environment.ts', stage('Inject Environment File') {
replaceTokens: true steps {
)] configFileProvider(
) { [configFile(
echo "Environment file injected for ${env.BRANCH_NAME}" fileId: getEnvFileId(),
targetLocation: 'src/environments/environment.ts',
replaceTokens: true
)]
) {
echo "Injected environment for ${env.BRANCH_NAME}"
}
}
}
stage('Install & Build') {
steps {
sh '''
npm ci
ng build --configuration production --base-href /admin/
'''
} }
} }
} }
}
stage('Build Angular') { // 🚨 Production Approval Gate
agent { label getAgentLabel() } stage('Production Approval') {
steps { when {
sh ''' branch 'prod'
npm install
ng build --configuration production --base-href /admin/
'''
}
} }
steps {
stage('Deploy') { input message: "Approve deployment to PRODUCTION?", ok: "Deploy"
agent { label getAgentLabel() }
steps {
sh '''
rm -rf $DEPLOY_PATH/*
cp -r dist/portfolio-admin/browser/* $DEPLOY_PATH/
sudo /usr/bin/systemctl reload nginx
'''
}
}
stage('Health Check') {
agent { label getAgentLabel() }
steps {
sh """
sleep 2
curl -f ${TARGET_URL}
"""
}
} }
} }
post { stage('Deploy & Verify') {
failure { agent { label getAgentLabel() }
echo "Deployment failed!"
} stages {
success {
echo "Deployment successful!" 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 -------- // -------- Helper Methods --------
//
def getAgentLabel() { def getAgentLabel() {
if (env.BRANCH_NAME == 'prod') { if (env.BRANCH_NAME == 'prod') {
return 'oracle-prod' return 'oracle-prod'
} else { } else {
return 'built-in' // for develop return 'built-in'
} }
} }
def getEnvFileId() { def getEnvFileId() {
if (env.BRANCH_NAME == 'prod') { if (env.BRANCH_NAME == 'prod') {
return 'admin-prod-properties' return 'admin-prod-properties'
} else { } else {
return 'admin-uat-properties' return 'admin-uat-properties'
} }
} }
def getDeployPath() { def getDeployPath() {
if (env.BRANCH_NAME == 'prod') { if (env.BRANCH_NAME == 'prod') {
return "/var/www/bangararaju.kottedi.in/admin" return "/var/www/bangararaju.kottedi.in/admin"
} else { } else {
return "/var/www/bangararaju.kottedi.in/admin" return "/var/www/bangararaju.kottedi.in/admin"
} }
} }
def getHealthUrl() { def getHealthUrl() {
if (env.BRANCH_NAME == 'prod') { if (env.BRANCH_NAME == 'prod') {
return "https://bangararaju.kottedi.in/admin" return "https://bangararaju.kottedi.in/admin"
} else { } else {
return "https://bangararaju-uat.kottedi.in/admin" return "https://bangararaju-uat.kottedi.in/admin"
} }
} }