feat: 무중단 배포 추가 #146
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: BE CI/CD with Rolling Deployment | ||
on: | ||
push: | ||
branches: [ "develop" ] | ||
pull_request: | ||
branches: [ "develop" ] | ||
jobs: | ||
backend-CI-and-Deploy: | ||
runs-on: ubuntu-latest | ||
env: | ||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
AWS_REGION: ${{ secrets.AWS_REGION }} | ||
TARGET_GROUP_ARN: ${{ secrets.TARGET_GROUP_ARN }} | ||
WORKING_DIRECTORY: ${{ secrets.WORKING_DIRECTORY }} | ||
EC2_USER: ${{ secrets.EC2_USER }} | ||
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} | ||
EC2_HOST_1: ${{ secrets.EC2_HOST_1 }} | ||
EC2_HOST_2: ${{ secrets.EC2_HOST_2 }} | ||
EC2_INSTANCE_ID_1: ${{ secrets.EC2_INSTANCE_ID_1 }} | ||
EC2_INSTANCE_ID_2: ${{ secrets.EC2_INSTANCE_ID_2 }} | ||
steps: | ||
# 소스 코드 체크아웃 | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
# AWS CLI 설치 | ||
- name: Install AWS CLI v2 | ||
run: | | ||
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | ||
unzip awscliv2.zip | ||
sudo ./aws/install | ||
# JDK 17 설정 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
# 환경별 application.yml 파일 생성 | ||
- name: Create application.yml files | ||
run: | | ||
mkdir -p $WORKING_DIRECTORY/src/main/resources/ | ||
echo "${{ secrets.APPLICATION_DEFAULT }}" > $WORKING_DIRECTORY/src/main/resources/application.yml | ||
echo "${{ secrets.APPLICATION_DEV }}" > $WORKING_DIRECTORY/src/main/resources/application-dev.yml | ||
echo "${{ secrets.APPLICATION_LOCAL }}" > $WORKING_DIRECTORY/src/main/resources/application-local.yml | ||
echo "${{ secrets.APPLICATION_PROD }}" > $WORKING_DIRECTORY/src/main/resources/application-prod.yml | ||
# Gradle 실행 권한 설정 | ||
- name: Grant execute permission to gradlew | ||
run: chmod +x gradlew | ||
working-directory: $WORKING_DIRECTORY | ||
# Gradle 빌드 및 테스트 실행 | ||
- name: Build and Test with Gradle | ||
run: ./gradlew clean build | ||
working-directory: $WORKING_DIRECTORY | ||
# 빌드된 JAR 파일을 EC2_HOST_1로 복사 | ||
- name: Copy JAR file to EC2_HOST_1 | ||
uses: appleboy/scp-action@master | ||
with: | ||
source: $WORKING_DIRECTORY/build/libs/*.jar | ||
target: /home/ubuntu/ | ||
host: $EC2_HOST_1 | ||
username: $EC2_USER | ||
key: $EC2_SSH_KEY | ||
# EC2_HOST_1에서 배포 및 대상 그룹 관리 | ||
- name: Deploy to EC2_HOST_1 | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: $EC2_HOST_1 | ||
username: $EC2_USER | ||
key: $EC2_SSH_KEY | ||
envs: | ||
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID | ||
Check failure on line 82 in .github/workflows/gradle-publish.yml GitHub Actions / BE CI/CD with Rolling DeploymentInvalid workflow file
|
||
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY | ||
AWS_REGION: $AWS_REGION | ||
TARGET_GROUP_ARN: $TARGET_GROUP_ARN | ||
INSTANCE_ID: $EC2_INSTANCE_ID_1 | ||
script: | | ||
# 대상 그룹에서 인스턴스 제거 | ||
aws elbv2 deregister-targets --target-group-arn $TARGET_GROUP_ARN --targets Id=$INSTANCE_ID | ||
# 기존 애플리케이션 종료 | ||
if screen -list | grep -q "\bbe\b"; then screen -S be -X quit; fi | ||
# 새로운 애플리케이션 실행 | ||
screen -dmS be java -jar /home/ubuntu/backend-server.jar | ||
# 애플리케이션 시작 대기 | ||
sleep 30 | ||
# 대상 그룹에 인스턴스 등록 | ||
aws elbv2 register-targets --target-group-arn $TARGET_GROUP_ARN --targets Id=$INSTANCE_ID | ||
# 인스턴스가 정상 상태가 될 때까지 대기 | ||
aws elbv2 wait target-in-service --target-group-arn $TARGET_GROUP_ARN --targets Id=$INSTANCE_ID | ||
# 빌드된 JAR 파일을 EC2_HOST_2로 복사 | ||
- name: Copy JAR file to EC2_HOST_2 | ||
uses: appleboy/scp-action@master | ||
with: | ||
source: $WORKING_DIRECTORY/build/libs/*.jar | ||
target: /home/ubuntu/ | ||
host: $EC2_HOST_2 | ||
username: $EC2_USER | ||
key: $EC2_SSH_KEY | ||
# EC2_HOST_2에서 배포 및 대상 그룹 관리 | ||
- name: Deploy to EC2_HOST_2 | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: $EC2_HOST_2 | ||
username: $EC2_USER | ||
key: $EC2_SSH_KEY | ||
envs: | ||
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID | ||
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY | ||
AWS_REGION: $AWS_REGION | ||
TARGET_GROUP_ARN: $TARGET_GROUP_ARN | ||
INSTANCE_ID: $EC2_INSTANCE_ID_2 | ||
script: | | ||
# 대상 그룹에서 인스턴스 제거 | ||
aws elbv2 deregister-targets --target-group-arn $TARGET_GROUP_ARN --targets Id=$INSTANCE_ID | ||
# 기존 애플리케이션 종료 | ||
if screen -list | grep -q "\bbe\b"; then screen -S be -X quit; fi | ||
# 새로운 애플리케이션 실행 | ||
screen -dmS be java -jar /home/ubuntu/backend-server.jar | ||
# 애플리케이션 시작 대기 | ||
sleep 30 | ||
# 대상 그룹에 인스턴스 등록 | ||
aws elbv2 register-targets --target-group-arn $TARGET_GROUP_ARN --targets Id=$INSTANCE_ID | ||
# 인스턴스가 정상 상태가 될 때까지 대기 | ||
aws elbv2 wait target-in-service --target-group-arn $TARGET_GROUP_ARN --targets Id=$INSTANCE_ID |