fix: params null-safe + post block node-safe

This commit is contained in:
devops 2026-09-16 12:55:24 +00:00
parent 45cfa9fc1a
commit b9a3e06ff2

19
Jenkinsfile vendored
View File

@ -9,7 +9,8 @@ pipeline {
environment {
REGISTRY = '51.195.4.170:5000'
IMAGE_NAME = 'swim/demo-app'
IMAGE_TAG = "${params.ROLLBACK_TAG?.trim() ? params.ROLLBACK_TAG : env.BUILD_NUMBER}"
// Elvis chain: params maujood na ho to bhi NPE nahi
IMAGE_TAG = "${(params.ROLLBACK_TAG ?: '').trim() ?: env.BUILD_NUMBER}"
// Jenkins container ke andar workspace /var/jenkins_home/... hai,
// par docker daemon HOST par chalta hai jahan wahi folder
// /opt/devops/data/jenkins/... par hai. -v ke liye HOST ka path chahiye.
@ -32,7 +33,7 @@ pipeline {
}
stage('Test') {
when { expression { !params.ROLLBACK_TAG?.trim() } }
when { expression { !(params.ROLLBACK_TAG ?: '').trim() } }
steps {
sh '''
# container ka path -> host ka path badlo
@ -49,7 +50,7 @@ pipeline {
}
stage('Build image') {
when { expression { !params.ROLLBACK_TAG?.trim() } }
when { expression { !(params.ROLLBACK_TAG ?: '').trim() } }
steps {
sh '''
docker build \
@ -62,7 +63,7 @@ pipeline {
}
stage('Smoke test image') {
when { expression { !params.ROLLBACK_TAG?.trim() } }
when { expression { !(params.ROLLBACK_TAG ?: '').trim() } }
steps {
sh '''
CID=$(docker run -d -P ${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG})
@ -79,7 +80,7 @@ assert r['version'] == '${IMAGE_TAG}', 'version galat!'
}
stage('Push to registry') {
when { expression { !params.ROLLBACK_TAG?.trim() } }
when { expression { !(params.ROLLBACK_TAG ?: '').trim() } }
steps {
withCredentials([usernamePassword(credentialsId: 'registry-creds',
usernameVariable: 'REG_USER',
@ -112,8 +113,16 @@ assert r['version'] == '${IMAGE_TAG}', 'version galat!'
post {
always {
script {
// Agar pipeline node milne se PEHLE fail ho jaye, to yahan
// node context nahi hota - sh/cleanWs crash kar dete hain.
if (env.WORKSPACE) {
sh 'docker image prune -f --filter "until=24h" || true'
cleanWs()
} else {
echo 'Node context nahi mila - cleanup skip'
}
}
}
success { echo "BUILD ${env.BUILD_NUMBER} SAFAL -> ${env.REGISTRY}/${env.IMAGE_NAME}:${env.IMAGE_TAG}" }
failure { echo "BUILD ${env.BUILD_NUMBER} FAIL" }