진짜 클라이언트 ip 주소 남기게 변경 (#32) #32
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
| # .github/workflows/frontend-cicd.yml | |
| name: Frontend Build and Push | |
| on: | |
| push: | |
| branches: | |
| - '08-cicd' | |
| paths: | |
| - 'leafy-frontend/**' | |
| - '.github/workflows/leafy-frontend-build-and-push.yml' | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| sha_tag: ${{ steps.get_sha.outputs.sha }} | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Get short commit sha | |
| id: get_sha | |
| run: echo "sha=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and Push Docker Image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./leafy-frontend | |
| file: ./leafy-frontend/Dockerfile | |
| push: true | |
| tags: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ steps.get_sha.outputs.sha }},${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:latest | |
| platforms: linux/amd64 | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Deploy to EC2 instance | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
| script: | | |
| echo "Updating .env file for frontend deployment..." | |
| # 1. .env 파일에서 프론트엔드 이미지 태그 라인만 찾아 교체합니다. | |
| sed -i "s|^FRONTEND_IMAGE_TAG=.*|FRONTEND_IMAGE_TAG=${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ needs.build.outputs.sha_tag }}|" .env | |
| # 2. 만약 .env 파일에 해당 라인이 없다면 추가해줍니다. | |
| if ! grep -q "FRONTEND_IMAGE_TAG=" .env; then | |
| echo "FRONTEND_IMAGE_TAG=${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ needs.build.outputs.sha_tag }}" >> .env | |
| fi | |
| echo "Current .env file content:" | |
| cat .env # 변경된 내용 확인 (디버깅에 유용!) | |
| # 3. Docker 로그인 후 프론트엔드 서비스만 재시작합니다. | |
| sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_TOKEN }}" | |
| sudo docker-compose pull leafy-front | |
| sudo docker-compose up -d --no-deps leafy-front # <-- 중요! | |
| # 4. 불필요한 이미지 정리 | |
| sudo docker image prune -af |