첫 배포를 하기 위해서 CD(지속적인 배포 Continous Delivery)까지 적용을 하려고 했다.
이전 글에서 배포용 ec2 와 RDS 관련 설정을 해주었다.
현재 내 프로젝트의 작업 순서를 보면 다음과 같다.
feature 브랜치에서 로드 작성 → pr 생성 → CI 작업 → 머지
자동화를 해둔 부분은 CI 밖에 없었기 때문에, 미루고 미뤘던 CD를 해보기로 하였다.
나는 젠킨스의 멀티브랜치파이프 라인 을 통해서 각 브랜치를 관리 했고, 해당 파이프라인 스크립트는 다음과 같다.
pipeline {
agent any
options {
disableConcurrentBuilds()
}
stages {
stage('CHECK-OUT') {
steps {
git branch: env.GIT_BRANCH,
credentialsId: 'test',
url: '<https://github.com/kodesalon/HamKke>'
}
}
stage('runsonarqube'){
steps{
sh '''
echo runsonarqube
./gradlew sonar \
-Dsonar.projectKey=hamkke \
-Dsonar.host.url=http://3.37.62.142:9000 \
-Dsonar.login=sqp_8b88bdf437937b274ff1ef9e7b3aec8301a88b43
'''
}
}
stage('build'){
steps{
sh '''
echo build start
./gradlew clean build -Pprofile=ci
'''
}
}
}
}
}
CI 에서는 브랜치를 체크아웃하고, build를 실행하고 소나큐브를 통해 정적 코드 분석을 하는 작업을 해주었다.
이후 배포 자동화 프로세스를 하기 위해서 파이프라인을 수정하였다.
우선 main 브랜치를 배포하는 것이아닌 devlop 브랜치를 배포하는 것으로 테스트를 해보았다.
pipeline {
agent any
options {
disableConcurrentBuilds()
}
stages {
stage('CHECK-OUT') {
steps {
git branch: env.GIT_BRANCH,
credentialsId: 'test',
url: '<https://github.com/kodesalon/HamKke>'
}
}
stage('runsonarqube'){
steps{
sh '''
echo runsonarqube
./gradlew sonar \
-Dsonar.projectKey=hamkke \
-Dsonar.host.url=http://3.37.62.142:9000 \
-Dsonar.login=sqp_8b88bdf437937b274ff1ef9e7b3aec8301a88b43
'''
}
}
stage('build'){
steps{
sh '''
echo build start
./gradlew clean build -Pprofile=ci
'''
}
}
stage('DEPLOY'){
when{
expression {env.GIT_BRANCH == 'develop'}
}
steps{
withCredentials([sshUserPrivateKey(credentialsId: "pemkey", keyFileVariable: 'key')]) {
sh '''echo develop!!!!!!!!!!!@#
echo build start
ssh -oStrictHostKeyChecking=no -T -i ${key} ubuntu@배포ec2-IP "
cd /home/ubuntu/HamKke
git checkout develop
git pull
./gradlew clean bootjar
cd /home/ubuntu/HamKke/build/libs
nohup java -jar -Dspring.profiles.active=deploy board-0.0.1-SNAPSHOT.jar &
"
echo build end
'''
}
}
}
}
}
정상적으로 작동하는 것을 확인하였고, 최종적으로 main 브랜치에서의 배포가 될 수 있도록 수정을 해주면