Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ var/
*.egg
venv/
.venv/
ENV/
ENV/

# 추가 : 로컬 벡터 DB 데이터 무시
chroma_storage/
101 changes: 99 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@ on:
push:
branches:
- main
workflow_dispatch: # 수동 트리거 옵션 추가

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest

- name: Run tests
run: |
pytest tests/ -v || echo "Warning: Some tests failed but continuing deployment"

deploy:
runs-on: ubuntu-latest
needs: test # 테스트 작업 완료 후 실행

steps:
- name: Checkout source
Expand All @@ -23,6 +46,45 @@ jobs:
run: |
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts

# - name: Create .env file
# run: |
# cat > .env << EOF
# OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}
# VECTOR_PERSIST_PATH=/app/data/chroma_storage
# EOF
#
# - name: Copy .env file to EC2
# run: |
# scp .env ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:~/algorithm-server/.env

- name: Check .env file without modifying
run: |
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
if [ -f ~/algorithm-server/.env ]; then
echo ".env 파일이 이미 존재합니다. 수정하지 않습니다."
# 민감한 정보 없이 어떤 환경 변수가 설정되어 있는지 확인
grep -v "PASSWORD\|KEY\|URI" ~/algorithm-server/.env | sort | grep "=" || echo "No variables found"
else
echo ".env 파일이 없습니다. 새로 생성이 필요할 수 있습니다."
fi
EOF

- name: Backup Vector DB
run: |
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
# 현재 날짜로 백업 디렉토리 생성
BACKUP_DIR="/home/${{ secrets.EC2_USER }}/backups/vectordb_$(date +%Y%m%d_%H%M%S)"
mkdir -p $BACKUP_DIR

# 기존 벡터 DB 데이터 백업
if [ -d "/home/${{ secrets.EC2_USER }}/algorithm-server/chroma_storage" ]; then
cp -r /home/${{ secrets.EC2_USER }}/algorithm-server/chroma_storage/* $BACKUP_DIR/ || echo "Vector DB backup failed but continuing"
echo "Vector DB backed up to $BACKUP_DIR"
else
echo "No vector DB found to backup"
fi
EOF

- name: SSH into EC2 and deploy
run: |
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
Expand All @@ -33,10 +95,45 @@ jobs:
cd algorithm-server

git pull origin main

# 벡터 DB 데이터 디렉토리 생성
mkdir -p /home/${{ secrets.EC2_USER }}/vector_db_data

# Docker 컨테이너 재시작
echo "Stopping previous container..."
sudo docker stop algo-container || true
sudo docker rm algo-container || true
sudo docker build -t algo-image .
sudo docker run -d --name algo-container -p 8000:8000 algo-image

echo "Building new image..."
sudo docker build -t algo-image:$(date +%Y%m%d) .
sudo docker tag algo-image:$(date +%Y%m%d) algo-image:latest

echo "Starting new container..."
sudo docker run -d --name algo-container \
-p 8000:8000 \
-v /home/${{ secrets.EC2_USER }}/vector_db_data:/app/data/chroma_storage \
--env-file .env \
--restart unless-stopped \
algo-image:latest

echo "Cleaning up old images..."
sudo docker image prune -af --filter "until=24h"

# 배포 확인
echo "Waiting for service to start..."
sleep 10
if curl -s http://localhost:8000/docs > /dev/null; then
echo "Deployment successful: Service is running"
else
echo "Warning: Service may not be running correctly"
fi
EOF

- name: Notify deployment status
if: always()
run: |
if [ "${{ job.status }}" == "success" ]; then
echo "Deployment completed successfully"
else
echo "Deployment failed or had warnings"
fi
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# 추가 : 벡터 DB 데이터 디렉토리 생성 및 권한 설정
RUN mkdir -p /app/data/chroma_storage && chmod -R 777 /app/data

# 필요한 패키지 설치
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip
Expand Down