This commit is contained in:
hiondal 2025-02-18 05:12:03 +09:00
parent 68330ded2e
commit f0526b281b

View File

@ -1,138 +1,143 @@
def PIPELINE_ID = "${env.BUILD_NUMBER}" def PIPELINE_ID = "${env.BUILD_NUMBER}"
def getImageTag() { def getImageTag() {
def dateFormat = new java.text.SimpleDateFormat('yyyyMMddHHmmss') def dateFormat = new java.text.SimpleDateFormat('yyyyMMddHHmmss')
def currentDate = new Date() def currentDate = new Date()
return dateFormat.format(currentDate) return dateFormat.format(currentDate)
} }
podTemplate( podTemplate(
label: "${PIPELINE_ID}", label: "${PIPELINE_ID}",
serviceAccount: 'jenkins', serviceAccount: 'jenkins',
containers: [ containers: [
containerTemplate(name: 'podman', containerTemplate(name: 'podman',
image: "mgoltzsche/podman", image: "mgoltzsche/podman",
ttyEnabled: true, ttyEnabled: true,
command: 'cat', command: 'cat',
privileged: true), privileged: true),
containerTemplate(name: 'gradle', containerTemplate(name: 'gradle',
image: 'gradle:jdk17', image: 'gradle:jdk17',
ttyEnabled: true, ttyEnabled: true,
command: 'cat', command: 'cat',
envVars: [ envVars: [
envVar(key: 'DOCKER_HOST', value: 'unix:///run/podman/podman.sock'), envVar(key: 'DOCKER_HOST', value: 'unix:///run/podman/podman.sock'),
envVar(key: 'TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE', value: '/run/podman/podman.sock'), envVar(key: 'TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE', value: '/run/podman/podman.sock'),
envVar(key: 'TESTCONTAINERS_RYUK_DISABLED', value: 'true') envVar(key: 'TESTCONTAINERS_RYUK_DISABLED', value: 'true')
]), ]),
containerTemplate(name: 'git', containerTemplate(name: 'git',
image: 'ghcr.io/chriswhong/git-yq:latest', image: 'alpine:3.19',
command: 'cat', command: 'cat',
ttyEnabled: true) ttyEnabled: true)
], ],
volumes: [ volumes: [
emptyDirVolume(mountPath: '/home/gradle/.gradle', memory: false), emptyDirVolume(mountPath: '/home/gradle/.gradle', memory: false),
emptyDirVolume(mountPath: '/run/podman', memory: false) emptyDirVolume(mountPath: '/run/podman', memory: false)
] ]
) { ) {
node(PIPELINE_ID) { node(PIPELINE_ID) {
def props def props
def imageTag = getImageTag() def imageTag = getImageTag()
def services = ['member', 'mysub', 'recommend'] def services = ['member', 'mysub', 'recommend']
def manifestRepo = 'cna-bootcamp/lifesub-manifest' def manifestRepo = 'cna-bootcamp/lifesub-manifest'
def manifestBranch = 'main' def manifestBranch = 'main'
stage("Get Source") { stage("Get Source") {
checkout scm checkout scm
props = readProperties file: "deployment/deploy_env_vars" props = readProperties file: "deployment/deploy_env_vars"
} }
stage('Build Applications') { stage('Build Applications') {
container('podman') { container('podman') {
sh 'podman system service -t 0 unix:///run/podman/podman.sock & sleep 2' sh 'podman system service -t 0 unix:///run/podman/podman.sock & sleep 2'
} }
container('gradle') { container('gradle') {
def testContainersConfig = '''docker.client.strategy=org.testcontainers.dockerclient.UnixSocketClientProviderStrategy def testContainersConfig = '''docker.client.strategy=org.testcontainers.dockerclient.UnixSocketClientProviderStrategy
docker.host=unix:///run/podman/podman.sock docker.host=unix:///run/podman/podman.sock
ryuk.container.privileged=true ryuk.container.privileged=true
testcontainers.reuse.enable=true''' testcontainers.reuse.enable=true'''
sh """ sh """
# TestContainers 설정 # TestContainers 설정
mkdir -p member/src/test/resources mysub-infra/src/test/resources recommend/src/test/resources 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}' > member/src/test/resources/testcontainers.properties
echo '${testContainersConfig}' > mysub-infra/src/test/resources/testcontainers.properties echo '${testContainersConfig}' > mysub-infra/src/test/resources/testcontainers.properties
echo '${testContainersConfig}' > recommend/src/test/resources/testcontainers.properties echo '${testContainersConfig}' > recommend/src/test/resources/testcontainers.properties
# 빌드 실행 # 빌드 실행
chmod +x gradlew chmod +x gradlew
./gradlew clean :member:build :mysub-infra:build :recommend:build ./gradlew clean :member:build :mysub-infra:build :recommend:build
""" """
} }
} }
stage('Build & Push Images') { stage('Build & Push Images') {
container('podman') { container('podman') {
withCredentials([usernamePassword( withCredentials([usernamePassword(
credentialsId: 'acr-credentials', credentialsId: 'acr-credentials',
usernameVariable: 'USERNAME', usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD' passwordVariable: 'PASSWORD'
)]) { )]) {
sh "podman login ${props.registry} --username \$USERNAME --password \$PASSWORD" sh "podman login ${props.registry} --username \$USERNAME --password \$PASSWORD"
services.each { service -> services.each { service ->
def buildDir = service == 'mysub' ? 'mysub-infra' : service def buildDir = service == 'mysub' ? 'mysub-infra' : service
def jarFile = service == 'mysub' ? 'mysub.jar' : "${service}.jar" def jarFile = service == 'mysub' ? 'mysub.jar' : "${service}.jar"
sh """ sh """
podman build \\ podman build \\
--build-arg BUILD_LIB_DIR="${buildDir}/build/libs" \\ --build-arg BUILD_LIB_DIR="${buildDir}/build/libs" \\
--build-arg ARTIFACTORY_FILE="${jarFile}" \\ --build-arg ARTIFACTORY_FILE="${jarFile}" \\
-f deployment/Dockerfile \\ -f deployment/Dockerfile \\
-t ${props.registry}/${props.image_org}/${service}:${imageTag} . -t ${props.registry}/${props.image_org}/${service}:${imageTag} .
podman push ${props.registry}/${props.image_org}/${service}:${imageTag} podman push ${props.registry}/${props.image_org}/${service}:${imageTag}
""" """
} }
} }
} }
} }
stage('Update Manifests') { stage('Update Manifests') {
container('git') { container('git') {
withCredentials([usernamePassword( // git과 yq 설치
credentialsId: 'github-credentials', sh '''
usernameVariable: 'GIT_USERNAME', apk add --no-cache git curl
passwordVariable: 'GIT_PASSWORD' curl -L https://github.com/mikefarah/yq/releases/download/v4.40.5/yq_linux_amd64 -o /usr/local/bin/yq
)]) { chmod +x /usr/local/bin/yq
sh ''' '''
git config --global user.email "jenkins@example.com"
git config --global user.name "Jenkins"
'''
// manifest 저장소를 https 프로토콜로 클론 withCredentials([usernamePassword(
sh """ credentialsId: 'github-credentials',
rm -rf lifesub-manifest usernameVariable: 'GIT_USERNAME',
git clone https://oauth2:${GIT_PASSWORD}@github.com/${manifestRepo}.git passwordVariable: 'GIT_PASSWORD'
""" )]) {
sh '''
git config --global user.email "jenkins@example.com"
git config --global user.name "Jenkins"
'''
dir('lifesub-manifest') { sh """
services.each { service -> rm -rf lifesub-manifest
def imagePath = "${props.registry}/${props.image_org}/${service}:${imageTag}" git clone https://oauth2:${GIT_PASSWORD}@github.com/${manifestRepo}.git
sh """ """
# deployment yaml 파일의 이미지 태그 업데이트
yq -i '.spec.template.spec.containers[0].image = "${imagePath}"' lifesub/deployments/${service}-deployment.yaml
"""
}
sh """ dir('lifesub-manifest') {
git add . services.each { service ->
git diff-index --quiet HEAD || git commit -m "Update backend services images to ${imageTag}" def imagePath = "${props.registry}/${props.image_org}/${service}:${imageTag}"
git push origin ${manifestBranch} sh """
""" yq -i '.spec.template.spec.containers[0].image = "${imagePath}"' lifesub/deployments/${service}-deployment.yaml
} """
} }
}
} sh """
} git add .
git diff-index --quiet HEAD || git commit -m "Update backend services images to ${imageTag}"
git push origin ${manifestBranch}
"""
}
}
}
}
}
} }