Feature: 코드 리팩토링(#71) #129
Workflow file for this run
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 MySQL and EC2 Deploy | |
on: | |
push: | |
branches: [ "develop" ] | |
pull_request: | |
branches: [ "develop" ] | |
jobs: | |
backend-CI-and-Deploy: | |
runs-on: ubuntu-latest | |
services: | |
db: | |
image: mysql:8.0 | |
env: | |
MYSQL_ROOT_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
MYSQL_DATABASE: aimo | |
MYSQL_USER: ${{ secrets.DB_USERNAME }} | |
MYSQL_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
ports: | |
- 3306:3306 | |
options: --health-cmd="mysqladmin ping -h localhost" --health-interval=10s --health-timeout=5s --health-retries=10 | |
steps: | |
# 소스 코드 체크아웃 | |
- uses: actions/checkout@v4 | |
# JDK 17 설정 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# YML 파일 생성 | |
- name: YML 파일 생성 | |
working-directory: . | |
run: | | |
# backend 디렉토리 생성 | |
mkdir -p backend/src/main/resources/ | |
# application.yml 파일들 생성 | |
echo "${{ secrets.APPLICATION_DEFAULT }}" > backend/src/main/resources/application.yml | |
echo "${{ secrets.APPLICATION_DEV }}" > backend/src/main/resources/application-dev.yml | |
echo "${{ secrets.APPLICATION_LOCAL }}" > backend/src/main/resources/application-local.yml | |
echo "${{ secrets.APPLICATION_PROD }}" > backend/src/main/resources/application-prod.yml | |
# github actions 시간대 확인 | |
echo =================== | |
date | |
echo =================== | |
# Gradle 실행 권한 설정 | |
- name: Grant execute permission to gradlew | |
run: chmod +x gradlew | |
working-directory: ${{ secrets.WORKING_DIRECTORY }} | |
# Gradle 빌드 및 테스트 실행 (로컬 컨테이너의 MySQL 사용) | |
- name: Build and Test with Gradle | |
run: ./gradlew clean build -Dspring.profiles.active=local | |
working-directory: ${{ secrets.WORKING_DIRECTORY }} | |
env: | |
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/aimo | |
SPRING_DATASOURCE_USERNAME: admin | |
SPRING_DATASOURCE_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
# 빌드된 JAR 파일을 EC2로 복사 | |
- name: Copy JAR file to EC2 | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.EC2_HOST_DEV }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
debug: true | |
source: ${{ secrets.WORKING_DIRECTORY }}/build/libs/*.jar | |
target: /home/ubuntu/ | |
# CloudWatch 설정 파일을 EC2로 복사 | |
- name: Copy CloudWatch config to EC2 | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.EC2_HOST_DEV }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
source: cloudwatch-config.json | |
target: /home/ubuntu/ | |
# EC2 연결 테스트 추가 (debug 옵션 활성화) | |
- name: Test EC2 SSH Connection | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST_DEV }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
debug: true | |
script: | | |
echo "SSH 연결 테스트 성공" | |
# EC2에서 애플리케이션 실행 (screen 세션 사용) | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST_DEV }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
debug: true | |
script: | | |
# CloudWatch 에이전트를 설정 파일로 시작 | |
sudo amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/home/ubuntu/cloudwatch-config.json -s | |
# 기존 screen 세션이 있다면 종료 | |
if screen -list | grep -q "\bbe\b"; then screen -S be -X quit; fi | |
# 새로운 screen 세션으로 애플리케이션 실행 | |
screen -dmS be java -jar /home/ubuntu/backend/build/libs/backend-server.jar --spring.profiles.active=dev |