mirror of
https://github.com/won-ktds/smarketing-backend.git
synced 2025-12-06 15:16:23 +00:00
212 lines
10 KiB
Groovy
212 lines
10 KiB
Groovy
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'),
|
|
containerTemplate(name: 'azure-cli', image: 'hiondal/azure-kubectl:latest', command: 'cat', ttyEnabled: true),
|
|
containerTemplate(name: 'envsubst', image: "hiondal/envsubst", command: 'sleep', args: '1h')
|
|
],
|
|
volumes: [
|
|
emptyDirVolume(mountPath: '/home/gradle/.gradle', memory: false),
|
|
emptyDirVolume(mountPath: '/root/.azure', memory: false),
|
|
emptyDirVolume(mountPath: '/run/podman', memory: false)
|
|
]
|
|
) {
|
|
node(PIPELINE_ID) {
|
|
def props
|
|
def imageTag = getImageTag()
|
|
def manifest = "deploy.yaml"
|
|
def namespace
|
|
def services = ['member', 'store', 'marketing-content', 'ai-recommend']
|
|
|
|
stage("Get Source") {
|
|
checkout scm
|
|
props = readProperties file: "smarketing-java/deployment/deploy_env_vars"
|
|
namespace = "${props.namespace}"
|
|
|
|
echo "네임스페이스: ${namespace}"
|
|
echo "팀 ID: ${props.teamid}"
|
|
}
|
|
|
|
stage("Setup AKS") {
|
|
container('azure-cli') {
|
|
withCredentials([azureServicePrincipal('azure-credentials')]) {
|
|
sh """
|
|
echo "=== Azure 로그인 ==="
|
|
az login --service-principal -u \$AZURE_CLIENT_ID -p \$AZURE_CLIENT_SECRET -t \$AZURE_TENANT_ID
|
|
|
|
echo "=== AKS 인증정보 가져오기 ==="
|
|
az aks get-credentials --resource-group rg-digitalgarage-01 --name aks-digitalgarage-01 --overwrite-existing
|
|
|
|
echo "=== kubectl 연결 테스트 ==="
|
|
kubectl cluster-info
|
|
|
|
echo "=== 네임스페이스 생성 ==="
|
|
kubectl create namespace ${namespace} --dry-run=client -o yaml | kubectl apply -f -
|
|
|
|
echo "=== Image Pull Secret 생성 ==="
|
|
kubectl create secret docker-registry acr-secret \\
|
|
--docker-server=${props.registry} \\
|
|
--docker-username=acrdigitalgarage02 \\
|
|
--docker-password=\$(az acr credential show --name acrdigitalgarage02 --query passwords[0].value -o tsv) \\
|
|
--namespace=${namespace} \\
|
|
--dry-run=client -o yaml | kubectl apply -f -
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Build Application') {
|
|
container('gradle') {
|
|
sh """
|
|
cd smarketing-java
|
|
|
|
chmod +x gradlew
|
|
|
|
./gradlew :member:clean :member:build -x test
|
|
./gradlew :store:clean :store:build -x test
|
|
./gradlew :marketing-content:clean :marketing-content:build -x test
|
|
./gradlew :ai-recommend:clean :ai-recommend:build -x test
|
|
|
|
echo "=== 빌드 결과 확인 ==="
|
|
find . -name "*.jar" -path "*/build/libs/*"
|
|
"""
|
|
}
|
|
}
|
|
|
|
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 ->
|
|
script {
|
|
def buildDir = "smarketing-java/${service}"
|
|
def fullImageName = "${props.registry}/${props.image_org}/${service}:${imageTag}"
|
|
|
|
echo "Building image for ${service}: ${fullImageName}"
|
|
|
|
// 실제 JAR 파일명 동적 탐지
|
|
def actualJarFile = sh(
|
|
script: """
|
|
cd ${buildDir}/build/libs
|
|
ls *.jar | grep -v 'plain.jar' | head -1
|
|
""",
|
|
returnStdout: true
|
|
).trim()
|
|
|
|
if (!actualJarFile) {
|
|
error "${service} JAR 파일을 찾을 수 없습니다"
|
|
}
|
|
|
|
echo "발견된 JAR 파일: ${actualJarFile}"
|
|
|
|
sh """
|
|
echo "=== ${service} 이미지 빌드 ==="
|
|
podman build \\
|
|
--build-arg BUILD_LIB_DIR="${buildDir}/build/libs" \\
|
|
--build-arg ARTIFACTORY_FILE="${actualJarFile}" \\
|
|
-f smarketing-java/deployment/container/Dockerfile \\
|
|
-t ${fullImageName} .
|
|
|
|
echo "=== ${service} 이미지 푸시 ==="
|
|
podman push ${fullImageName}
|
|
|
|
echo "Successfully built and pushed: ${fullImageName}"
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Generate & Apply Manifest') {
|
|
container('envsubst') {
|
|
sh """
|
|
export namespace=${namespace}
|
|
export allowed_origins=${props.allowed_origins}
|
|
export jwt_secret_key=${props.jwt_secret_key}
|
|
export postgres_user=${props.postgres_user}
|
|
export postgres_password=${props.postgres_password}
|
|
export replicas=${props.replicas}
|
|
export resources_requests_cpu=${props.resources_requests_cpu}
|
|
export resources_requests_memory=${props.resources_requests_memory}
|
|
export resources_limits_cpu=${props.resources_limits_cpu}
|
|
export resources_limits_memory=${props.resources_limits_memory}
|
|
|
|
# 이미지 경로 환경변수 설정
|
|
export member_image_path=${props.registry}/${props.image_org}/member:${imageTag}
|
|
export store_image_path=${props.registry}/${props.image_org}/store:${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}
|
|
|
|
# manifest 생성
|
|
envsubst < smarketing-java/deployment/${manifest}.template > smarketing-java/deployment/${manifest}
|
|
|
|
echo "=== Generated Manifest File ==="
|
|
cat smarketing-java/deployment/${manifest}
|
|
echo "==============================="
|
|
"""
|
|
}
|
|
|
|
container('azure-cli') {
|
|
sh """
|
|
echo "=== Manifest 적용 ==="
|
|
kubectl apply -f smarketing-java/deployment/${manifest}
|
|
|
|
echo "=== 배포 상태 즉시 확인 ==="
|
|
kubectl -n ${namespace} get deployments
|
|
kubectl -n ${namespace} get pods
|
|
kubectl -n ${namespace} get events --sort-by='.lastTimestamp' | tail -20
|
|
|
|
echo "=== Pod 상세 정보 확인 ==="
|
|
for pod in \$(kubectl -n ${namespace} get pods -o name); do
|
|
echo "=== \$pod 상세 정보 ==="
|
|
kubectl -n ${namespace} describe \$pod
|
|
echo "=== \$pod 로그 ==="
|
|
kubectl -n ${namespace} logs \$pod --tail=50 || echo "로그 없음"
|
|
echo "================"
|
|
done
|
|
|
|
echo "=== Deployment 대기 (30초 timeout으로 단축) ==="
|
|
timeout 30 kubectl -n ${namespace} wait --for=condition=available deployment/member --timeout=30s || echo "member deployment 대기 실패"
|
|
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 "=== 최종 배포 상태 확인 ==="
|
|
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
|
|
if [ ! -z "\$pod" ]; then
|
|
echo "=== 실패한 Pod: \$pod ==="
|
|
kubectl -n ${namespace} describe \$pod
|
|
kubectl -n ${namespace} logs \$pod --previous --tail=50 2>/dev/null || echo "이전 로그 없음"
|
|
kubectl -n ${namespace} logs \$pod --tail=50 2>/dev/null || echo "현재 로그 없음"
|
|
fi
|
|
done
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|