mirror of
https://github.com/cna-bootcamp/lifesub.git
synced 2025-12-06 08:06:24 +00:00
186 lines
8.8 KiB
Plaintext
186 lines
8.8 KiB
Plaintext
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: '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', 'mysub', 'recommend']
|
|
|
|
stage("Get Source") {
|
|
checkout scm
|
|
props = readProperties file: "deployment/deploy_env_vars"
|
|
namespace = "${props.namespace}"
|
|
}
|
|
|
|
stage("Setup AKS") {
|
|
container('azure-cli') {
|
|
withCredentials([azureServicePrincipal('azure-credentials')]) {
|
|
sh """
|
|
az login --service-principal -u \$AZURE_CLIENT_ID -p \$AZURE_CLIENT_SECRET -t \$AZURE_TENANT_ID
|
|
az aks get-credentials --resource-group ictcoe-edu --name ${props.teamid}-aks --overwrite-existing
|
|
kubectl create namespace ${namespace} --dry-run=client -o yaml | kubectl apply -f -
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
"""
|
|
|
|
// Member 서비스 빌드 및 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 \
|
|
-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 \
|
|
-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 \
|
|
-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/Dockerfile \
|
|
-t ${props.registry}/${props.image_org}/${service}:${imageTag} .
|
|
|
|
podman push ${props.registry}/${props.image_org}/${service}:${imageTag}
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 mysub_image_path=${props.registry}/${props.image_org}/mysub:${imageTag}
|
|
export recommend_image_path=${props.registry}/${props.image_org}/recommend:${imageTag}
|
|
|
|
# manifest 생성
|
|
envsubst < deployment/${manifest}.template > deployment/${manifest}
|
|
cat deployment/${manifest}
|
|
"""
|
|
}
|
|
|
|
container('azure-cli') {
|
|
sh """
|
|
kubectl apply -f deployment/${manifest}
|
|
|
|
echo "Waiting for deployments to be ready..."
|
|
kubectl -n ${namespace} wait --for=condition=available deployment/member --timeout=300s
|
|
kubectl -n ${namespace} wait --for=condition=available deployment/mysub --timeout=300s
|
|
kubectl -n ${namespace} wait --for=condition=available deployment/recommend --timeout=300s
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
} |