Skip to content

Update gradle-publish.yml #154

Update gradle-publish.yml

Update gradle-publish.yml #154

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 }}
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
# JDK 17 설정
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# Gradle 실행 권한 설정
- name: Grant execute permission to gradlew
run: chmod +x gradlew
working-directory: backend
# Gradle 빌드
- name: Build and Test with Gradle
run: ./gradlew clean build
working-directory: backend
env:
SPRING_DATASOURCE_URL: jdbc:mysql://aimodb.cbimeayem48r.ap-northeast-2.rds.amazonaws.com:3306/aimo
SPRING_DATASOURCE_USERNAME: admin
SPRING_DATASOURCE_PASSWORD: ${{ secrets.DB_PASSWORD }}
# 빌드된 JAR 파일을 EC2_HOST_1로 복사
- name: Copy JAR file to EC2_HOST_1
uses: appleboy/scp-action@master
with:
source: backend/build/libs/*.jar
target: /home/ubuntu/
host: ${{ secrets.EC2_HOST_1 }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
# EC2_HOST_1에서 배포 및 대상 그룹 관리
# 빌드된 JAR 파일을 EC2_HOST_1로 복사
- name: Copy JAR file to EC2_HOST_1
uses: appleboy/scp-action@master
with:
source: backend/build/libs/*.jar
target: /home/ubuntu/
host: ${{ secrets.EC2_HOST_1 }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
# EC2_HOST_1에서 배포 및 대상 그룹 관리
- name: Deploy to EC2_HOST_1
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST_1 }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
envs: |
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 }}
INSTANCE_ID=${{ secrets.EC2_INSTANCE_ID_1 }}
script: |
# 변수 값 확인
echo "TARGET_GROUP_ARN: $TARGET_GROUP_ARN"
echo "INSTANCE_ID: $INSTANCE_ID"
# 대상 그룹에서 인스턴스 제거
if [ -z "$TARGET_GROUP_ARN" ]; then
echo "Error: TARGET_GROUP_ARN is empty"
exit 1
fi
if [ -z "$INSTANCE_ID" ]; then
echo "Error: INSTANCE_ID is empty"
exit 1
fi
echo "Deregistering instance $INSTANCE_ID from target group $TARGET_GROUP_ARN"
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
# 새로운 애플리케이션 실행
echo "Starting new application instance"
screen -dmS be java -jar /home/ubuntu/backend-server.jar
# 애플리케이션 시작 대기
sleep 30
# 대상 그룹에 인스턴스 등록
echo "Registering instance $INSTANCE_ID to target group $TARGET_GROUP_ARN"
aws elbv2 register-targets --target-group-arn "$TARGET_GROUP_ARN" --targets Id="$INSTANCE_ID"
# 인스턴스가 정상 상태가 될 때까지 대기
echo "Waiting for instance $INSTANCE_ID to be in service in target group $TARGET_GROUP_ARN"
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: backend/build/libs/*.jar
target: /home/ubuntu/
host: ${{ secrets.EC2_HOST_2 }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
# EC2_HOST_2에서 배포 및 대상 그룹 관리
- name: Deploy to EC2_HOST_2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST_2 }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
envs: |
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 }}
INSTANCE_ID=${{ secrets.EC2_INSTANCE_ID_2 }}
script: |
# 대상 그룹에서 인스턴스 제거
echo "Deregistering instance $INSTANCE_ID from target group $TARGET_GROUP_ARN"
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
# 새로운 애플리케이션 실행
echo "Starting new application instance"
screen -dmS be java -jar /home/ubuntu/backend-server.jar
# 애플리케이션 시작 대기
sleep 30
# 대상 그룹에 인스턴스 등록
echo "Registering instance $INSTANCE_ID to target group $TARGET_GROUP_ARN"
aws elbv2 register-targets --target-group-arn "$TARGET_GROUP_ARN" --targets Id="$INSTANCE_ID"
# 인스턴스가 정상 상태가 될 때까지 대기
echo "Waiting for instance $INSTANCE_ID to be in service in target group $TARGET_GROUP_ARN"
aws elbv2 wait target-in-service --target-group-arn "$TARGET_GROUP_ARN" --targets Id="$INSTANCE_ID"