chore: 배포 테스트 #25
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: 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로 전송 | |
scp -i ec2_key.pem -o StrictHostKeyChecking=no -r $(pwd)/ $USER@$HOST:~/flask-milvus-app/ | |
# EC2에서 Docker Compose 실행 | |
ssh -i ec2_key.pem $USER@$HOST << 'EOF' | |
# 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 로그인 | |
sudo docker login -u "${DOCKER_USER}" -p "${DOCKER_PASS}" | |
# 프로젝트 디렉터리 이동 및 Docker Compose 실행 | |
cd ~/flask-milvus-app | |
sudo docker-compose down || true | |
sudo docker-compose pull | |
sudo docker-compose up -d --build | |
# 컨테이너 상태 확인 | |
sudo docker-compose ps | |
EOF |