update: jenkinsfile code for multibranch pipeline
This commit is contained in:
parent
e55faa7b01
commit
513a72f4fd
|
|
@ -10,10 +10,6 @@ pipeline {
|
||||||
MANIFEST_DIR = "manifest"
|
MANIFEST_DIR = "manifest"
|
||||||
}
|
}
|
||||||
|
|
||||||
parameters {
|
|
||||||
string(name: 'IMAGE_TAG', defaultValue: '', description: 'Custom image tag (optional)')
|
|
||||||
}
|
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Prepare Workspace') {
|
stage('Prepare Workspace') {
|
||||||
steps {
|
steps {
|
||||||
|
|
@ -38,7 +34,7 @@ pipeline {
|
||||||
stage('Build and Push Docker Images') {
|
stage('Build and Push Docker Images') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
def tag = params.IMAGE_TAG ?: "build-${env.BUILD_NUMBER}"
|
def tag = "build-${env.BUILD_NUMBER}"
|
||||||
echo "Building and pushing images with tag: ${tag}"
|
echo "Building and pushing images with tag: ${tag}"
|
||||||
|
|
||||||
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
|
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
|
||||||
|
|
@ -60,43 +56,35 @@ pipeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Update Manifest for Current Branch') {
|
stage('Update All Manifest Branches') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
def currentBranch = env.BRANCH_NAME ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
|
def envList = ["dev", "stag", "prod"]
|
||||||
echo "🔹 Current branch detected: ${currentBranch}"
|
|
||||||
|
|
||||||
def envMap = [
|
|
||||||
"develop": "dev",
|
|
||||||
"staging": "stag",
|
|
||||||
"main": "prod"
|
|
||||||
]
|
|
||||||
def overlayEnv = envMap.get(currentBranch, currentBranch)
|
|
||||||
|
|
||||||
if (!["dev", "stag", "prod"].contains(overlayEnv)) {
|
|
||||||
error "Invalid branch/environment: ${overlayEnv}. Only dev, stag, or prod allowed."
|
|
||||||
}
|
|
||||||
|
|
||||||
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||||
sh """
|
envList.each { overlayEnv ->
|
||||||
echo "Cloning manifest repo..."
|
echo "🔹 Updating manifest for environment: ${overlayEnv}"
|
||||||
rm -rf ${MANIFEST_DIR}
|
|
||||||
git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}
|
|
||||||
"""
|
|
||||||
|
|
||||||
dir(env.MANIFEST_DIR) {
|
|
||||||
sh """
|
sh """
|
||||||
git checkout ${overlayEnv} || git checkout -b ${overlayEnv}
|
echo "Cloning branch ${overlayEnv}..."
|
||||||
|
rm -rf ${MANIFEST_DIR}
|
||||||
echo "Updating image tags inside overlays/${overlayEnv}/patch-deployment.yaml..."
|
git clone --single-branch --branch ${overlayEnv} https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} || \
|
||||||
yq e -i '(.spec.template.spec.containers[] | select(.name == "backend") | .image) = "${REGISTRY}/${APP_NAME}-be:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml
|
(git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} && cd ${MANIFEST_DIR} && git checkout -b ${overlayEnv})
|
||||||
yq e -i '(.spec.template.spec.containers[] | select(.name == "frontend") | .image) = "${REGISTRY}/${APP_NAME}-fe:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml
|
|
||||||
|
|
||||||
git add overlays/${overlayEnv}/patch-deployment.yaml
|
|
||||||
git commit -m "chore(${overlayEnv}): update image tags to ${overlayEnv}-${env.IMAGE_TAG_FINAL}" || echo "No changes to commit"
|
|
||||||
|
|
||||||
git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv}
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
dir(env.MANIFEST_DIR) {
|
||||||
|
sh """
|
||||||
|
echo "Updating image tags for ${overlayEnv}..."
|
||||||
|
yq e -i '(.spec.template.spec.containers[] | select(.name == "backend") | .image) = "${REGISTRY}/${APP_NAME}-be:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml
|
||||||
|
yq e -i '(.spec.template.spec.containers[] | select(.name == "frontend") | .image) = "${REGISTRY}/${APP_NAME}-fe:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml
|
||||||
|
|
||||||
|
git reset -- overlays/*/
|
||||||
|
git add overlays/${overlayEnv}/
|
||||||
|
git commit -m "chore(${overlayEnv}): update image tags to ${overlayEnv}-${env.IMAGE_TAG_FINAL}" || echo "No changes to commit"
|
||||||
|
|
||||||
|
git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv}
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +100,7 @@ pipeline {
|
||||||
|
|
||||||
post {
|
post {
|
||||||
success {
|
success {
|
||||||
echo "Pipeline completed successfully for branch: ${env.BRANCH_NAME}"
|
echo "Pipeline completed successfully for all branches (dev, stag, prod)"
|
||||||
}
|
}
|
||||||
failure {
|
failure {
|
||||||
echo "Pipeline failed. Check logs for details."
|
echo "Pipeline failed. Check logs for details."
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue