update: jenkinsfile code for multibranch pipeline
This commit is contained in:
parent
513a72f4fd
commit
f0f871369f
|
|
@ -21,7 +21,7 @@ pipeline {
|
||||||
stage('Install yq') {
|
stage('Install yq') {
|
||||||
steps {
|
steps {
|
||||||
sh '''
|
sh '''
|
||||||
if ! command -v yq &> /dev/null; then
|
if ! [ -x /usr/local/bin/yq ]; then
|
||||||
echo "Installing yq..."
|
echo "Installing yq..."
|
||||||
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
|
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
|
||||||
chmod +x /usr/local/bin/yq
|
chmod +x /usr/local/bin/yq
|
||||||
|
|
@ -34,11 +34,13 @@ pipeline {
|
||||||
stage('Build and Push Docker Images') {
|
stage('Build and Push Docker Images') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
def tag = "build-${env.BUILD_NUMBER}"
|
def gitBranch = env.BRANCH_NAME ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
|
||||||
echo "Building and pushing images with tag: ${tag}"
|
def tag = "${gitBranch}-build-${env.BUILD_NUMBER}"
|
||||||
|
echo "🛠 Building and pushing Docker 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')]) {
|
||||||
sh """
|
sh """
|
||||||
|
set -e
|
||||||
echo "\$DOCKER_PASS" | docker login -u "\$DOCKER_USER" --password-stdin
|
echo "\$DOCKER_PASS" | docker login -u "\$DOCKER_USER" --password-stdin
|
||||||
|
|
||||||
docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./employee-be
|
docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./employee-be
|
||||||
|
|
@ -60,40 +62,54 @@ pipeline {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
def envList = ["dev", "stag", "prod"]
|
def envList = ["dev", "stag", "prod"]
|
||||||
|
def parallelStages = [:]
|
||||||
|
|
||||||
|
envList.each { overlayEnv ->
|
||||||
|
parallelStages[overlayEnv] = {
|
||||||
|
echo "Starting manifest update for environment: ${overlayEnv}"
|
||||||
|
|
||||||
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')]) {
|
||||||
envList.each { overlayEnv ->
|
|
||||||
echo "🔹 Updating manifest for environment: ${overlayEnv}"
|
|
||||||
|
|
||||||
sh """
|
sh """
|
||||||
echo "Cloning branch ${overlayEnv}..."
|
set -e
|
||||||
rm -rf ${MANIFEST_DIR}
|
echo "Cloning manifest branch: ${overlayEnv}"
|
||||||
git clone --single-branch --branch ${overlayEnv} https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} || \
|
rm -rf ${MANIFEST_DIR}-${overlayEnv}
|
||||||
(git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} && cd ${MANIFEST_DIR} && git checkout -b ${overlayEnv})
|
git clone --single-branch --branch ${overlayEnv} https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}-${overlayEnv} || \
|
||||||
|
(git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}-${overlayEnv} && cd ${MANIFEST_DIR}-${overlayEnv} && git checkout -b ${overlayEnv})
|
||||||
"""
|
"""
|
||||||
|
|
||||||
dir(env.MANIFEST_DIR) {
|
dir("${MANIFEST_DIR}-${overlayEnv}") {
|
||||||
sh """
|
sh """
|
||||||
|
set -e
|
||||||
|
echo "Cleaning up overlays folder to only keep ${overlayEnv}"
|
||||||
|
find overlays -mindepth 1 -maxdepth 1 -type d ! -name "${overlayEnv}" -exec rm -rf {} +
|
||||||
|
|
||||||
echo "Updating image tags for ${overlayEnv}..."
|
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 == "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
|
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 config user.email "jenkins@automation.local"
|
||||||
git add overlays/${overlayEnv}/
|
git config user.name "Jenkins CI"
|
||||||
|
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 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}
|
echo "Pushing updates to branch ${overlayEnv}"
|
||||||
|
git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv} || echo "No push needed for ${overlayEnv}"
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
echo "Finished updating ${overlayEnv}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parallel parallelStages
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('ArgoCD Sync (optional)') {
|
stage('ArgoCD Sync (optional)') {
|
||||||
steps {
|
steps {
|
||||||
echo "If ArgoCD auto-sync is enabled, updates will deploy automatically."
|
echo "ℹIf ArgoCD auto-sync is enabled, updates will deploy automatically."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue