release 오류 해결 #39
This file contains hidden or 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 to AWS EC2 | |
| on: | |
| push: | |
| branches: [ "release" ] # release 브랜치에 push가 발생했을 때 CI | |
| workflow_dispatch: | |
| env: | |
| IMAGE_NAME: ${{ secrets.ECR_REPO }} | |
| ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| AWS_REGION: ${{ secrets.AWS_REGION }} | |
| steps: | |
| # 소스코드 체크아웃 | |
| - name: Checkout source | |
| uses: actions/checkout@v3 | |
| # Docker buildx 설치 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| # 이전에 빌드한 캐시 불러오기 | |
| - name: Cache Docker layers | |
| uses: actions/cache@v4 | |
| with: | |
| key: ${{ runner.os }}-buildx-${{ github.sha }} | |
| path: /tmp/.buildx-cache | |
| restore-keys: ${{ runner.os }}-buildx- | |
| # ECR 로그인 | |
| - name: Login to Amazon ECR | |
| uses: aws-actions/amazon-ecr-login@v1 | |
| # Docker 이미지 빌드 및 ECR에 푸시 | |
| - name: Build and Push Docker Image | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ env.ECR_REGISTRY }}/${{ env.IMAGE_NAME }}:latest | |
| cache-from: type=local,src=/tmp/.buildx-cache # 기존 캐시 사용 | |
| cache-to: type=local,dest=/tmp/.buildx-cache-new # 새 캐시 저장 | |
| - name: Deploy to EC2 via SSH | |
| uses: appleboy/ssh-action@v1.0.0 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ec2-user | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ env.ECR_REGISTRY }} | |
| cp ~/app-path/.env.prod ./.env | |
| docker-compose -f docker-compose.prod.yml down | |
| docker-compose -f docker-compose.prod.yml pull | |
| docker-compose -f docker-compose.prod.yml run --rm -e MODE=prod fastapi alembic upgrade head | |
| docker-compose -f docker-compose.prod.yml up -d |