fix: checkout application repo stage

This commit is contained in:
adelyaou 2025-10-22 15:26:59 +07:00
parent 0767b42851
commit 5d59eeb6ba
1 changed files with 39 additions and 31 deletions

70
Jenkinsfile vendored
View File

@ -40,35 +40,39 @@ pipeline {
stage('Checkout Application Repo') {
steps {
checkout scm
dir('app') {
checkout scm
}
}
}
stage('Build and Push Docker Image') {
steps {
script {
def tag = params.IMAGE_TAG ?: "build-${env.BUILD_NUMBER}"
echo "Building and pushing image with tag: ${tag}"
dir('app') {
script {
def tag = params.IMAGE_TAG ?: "build-${env.BUILD_NUMBER}"
echo "Building and pushing image with tag: ${tag}"
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh """
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./backend
docker build -t ${REGISTRY}/${APP_NAME}-fe:${tag} ./frontend
docker push ${REGISTRY}/${APP_NAME}-be:${tag}
docker push ${REGISTRY}/${APP_NAME}-fe:${tag}
docker logout
"""
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh """
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./backend
docker build -t ${REGISTRY}/${APP_NAME}-fe:${tag} ./frontend
docker push ${REGISTRY}/${APP_NAME}-be:${tag}
docker push ${REGISTRY}/${APP_NAME}-fe:${tag}
docker logout
"""
}
env.IMAGE_TAG_FINAL = tag
}
env.IMAGE_TAG_FINAL = tag
}
}
}
stage('Checkout Manifest Repo') {
steps {
script {
dir('manifest') {
checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH]],
userRemoteConfigs: [[url: env.MANIFEST_REPO, credentialsId: env.MANIFEST_CRED_ID]]
@ -79,29 +83,33 @@ pipeline {
stage('Update Image Tags in Manifests') {
steps {
script {
echo "Updating manifests to tag: ${env.IMAGE_TAG_FINAL}"
dir('manifest') {
script {
echo "Updating manifests to tag: ${env.IMAGE_TAG_FINAL}"
sh """
yq e -i '.images[] |= (.newTag = "${env.IMAGE_TAG_FINAL}")' base/kustomization.yaml || true
sh """
yq e -i '.images[] |= (.newTag = "${env.IMAGE_TAG_FINAL}")' base/kustomization.yaml || true
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-be:${env.IMAGE_TAG_FINAL}"' base/backend-deployment.yaml
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-fe:${env.IMAGE_TAG_FINAL}"' base/frontend-deployment.yaml
"""
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-be:${env.IMAGE_TAG_FINAL}"' base/backend-deployment.yaml
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-fe:${env.IMAGE_TAG_FINAL}"' base/frontend-deployment.yaml
"""
}
}
}
}
stage('Commit & Push Manifest Updates') {
steps {
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
sh """
git config user.email "jenkins@local"
git config user.name "jenkins"
git add -A
git diff --staged --quiet || (git commit -m "chore: update image to ${env.IMAGE_TAG_FINAL}" && git push https://${GIT_USER}:${GIT_PASS}@${env.MANIFEST_REPO.replace('https://', '')} ${env.BRANCH})
"""
echo "Manifest repo updated successfully"
dir('manifest') {
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
sh """
git config user.email "jenkins@local"
git config user.name "jenkins"
git add -A
git diff --staged --quiet || (git commit -m "chore: update image to ${env.IMAGE_TAG_FINAL}" && git push https://${GIT_USER}:${GIT_PASS}@${env.MANIFEST_REPO.replace('https://', '')} ${env.BRANCH})
"""
echo "Manifest repo updated successfully"
}
}
}
}