mirror of
https://github.com/won-ktds/smarketing-backend.git
synced 2025-12-06 07:06:24 +00:00
Update Jenkinsfile
This commit is contained in:
parent
a87032e7a6
commit
135264dd95
188
smarketing-java/deployment/Jenkinsfile
vendored
188
smarketing-java/deployment/Jenkinsfile
vendored
@ -1,181 +1,8 @@
|
|||||||
def PIPELINE_ID = "${env.BUILD_NUMBER}"
|
stage('Generate & Apply Manifest') {
|
||||||
|
|
||||||
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: 'gradle', image: 'gradle:jdk17', ttyEnabled: true, command: 'cat'),
|
|
||||||
containerTemplate(name: 'docker', image: 'docker:20.10.16-dind', ttyEnabled: true, privileged: true),
|
|
||||||
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: '/var/run', 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
|
|
||||||
|
|
||||||
// smarketing-java 하위에 있는 설정 파일 읽기
|
|
||||||
props = readProperties file: "smarketing-java/deployment/deploy_env_vars"
|
|
||||||
namespace = "${props.namespace}"
|
|
||||||
|
|
||||||
echo "=== Build Information ==="
|
|
||||||
echo "Services: ${services}"
|
|
||||||
echo "Namespace: ${namespace}"
|
|
||||||
echo "Image Tag: ${imageTag}"
|
|
||||||
}
|
|
||||||
|
|
||||||
stage("Check Changes") {
|
|
||||||
script {
|
|
||||||
def changes = sh(
|
|
||||||
script: "git diff --name-only HEAD~1 HEAD",
|
|
||||||
returnStdout: true
|
|
||||||
).trim()
|
|
||||||
|
|
||||||
if (!changes.contains("smarketing-java/")) {
|
|
||||||
echo "No changes in smarketing-java, skipping build"
|
|
||||||
currentBuild.result = 'SUCCESS'
|
|
||||||
error("Stopping pipeline - no changes detected")
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Changes detected in smarketing-java, proceeding with build"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
az account set --subscription 2513dd36-7978-48e3-9a7c-b221d4874f66
|
|
||||||
|
|
||||||
echo "=== AKS 인증정보 가져오기 (rg-digitalgarage-02) ==="
|
|
||||||
az aks get-credentials --resource-group rg-digitalgarage-02 --name aks-digitalgarage-02 --overwrite-existing
|
|
||||||
|
|
||||||
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 -
|
|
||||||
|
|
||||||
echo "=== 클러스터 상태 확인 ==="
|
|
||||||
kubectl get nodes
|
|
||||||
kubectl get ns ${namespace}
|
|
||||||
|
|
||||||
echo "=== 현재 연결된 클러스터 확인 ==="
|
|
||||||
kubectl config current-context
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Build Applications') {
|
|
||||||
container('gradle') {
|
|
||||||
sh """
|
|
||||||
echo "=== smarketing-java 디렉토리로 이동 ==="
|
|
||||||
cd smarketing-java
|
|
||||||
|
|
||||||
echo "=== gradlew 권한 설정 ==="
|
|
||||||
chmod +x gradlew
|
|
||||||
|
|
||||||
echo "=== 전체 서비스 빌드 ==="
|
|
||||||
./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/*" | grep -v 'plain.jar'
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Build & Push Images') {
|
|
||||||
container('docker') {
|
|
||||||
sh """
|
|
||||||
echo "=== Docker 데몬 시작 대기 ==="
|
|
||||||
timeout 30 sh -c 'until docker info; do sleep 1; done'
|
|
||||||
"""
|
|
||||||
|
|
||||||
// ACR Credential을 Jenkins에서 직접 사용
|
|
||||||
withCredentials([usernamePassword(
|
|
||||||
credentialsId: 'acr-credentials',
|
|
||||||
usernameVariable: 'ACR_USERNAME',
|
|
||||||
passwordVariable: 'ACR_PASSWORD'
|
|
||||||
)]) {
|
|
||||||
sh """
|
|
||||||
echo "=== Docker로 ACR 로그인 ==="
|
|
||||||
echo "\$ACR_PASSWORD" | docker login ${props.registry} --username \$ACR_USERNAME --password-stdin
|
|
||||||
"""
|
|
||||||
|
|
||||||
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} 이미지 빌드 ==="
|
|
||||||
docker build \\
|
|
||||||
--build-arg BUILD_LIB_DIR="${buildDir}/build/libs" \\
|
|
||||||
--build-arg ARTIFACTORY_FILE="${actualJarFile}" \\
|
|
||||||
-f smarketing-java/deployment/container/Dockerfile \\
|
|
||||||
-t ${fullImageName} .
|
|
||||||
|
|
||||||
echo "=== ${service} 이미지 푸시 ==="
|
|
||||||
docker push ${fullImageName}
|
|
||||||
|
|
||||||
echo "Successfully built and pushed: ${fullImageName}"
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Generate & Apply Manifest') {
|
|
||||||
container('envsubst') {
|
container('envsubst') {
|
||||||
script {
|
script {
|
||||||
def postgresPort = props.POSTGRES_PORT ?: '5432'
|
def postgresPort = "'" + (props.POSTGRES_PORT ?: '5432') + "'"
|
||||||
def redisPort = props.REDIS_PORT ?: '6379'
|
def redisPort = "'" + (props.REDIS_PORT ?: '6379') + "'"
|
||||||
|
|
||||||
sh """
|
sh """
|
||||||
echo "=== 환경변수 설정 ==="
|
echo "=== 환경변수 설정 ==="
|
||||||
@ -186,7 +13,7 @@ podTemplate(
|
|||||||
export postgres_password=${props.postgres_password}
|
export postgres_password=${props.postgres_password}
|
||||||
export replicas=${props.replicas}
|
export replicas=${props.replicas}
|
||||||
|
|
||||||
# PostgreSQL 환경변수 추가
|
# PostgreSQL 환경변수 추가 (문자열로)
|
||||||
export postgres_host=${props.POSTGRES_HOST}
|
export postgres_host=${props.POSTGRES_HOST}
|
||||||
export postgres_port=${postgresPort}
|
export postgres_port=${postgresPort}
|
||||||
export postgres_db_member=member
|
export postgres_db_member=member
|
||||||
@ -194,7 +21,7 @@ podTemplate(
|
|||||||
export postgres_db_marketing_content=marketing_content
|
export postgres_db_marketing_content=marketing_content
|
||||||
export postgres_db_ai_recommend=AiRecommendationDB
|
export postgres_db_ai_recommend=AiRecommendationDB
|
||||||
|
|
||||||
# Redis 환경변수 추가
|
# Redis 환경변수 추가 (문자열로)
|
||||||
export redis_host=${props.REDIS_HOST}
|
export redis_host=${props.REDIS_HOST}
|
||||||
export redis_port=${redisPort}
|
export redis_port=${redisPort}
|
||||||
export redis_password=${props.REDIS_PASSWORD}
|
export redis_password=${props.REDIS_PASSWORD}
|
||||||
@ -223,6 +50,9 @@ podTemplate(
|
|||||||
|
|
||||||
container('azure-cli') {
|
container('azure-cli') {
|
||||||
sh """
|
sh """
|
||||||
|
echo "=== 기존 ConfigMap 삭제 (타입 충돌 해결) ==="
|
||||||
|
kubectl delete configmap member-config store-config marketing-content-config ai-recommend-config -n ${namespace} --ignore-not-found=true
|
||||||
|
|
||||||
echo "=== 현재 연결된 클러스터 재확인 ==="
|
echo "=== 현재 연결된 클러스터 재확인 ==="
|
||||||
kubectl config current-context
|
kubectl config current-context
|
||||||
kubectl cluster-info | head -3
|
kubectl cluster-info | head -3
|
||||||
@ -255,6 +85,4 @@ podTemplate(
|
|||||||
done
|
done
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user