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

77
Jenkinsfile vendored
View File

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