From d4c82f7ff1d576fc85f5f899a224ae5748902b78 Mon Sep 17 00:00:00 2001 From: "shdhkims@naver.com" <49316394+shdhkim@users.noreply.github.com> Date: Sat, 21 Dec 2024 18:02:28 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20=EB=B0=B0=ED=8F=AC=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 84 +++++++++++++----------------------- 1 file changed, 30 insertions(+), 54 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d7da060..2d32c02 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest steps: - # 1. 코드 체크아웃 + # 1. 소스코드 체크아웃 - name: Checkout code uses: actions/checkout@v4 @@ -26,14 +26,14 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt - # 4. Docker Hub 로그인 + # 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 이미지 빌드 및 푸시 (Flask) + # 5. Docker 이미지 빌드 & 푸시 (Flask) - name: Build and Push Flask Docker Image uses: docker/build-push-action@v4 with: @@ -41,76 +41,52 @@ jobs: push: true tags: ${{ secrets.DOCKER_USERNAME }}/flask-app:latest - # 6. EC2로 배포 (Docker Compose 방식) - - name: Deploy to EC2 via Docker Compose + # 6. SCP로 docker-compose.yml을 EC2에 전송 후 Docker Compose 실행 + - name: Deploy to EC2 (SCP + SSH) env: - HOST: 3.39.237.18 # EC2 퍼블릭 IP - USER: ${{ secrets.DEPLOY_USER }} # EC2 사용자 이름 - KEY: ${{ secrets.DEPLOY_KEY }} # SSH 개인 키 + HOST: 3.39.237.18 # EC2 퍼블릭 IP + USER: ${{ secrets.DEPLOY_USER }} # ec2-user 등 + KEY: ${{ secrets.DEPLOY_KEY }} # SSH 개인 키 run: | - # SSH 설정 + # 6-1) SSH 설정 mkdir -p ~/.ssh - echo "$KEY" > deploy_key.pem - chmod 400 deploy_key.pem - echo "Adding $HOST to known_hosts" - ssh-keyscan -H $HOST >> ~/.ssh/known_hosts || exit 1 - - # EC2 접속 및 Docker Compose 실행 - ssh -i deploy_key.pem $USER@$HOST <<'EOF' - # Docker 설치 확인 및 설치 + echo "$KEY" > ec2_key.pem + chmod 400 ec2_key.pem + ssh-keyscan -H $HOST >> ~/.ssh/known_hosts + + # 6-2) docker-compose.yml 파일을 EC2로 전송 (로컬 디렉터리 기준) + scp -i ec2_key.pem -o StrictHostKeyChecking=no \ + docker-compose.yml \ + $USER@$HOST:~/flask-milvus-app/docker-compose.yml + + # 6-3) EC2에서 Docker Compose 실행 + ssh -i ec2_key.pem $USER@$HOST << 'SSHEND' + # Docker 설치 if ! [ -x "$(command -v docker)" ]; then sudo yum update -y sudo yum install -y docker sudo service docker start fi - # Docker Compose 설치 확인 및 설치 + # Docker Compose 설치 if ! [ -x "$(command -v docker-compose)" ]; then - sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/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 fi # Docker Hub 로그인 sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} - # 프로젝트 디렉터리로 이동 + # 프로젝트 디렉터리 준비 mkdir -p ~/flask-milvus-app cd ~/flask-milvus-app - # docker-compose.yml 파일 복사 - cat > docker-compose.yml <<'COMPOSE' -version: '3.8' - -services: - flask-app: - container_name: flask-app - image: ${{ secrets.DOCKER_USERNAME }}/flask-app:latest - ports: - - "5000:5000" - depends_on: - - milvus - environment: - - MILVUS_HOST=milvus - - MILVUS_PORT=19530 - restart: always - - milvus: - image: milvusdb/milvus:latest - container_name: milvus - ports: - - "19530:19530" - volumes: - - milvus_data:/var/lib/milvus - environment: - - MILVUS_LOG_LEVEL=info - restart: always - -volumes: - milvus_data: -COMPOSE - # Docker Compose 실행 - sudo docker-compose down + sudo docker-compose down || true sudo docker-compose pull sudo docker-compose up -d --build - EOF \ No newline at end of file + + # 컨테이너 상태 + sudo docker-compose ps + EOF \ No newline at end of file