This commit is contained in:
Ubuntu 2025-05-30 02:04:51 +00:00
parent 477e45239c
commit 393f2c43fa

View File

@ -0,0 +1,171 @@
def PIPELINE_ID = "${env.BUILD_NUMBER}"
def getImageTag() {
def dateFormat = new java.text.SimpleDateFormat('yyyyMMddHHmmss')
def currentDate = new Date()
return dateFormat.format(currentDate)
}
podTemplate(
label: "${PIPELINE_ID}",
serviceAccount: 'jenkins',
containers: [
containerTemplate(name: 'podman', image: "mgoltzsche/podman", ttyEnabled: true, command: 'cat', privileged: true),
containerTemplate(name: 'gradle',
image: 'gradle:jdk17',
ttyEnabled: true,
command: 'cat',
envVars: [
envVar(key: 'DOCKER_HOST', value: 'unix:///run/podman/podman.sock'),
envVar(key: 'TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE', value: '/run/podman/podman.sock'),
envVar(key: 'TESTCONTAINERS_RYUK_DISABLED', value: 'true')
]),
containerTemplate(name: 'git', image: 'alpine/git:latest', command: 'cat', ttyEnabled: true)
],
volumes: [
emptyDirVolume(mountPath: '/home/gradle/.gradle', memory: false),
emptyDirVolume(mountPath: '/run/podman', memory: false)
]
) {
node(PIPELINE_ID) {
def props
def imageTag = getImageTag()
def services = ['member', 'mysub', 'recommend']
stage("Get Source") {
checkout scm
props = readProperties file: "deployment/deploy_env_vars"
}
stage('Build Applications & SonarQube Analysis') {
container('podman') {
sh 'podman system service -t 0 unix:///run/podman/podman.sock & sleep 2'
}
container('gradle') {
def testContainersConfig = '''docker.client.strategy=org.testcontainers.dockerclient.UnixSocketClientProviderStrategy
docker.host=unix:///run/podman/podman.sock
ryuk.container.privileged=true
testcontainers.reuse.enable=true'''
sh """
# TestContainers 설정
mkdir -p member/src/test/resources mysub-infra/src/test/resources recommend/src/test/resources
echo '${testContainersConfig}' > member/src/test/resources/testcontainers.properties
echo '${testContainersConfig}' > mysub-infra/src/test/resources/testcontainers.properties
echo '${testContainersConfig}' > recommend/src/test/resources/testcontainers.properties
"""
// 빌드 및 SonarQube 분석
withSonarQubeEnv('SonarQube') {
sh """
chmod +x gradlew
# 빌드 실행
./gradlew :member:build :mysub-infra:build :recommend:build -x test
# Member 서비스
./gradlew :member:test :member:jacocoTestReport :member:sonar \
-Dsonar.projectKey=lifesub-member-dg0400 \
-Dsonar.projectName=lifesub-member \
-Dsonar.java.binaries=build/classes/java/main \
-Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml \
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
# Recommend 서비스
./gradlew :recommend:test :recommend:jacocoTestReport :recommend:sonar \
-Dsonar.projectKey=lifesub-recommend-dg0400 \
-Dsonar.projectName=lifesub-recommend \
-Dsonar.java.binaries=build/classes/java/main \
-Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml \
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
# Mysub 서비스 (biz & infra 구조)
./gradlew :mysub-infra:test :mysub-infra:jacocoTestReport :mysub-infra:sonar \
-Dsonar.projectKey=lifesub-mysub-dg0400 \
-Dsonar.projectName=lifesub-mysub \
-Dsonar.java.binaries=build/classes/java/main \
-Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml \
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
"""
}
}
}
stage('Quality Gate') {
timeout(time: 10, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage('Build & Push Images') {
container('podman') {
withCredentials([usernamePassword(
credentialsId: 'acr-credentials',
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD'
)]) {
sh "podman login ${props.registry} --username \$USERNAME --password \$PASSWORD"
services.each { service ->
def buildDir = service == 'mysub' ? 'mysub-infra' : service
def jarFile = service == 'mysub' ? 'mysub.jar' : "${service}.jar"
sh """
podman build \
--build-arg BUILD_LIB_DIR="${buildDir}/build/libs" \
--build-arg ARTIFACTORY_FILE="${jarFile}" \
-f deployment/container/Dockerfile \
-t ${props.registry}/${props.image_org}/${service}:${imageTag} .
podman push ${props.registry}/${props.image_org}/${service}:${imageTag}
"""
}
}
}
}
stage('Update Manifest Repository') {
container('git') {
withCredentials([usernamePassword(
credentialsId: 'github-credentials-dg0400',
usernameVariable: 'GIT_USERNAME',
passwordVariable: 'GIT_PASSWORD'
)]) {
sh """
# Git 설정
git config --global user.name "Jenkins"
git config --global user.email "jenkins@example.com"
# Manifest Repository Clone
git clone https://\$GIT_USERNAME:\$GIT_PASSWORD@github.com/cna-bootcamp/lifesub-manifest.git
cd lifesub-manifest
# 각 서비스별 이미지 태그 업데이트
for service in ${services.join(' ')}; do
echo "Updating \$service deployment with image tag: ${imageTag}"
if [ -f "lifesub/\$service/\$service-deployment.yaml" ]; then
# 기존 이미지 태그를 새 태그로 교체
sed -i "s|image: ${props.registry}/${props.image_org}/\$service:.*|image: ${props.registry}/${props.image_org}/\$service:${imageTag}|g" lifesub/\$service/\$service-deployment.yaml
echo "Updated \$service deployment file:"
cat lifesub/\$service/\$service-deployment.yaml | grep "image:"
else
echo "Warning: Deployment file for \$service not found"
fi
done
# 변경사항 커밋 및 푸시
git add .
git commit -m "Update backend services image tags to ${imageTag}" || echo "No changes to commit"
git push origin main
"""
}
}
}
}
}