Update Jenkinsfile

This commit is contained in:
John Hanzu Kim 2025-06-17 09:36:26 +09:00 committed by GitHub
parent 42c071fbf0
commit d244f040ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,18 +10,15 @@ podTemplate(
label: "${PIPELINE_ID}", label: "${PIPELINE_ID}",
serviceAccount: 'jenkins', serviceAccount: 'jenkins',
containers: [ containers: [
containerTemplate(name: 'podman', image: "mgoltzsche/podman", ttyEnabled: true, command: 'cat', privileged: true), containerTemplate(name: 'gradle', image: 'gradle:jdk17', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'gradle', containerTemplate(name: 'docker', image: 'docker:20.10.16-dind', ttyEnabled: true, privileged: true),
image: 'gradle:jdk17',
ttyEnabled: true,
command: 'cat'),
containerTemplate(name: 'azure-cli', image: 'hiondal/azure-kubectl:latest', command: 'cat', ttyEnabled: true), containerTemplate(name: 'azure-cli', image: 'hiondal/azure-kubectl:latest', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'envsubst', image: "hiondal/envsubst", command: 'sleep', args: '1h') containerTemplate(name: 'envsubst', image: "hiondal/envsubst", command: 'sleep', args: '1h')
], ],
volumes: [ volumes: [
emptyDirVolume(mountPath: '/home/gradle/.gradle', memory: false), emptyDirVolume(mountPath: '/home/gradle/.gradle', memory: false),
emptyDirVolume(mountPath: '/root/.azure', memory: false), emptyDirVolume(mountPath: '/root/.azure', memory: false),
emptyDirVolume(mountPath: '/run/podman', memory: false) emptyDirVolume(mountPath: '/var/run', memory: false)
] ]
) { ) {
node(PIPELINE_ID) { node(PIPELINE_ID) {
@ -33,11 +30,15 @@ podTemplate(
stage("Get Source") { stage("Get Source") {
checkout scm checkout scm
// smarketing-java 하위에 있는 설정 파일 읽기
props = readProperties file: "smarketing-java/deployment/deploy_env_vars" props = readProperties file: "smarketing-java/deployment/deploy_env_vars"
namespace = "${props.namespace}" namespace = "${props.namespace}"
echo "네임스페이스: ${namespace}" echo "=== Build Information ==="
echo "팀 ID: ${props.teamid}" echo "Services: ${services}"
echo "Namespace: ${namespace}"
echo "Image Tag: ${imageTag}"
} }
stage("Setup AKS") { stage("Setup AKS") {
@ -46,13 +47,11 @@ podTemplate(
sh """ sh """
echo "=== Azure 로그인 ===" echo "=== Azure 로그인 ==="
az login --service-principal -u \$AZURE_CLIENT_ID -p \$AZURE_CLIENT_SECRET -t \$AZURE_TENANT_ID az login --service-principal -u \$AZURE_CLIENT_ID -p \$AZURE_CLIENT_SECRET -t \$AZURE_TENANT_ID
az account set --subscription 2513dd36-7978-48e3-9a7c-b221d4874f66
echo "=== AKS 인증정보 가져오기 ===" echo "=== AKS 인증정보 가져오기 ==="
az aks get-credentials --resource-group rg-digitalgarage-01 --name aks-digitalgarage-01 --overwrite-existing az aks get-credentials --resource-group rg-digitalgarage-01 --name aks-digitalgarage-01 --overwrite-existing
echo "=== kubectl 연결 테스트 ==="
kubectl cluster-info
echo "=== 네임스페이스 생성 ===" echo "=== 네임스페이스 생성 ==="
kubectl create namespace ${namespace} --dry-run=client -o yaml | kubectl apply -f - kubectl create namespace ${namespace} --dry-run=client -o yaml | kubectl apply -f -
@ -63,37 +62,48 @@ podTemplate(
--docker-password=\$(az acr credential show --name acrdigitalgarage02 --query passwords[0].value -o tsv) \\ --docker-password=\$(az acr credential show --name acrdigitalgarage02 --query passwords[0].value -o tsv) \\
--namespace=${namespace} \\ --namespace=${namespace} \\
--dry-run=client -o yaml | kubectl apply -f - --dry-run=client -o yaml | kubectl apply -f -
echo "=== 클러스터 상태 확인 ==="
kubectl get nodes
kubectl get ns ${namespace}
""" """
} }
} }
} }
stage('Build Application') { stage('Build Applications') {
container('gradle') { container('gradle') {
sh """ sh """
echo "=== smarketing-java 디렉토리로 이동 ==="
cd smarketing-java cd smarketing-java
echo "=== gradlew 권한 설정 ==="
chmod +x gradlew chmod +x gradlew
echo "=== 전체 서비스 빌드 ==="
./gradlew :member:clean :member:build -x test ./gradlew :member:clean :member:build -x test
./gradlew :store:clean :store:build -x test ./gradlew :store:clean :store:build -x test
./gradlew :marketing-content:clean :marketing-content:build -x test ./gradlew :marketing-content:clean :marketing-content:build -x test
./gradlew :ai-recommend:clean :ai-recommend:build -x test ./gradlew :ai-recommend:clean :ai-recommend:build -x test
echo "=== 빌드 결과 확인 ===" echo "=== 빌드 결과 확인 ==="
find . -name "*.jar" -path "*/build/libs/*" find . -name "*.jar" -path "*/build/libs/*" | grep -v 'plain.jar'
""" """
} }
} }
stage('Build & Push Images') { stage('Build & Push Images') {
container('podman') { container('docker') {
withCredentials([usernamePassword( withCredentials([azureServicePrincipal('azure-credentials')]) {
credentialsId: 'acr-credentials', sh """
usernameVariable: 'USERNAME', echo "=== Docker 데몬 시작 대기 ==="
passwordVariable: 'PASSWORD' timeout 30 sh -c 'until docker info; do sleep 1; done'
)]) {
sh "podman login ${props.registry} --username \$USERNAME --password \$PASSWORD" echo "=== Azure 로그인 및 ACR 로그인 ==="
az login --service-principal -u \$AZURE_CLIENT_ID -p \$AZURE_CLIENT_SECRET -t \$AZURE_TENANT_ID
az account set --subscription 2513dd36-7978-48e3-9a7c-b221d4874f66
az acr login --name acrdigitalgarage02
"""
services.each { service -> services.each { service ->
script { script {
@ -119,14 +129,14 @@ podTemplate(
sh """ sh """
echo "=== ${service} 이미지 빌드 ===" echo "=== ${service} 이미지 빌드 ==="
podman build \\ docker build \\
--build-arg BUILD_LIB_DIR="${buildDir}/build/libs" \\ --build-arg BUILD_LIB_DIR="${buildDir}/build/libs" \\
--build-arg ARTIFACTORY_FILE="${actualJarFile}" \\ --build-arg ARTIFACTORY_FILE="${actualJarFile}" \\
-f smarketing-java/deployment/container/Dockerfile \\ -f smarketing-java/deployment/container/Dockerfile \\
-t ${fullImageName} . -t ${fullImageName} .
echo "=== ${service} 이미지 푸시 ===" echo "=== ${service} 이미지 푸시 ==="
podman push ${fullImageName} docker push ${fullImageName}
echo "Successfully built and pushed: ${fullImageName}" echo "Successfully built and pushed: ${fullImageName}"
""" """
@ -139,16 +149,18 @@ podTemplate(
stage('Generate & Apply Manifest') { stage('Generate & Apply Manifest') {
container('envsubst') { container('envsubst') {
sh """ sh """
echo "=== 환경변수 설정 ==="
export namespace=${namespace} export namespace=${namespace}
export allowed_origins=${props.allowed_origins} export allowed_origins=${props.allowed_origins}
export jwt_secret_key=${props.jwt_secret_key} export jwt_secret_key=${props.jwt_secret_key}
export postgres_user=${props.postgres_user} export postgres_user=${props.postgres_user}
export postgres_password=${props.postgres_password} export postgres_password=${props.postgres_password}
export replicas=${props.replicas} export replicas=${props.replicas}
export resources_requests_cpu=${props.resources_requests_cpu} # 리소스 요구사항 조정 (작게)
export resources_requests_memory=${props.resources_requests_memory} export resources_requests_cpu=100m
export resources_limits_cpu=${props.resources_limits_cpu} export resources_requests_memory=128Mi
export resources_limits_memory=${props.resources_limits_memory} export resources_limits_cpu=500m
export resources_limits_memory=512Mi
# 이미지 경로 환경변수 설정 # 이미지 경로 환경변수 설정
export member_image_path=${props.registry}/${props.image_org}/member:${imageTag} export member_image_path=${props.registry}/${props.image_org}/member:${imageTag}
@ -156,7 +168,7 @@ podTemplate(
export marketing_content_image_path=${props.registry}/${props.image_org}/marketing-content:${imageTag} export marketing_content_image_path=${props.registry}/${props.image_org}/marketing-content:${imageTag}
export ai_recommend_image_path=${props.registry}/${props.image_org}/ai-recommend:${imageTag} export ai_recommend_image_path=${props.registry}/${props.image_org}/ai-recommend:${imageTag}
# manifest 생성 echo "=== Manifest 생성 ==="
envsubst < smarketing-java/deployment/${manifest}.template > smarketing-java/deployment/${manifest} envsubst < smarketing-java/deployment/${manifest}.template > smarketing-java/deployment/${manifest}
echo "=== Generated Manifest File ===" echo "=== Generated Manifest File ==="
@ -167,41 +179,30 @@ podTemplate(
container('azure-cli') { container('azure-cli') {
sh """ sh """
echo "=== PostgreSQL 서비스 확인 ==="
kubectl get svc -n ${namespace} | grep postgresql || echo "PostgreSQL 서비스가 없습니다. 먼저 설치해주세요."
echo "=== Manifest 적용 ===" echo "=== Manifest 적용 ==="
kubectl apply -f smarketing-java/deployment/${manifest} kubectl apply -f smarketing-java/deployment/${manifest}
echo "=== 배포 상태 즉시 확인 ===" echo "=== 배포 상태 확인 (60초 대기) ==="
kubectl -n ${namespace} get deployments kubectl -n ${namespace} get deployments
kubectl -n ${namespace} get pods kubectl -n ${namespace} get pods
kubectl -n ${namespace} get events --sort-by='.lastTimestamp' | tail -20
echo "=== Pod 상세 정보 확인 ===" echo "=== 각 서비스 배포 대기 (60초 timeout) ==="
for pod in \$(kubectl -n ${namespace} get pods -o name); do timeout 60 kubectl -n ${namespace} wait --for=condition=available deployment/member --timeout=60s || echo "member deployment 대기 타임아웃"
echo "=== \$pod 상세 정보 ===" timeout 60 kubectl -n ${namespace} wait --for=condition=available deployment/store --timeout=60s || echo "store deployment 대기 타임아웃"
kubectl -n ${namespace} describe \$pod timeout 60 kubectl -n ${namespace} wait --for=condition=available deployment/marketing-content --timeout=60s || echo "marketing-content deployment 대기 타임아웃"
echo "=== \$pod 로그 ===" timeout 60 kubectl -n ${namespace} wait --for=condition=available deployment/ai-recommend --timeout=60s || echo "ai-recommend deployment 대기 타임아웃"
kubectl -n ${namespace} logs \$pod --tail=50 || echo "로그 없음"
echo "================"
done
echo "=== Deployment 대기 (30초 timeout으로 단축) ===" echo "=== 최종 상태 ==="
timeout 30 kubectl -n ${namespace} wait --for=condition=available deployment/member --timeout=30s || echo "member deployment 대기 실패" kubectl -n ${namespace} get all
timeout 30 kubectl -n ${namespace} wait --for=condition=available deployment/store --timeout=30s || echo "store deployment 대기 실패"
timeout 30 kubectl -n ${namespace} wait --for=condition=available deployment/marketing-content --timeout=30s || echo "marketing-content deployment 대기 실패"
timeout 30 kubectl -n ${namespace} wait --for=condition=available deployment/ai-recommend --timeout=30s || echo "ai-recommend deployment 대기 실패"
echo "=== 최종 배포 상태 확인 ===" echo "=== 실패한 Pod 상세 정보 ==="
kubectl -n ${namespace} get deployments
kubectl -n ${namespace} get pods
kubectl -n ${namespace} get services
echo "=== 실패한 Pod가 있다면 상세 로그 ==="
for pod in \$(kubectl -n ${namespace} get pods --field-selector=status.phase!=Running -o name 2>/dev/null || true); do for pod in \$(kubectl -n ${namespace} get pods --field-selector=status.phase!=Running -o name 2>/dev/null || true); do
if [ ! -z "\$pod" ]; then if [ ! -z "\$pod" ]; then
echo "=== 실패한 Pod: \$pod ===" echo "=== 실패한 Pod: \$pod ==="
kubectl -n ${namespace} describe \$pod kubectl -n ${namespace} describe \$pod | tail -20
kubectl -n ${namespace} logs \$pod --previous --tail=50 2>/dev/null || echo "이전 로그 없음"
kubectl -n ${namespace} logs \$pod --tail=50 2>/dev/null || echo "현재 로그 없음"
fi fi
done done
""" """