Skip to content

refactor: organization:tenant=1:1 관계 최종 수정 및 ERD md 파일 일부 수정 #38

refactor: organization:tenant=1:1 관계 최종 수정 및 ERD md 파일 일부 수정

refactor: organization:tenant=1:1 관계 최종 수정 및 ERD md 파일 일부 수정 #38

name: Deploy to EC2
on:
push:
branches: [develop, feature/cicd-ec2-setup]
workflow_dispatch:
jobs:
# CI 및 빌드
test-and-build:
runs-on: ubuntu-latest
services:
redis:
image: redis:7.0
ports:
- 6379:6379
options: >-
--health-cmd="redis-cli ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Cache Maven dependencies
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Run tests
run: mvn clean test
env:
SPRING_PROFILES_ACTIVE: test
SPRING_REDIS_HOST: 127.0.0.1
SPRING_REDIS_PORT: 6379
- name: Build JAR
run: mvn clean package -DskipTests
- name: Upload JAR artifact
uses: actions/upload-artifact@v4
with:
name: agenticcp-jar
path: target/*.jar
# EC2 배포
deploy:
needs: [test-and-build]
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Download JAR artifact
uses: actions/download-artifact@v4
with:
name: agenticcp-jar
path: ./artifact
- name: Check EC2 Connection
run: |
echo "EC2 연결 확인 중..."
if timeout 10s bash -c "</dev/tcp/${{ secrets.EC2_HOST }}/22"; then
echo "EC2 연결 성공"
echo "EC2_READY=true" >> $GITHUB_ENV
else
echo "EC2 연결 실패 - EC2가 중지된 상태입니다"
echo "EC2_READY=false" >> $GITHUB_ENV
fi
- name: Deploy to EC2
if: env.EC2_READY == 'true'
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
source: "./artifact/*.jar"
target: "/tmp/"
- name: Restart on EC2
if: env.EC2_READY == 'true'
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
timeout: 10m
script: |
cd /opt/agenticcp-core
# 최신 코드 pull
git fetch origin
if [ "${{ github.ref_name }}" = "develop" ]; then
git reset --hard origin/develop
elif [ "${{ github.ref_name }}" = "feature/cicd-ec2-setup" ]; then
git reset --hard origin/feature/cicd-ec2-setup
else
git reset --hard origin/${{ github.ref_name }}
fi
# JAR 파일을 target 디렉토리로 복사
mkdir -p target
cp /tmp/*.jar target/
rm -f /tmp/*.jar
# Docker 상태 확인
echo "=== Docker 상태 확인 ==="
docker ps -a
# 인프라 변경 감지 (docker-compose.yml, Dockerfile 등 변경 여부)
echo "=== 인프라 변경 감지 ==="
# 이전 커밋이 있는지 확인
if git rev-parse HEAD~1 >/dev/null 2>&1; then
CHANGED_FILES=$(git diff HEAD~1 --name-only)
if echo "$CHANGED_FILES" | grep -qE "(docker-compose\.yml|Dockerfile)"; then
echo "인프라 변경 감지됨 - 전체 서비스 재배포"
INFRA_CHANGED=true
else
echo "코드 변경만 감지됨 - app만 재배포"
INFRA_CHANGED=false
fi
else
# 첫 배포거나 커밋이 하나뿐인 경우
echo "첫 배포이거나 이전 커밋이 없음 - 전체 서비스 재배포"
INFRA_CHANGED=true
fi
if [ "$INFRA_CHANGED" = "true" ]; then
# 인프라 변경 시: 전체 서비스 재배포
echo "=== 기존 컨테이너 전체 중지 및 삭제 ==="
docker-compose down || true
# app만 Dockerfile.develop로 빌드
echo "=== app 이미지를 Dockerfile.develop로 빌드 ==="
docker build -f Dockerfile.develop -t agenticcp-core_app:latest .
echo "=== 전체 서비스 시작 ==="
docker-compose up -d
else
# 코드 변경만: app만 재배포
echo "=== 기존 app 컨테이너 중지 및 삭제 ==="
docker-compose stop app || true
docker rm -f agenticcp-app || true
# Maven 빌드 없이 바로 Docker 이미지 빌드 (JAR만 사용)
echo "=== Docker 이미지 빌드 ==="
# Dockerfile.develop을 사용하여 이미지 빌드
echo "=== Dockerfile.develop로 이미지 빌드 ==="
docker build -f Dockerfile.develop -t agenticcp-core_app:latest .
# app 컨테이너만 시작 (mysql, redis는 이미 실행 중)
docker-compose up -d app
fi
# 컨테이너 상태 확인
echo "=== 컨테이너 상태 확인 ==="
docker ps -a
# 애플리케이션 로그 확인
echo "=== 애플리케이션 로그 (최근 50줄) ==="
sleep 5
docker-compose logs app | tail -50
# 헬스체크 대기
echo "=== 헬스체크 시작 (최대 2분 대기) ==="
for i in {1..24}; do
echo "시도 $i/24..."
if curl -f http://localhost:8080/api/actuator/health 2>/dev/null; then
echo "헬스체크 성공!"
exit 0
fi
sleep 5
done
echo "헬스체크 실패 - 애플리케이션 로그 확인"
docker-compose logs app
exit 1