Update docker-image.yml #2
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: Frontend CI/CD | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| pull_request: | |
| branches: [ "main" ] | |
| permissions: | |
| contents: read | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 18 | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm install | |
| - name: Build frontend | |
| run: npm run build | |
| - name: Log in to Docker Hub | |
| run: echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin | |
| - name: Build Docker image | |
| run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/frontend:latest . | |
| - name: Push Docker image to Docker Hub | |
| run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/frontend:latest | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| steps: | |
| - name: SSH into EC2 and deploy frontend | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.HOST }} | |
| username: ubuntu | |
| key: ${{ secrets.KEY }} | |
| script: | | |
| cd /home/ubuntu | |
| # ✅ 실행 중인 모든 컨테이너 정리 | |
| echo "🛑 Stopping all running containers..." | |
| sudo docker rm -f $(docker ps -qa) || true | |
| # ✅ 최신 이미지 가져오기 | |
| echo "🚀 Pulling the latest frontend Docker image..." | |
| sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/frontend:latest | |
| # ✅ 기존 컨테이너 종료 및 네트워크 정리 | |
| echo "🛑 Stopping and removing existing containers..." | |
| docker-compose down | |
| # ✅ 최신 이미지 적용 (docker-compose pull 강제 적용) | |
| echo "🚀 Updating to the latest frontend image..." | |
| docker-compose pull | |
| # ✅ 컨테이너 실행 (강제 재생성) | |
| echo "🚀 Starting the new container..." | |
| docker-compose up -d --build --force-recreate --no-deps | |
| # ✅ 사용하지 않는 이미지 정리 | |
| echo "🧹 Cleaning up old images..." | |
| docker image prune -f |