Merge pull request #48 from Fac2Real/feature/FRB-187 #24
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: Build and Deploy to EC2 | |
| on: | |
| push: | |
| branches: | |
| - develop | |
| jobs: | |
| build-and-push-image: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v2 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'corretto' | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v2 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ap-northeast-2 | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v1 | |
| - name: Build, tag, and push image to ECR | |
| env: | |
| ECR_REGISTRY: ${{ secrets.AWS_ECR_REGISTRY }} | |
| ECR_REPOSITORY: ${{ secrets.AWS_ECR_REPOSITORY }} | |
| IMAGE_TAG: backend-latest | |
| run: | | |
| docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
| deploy-and-push-image: | |
| runs-on: ubuntu-latest | |
| needs: build-and-push-image | |
| steps: | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v2 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ap-northeast-2 | |
| - name: Login to Amazon ECR | |
| uses: aws-actions/amazon-ecr-login@v1 | |
| - name: Deploy to EC2 via SSH | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.AWS_EC2_HOST }} | |
| username: ${{ secrets.AWS_EC2_USER }} | |
| key: ${{ secrets.AWS_EC2_SSH_KEY }} | |
| script: | | |
| # ECR 로그인 | |
| aws ecr get-login-password --region ap-northeast-2 | \ | |
| docker login --username AWS --password-stdin 853660505909.dkr.ecr.ap-northeast-2.amazonaws.com | |
| # 기존 컨테이너 정리 | |
| cd datastream | |
| docker-compose -f docker-compose-service.yml down -v --rmi all | |
| # 새 이미지 풀 & 실행 | |
| docker-compose -f docker-compose-service.yml pull | |
| docker-compose -f docker-compose-service.yml up -d | |
| # 기존 이미지 재실행 | |
| docker-compose -f docker-compose-elk.yml down -v | |
| docker-compose -f docker-compose-elk.yml up -d |