Merge pull request #26 from ktb3-team4/feature/chat #59
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: Docker Build and Push | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - develop | |
| - feat/env | |
| pull_request: | |
| branches: | |
| - main | |
| - develop | |
| - feat/env | |
| env: | |
| IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/front | |
| IMAGE_TAG: latest | |
| jobs: | |
| discover-servers: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.set-matrix.outputs.matrix }} | |
| steps: | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ secrets.AWS_REGION }} | |
| - name: Get frontend servers from AWS | |
| id: set-matrix | |
| run: | | |
| SERVERS=$(aws ec2 describe-instances \ | |
| --filters "Name=tag:Role,Values=frontend" "Name=instance-state-name,Values=running" \ | |
| --query 'Reservations[*].Instances[*].[PublicIpAddress]' \ | |
| --output json | jq -c 'flatten | map(select(. != null))') | |
| echo "Discovered servers: $SERVERS" | |
| echo "matrix={\"server\":$SERVERS}" >> $GITHUB_OUTPUT | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Docker Hub Login | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_TOKEN }} | |
| - name: Build and Push Image | |
| run: | | |
| docker build \ | |
| --build-arg NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }} \ | |
| --build-arg NEXT_PUBLIC_SOCKET_URL=${{ secrets.NEXT_PUBLIC_SOCKET_URL }} \ | |
| --build-arg NEXT_PUBLIC_AWS_REGION=${{ secrets.AWS_REGION }} \ | |
| --build-arg NEXT_PUBLIC_BUCKET=${{ secrets.NEXT_PUBLIC_BUCKET }} \ | |
| --build-arg NEXT_PUBLIC_CLOUDFRONT_DOMAIN=${{ secrets.CLOUDFRONT_DOMAIN }} \ | |
| -t $IMAGE_NAME:$IMAGE_TAG . | |
| docker push $IMAGE_NAME:$IMAGE_TAG | |
| deploy: | |
| needs: [build-and-push, discover-servers] | |
| runs-on: ubuntu-latest | |
| # if: github.event_name == 'push' | |
| strategy: | |
| matrix: ${{ fromJson(needs.discover-servers.outputs.matrix) }} | |
| fail-fast: false # 한 서버 실패해도 다른 서버 계속 배포 | |
| steps: | |
| - name: Set up SSH | |
| uses: webfactory/ssh-agent@v0.9.0 | |
| with: | |
| ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| - name: Add known_hosts | |
| run: | | |
| mkdir -p ~/.ssh | |
| ssh-keyscan -H ${{ matrix.server }} >> ~/.ssh/known_hosts | |
| - name: Deploy to EC2 (${{ matrix.server }}) | |
| run: | | |
| ssh ${{ secrets.SERVER_USER }}@${{ matrix.server }} 'bash -s' <<EOF | |
| set -e | |
| DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }} | |
| DOCKER_TOKEN=${{ secrets.DOCKER_TOKEN }} | |
| IMAGE_NAME=\$DOCKER_USERNAME/front:latest | |
| echo ">>> Docker Hub login" | |
| echo "\$DOCKER_TOKEN" | docker login -u "\$DOCKER_USERNAME" --password-stdin | |
| echo ">>> Pull latest image: \$IMAGE_NAME" | |
| docker pull "\$IMAGE_NAME" | |
| echo ">>> Move to app directory" | |
| mkdir -p /home/${{ secrets.SERVER_USER }}/app | |
| cd /home/${{ secrets.SERVER_USER }}/app | |
| echo ">>> docker compose down (if exists)" | |
| docker compose -f /home/${{ secrets.SERVER_USER }}/app/docker-compose.yml down || true | |
| echo ">>> docker compose up -d" | |
| docker compose -f /home/${{ secrets.SERVER_USER }}/app/docker-compose.yml up -d | |
| echo ">>> Deployed!" | |
| EOF |