Skip to content

chore: 배포 테스트 #29

chore: 배포 테스트

chore: 배포 테스트 #29

Workflow file for this run

name: Deploy Flask and Milvus App
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
## 1. 소스코드 체크아웃
- name: Checkout Source Code
uses: actions/checkout@v4
## 2. Python 3.11 설정
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'
## 3. Python 의존성 설치
- name: Install Python Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
## 4. Docker Hub 로그인
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
## 5. Docker 이미지 빌드 & 푸시
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/flask-app:latest
## 6. EC2에 배포 (SCP + SSH)
- name: Deploy to EC2 (SCP + SSH)
env:
HOST: 3.39.237.18
USER: ${{ secrets.DEPLOY_USER }}
KEY: ${{ secrets.DEPLOY_KEY }}
DOCKER_USER: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASS: ${{ secrets.DOCKER_PASSWORD }}
run: |
# SSH 설정
mkdir -p ~/.ssh
echo "$KEY" > ec2_key.pem
chmod 400 ec2_key.pem
ssh-keyscan -H $HOST >> ~/.ssh/known_hosts
# 전체 프로젝트를 EC2로 전송 (에러 방지 및 정확한 복사)
rsync -avz -e "ssh -i ec2_key.pem -o StrictHostKeyChecking=no" $(pwd)/* $USER@$HOST:~/flask-milvus-app/
# EC2에서 Docker Compose 실행
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=5 -i ec2_key.pem $USER@$HOST << 'EOF'
set -e
# Docker 설치 (설치되어 있지 않은 경우)
if ! [ -x "$(command -v docker)" ]; then
echo "Docker가 설치되어 있지 않음. 설치 중..."
sudo yum update -y
sudo yum install -y docker
sudo service docker start
else
echo "Docker가 이미 설치되어 있습니다."
fi
# Docker Compose 설치 (설치되어 있지 않은 경우)
if ! [ -x "$(command -v docker-compose)" ]; then
echo "Docker Compose가 설치되어 있지 않음. 설치 중..."
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
else
echo "Docker Compose가 이미 설치되어 있습니다."
fi
# Docker Hub 로그인
echo "Docker Hub에 로그인합니다..."
sudo docker login -u "${DOCKER_USER}" -p "${DOCKER_PASS}"
# 프로젝트 디렉터리 이동 및 Docker Compose 실행
echo "Docker Compose 실행..."
cd ~/flask-milvus-app
sudo docker-compose down || true
sudo docker-compose pull
sudo docker-compose up -d --build --timeout 600
# 컨테이너 상태 확인
echo "컨테이너 상태를 확인합니다."
sudo docker-compose ps
# HEALTHCHECK 검증
echo "HEALTHCHECK 검증 중..."
if [[ $(sudo docker inspect --format='{{.State.Health.Status}}' flask-app) != "healthy" ]]; then
echo "ERROR: Flask 컨테이너가 healthy 상태가 아닙니다!"
sudo docker-compose logs flask-app
exit 1
fi
EOF