Switch Dockerfiles to use npm #3
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 Push to DockerHub | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| env: | |
| DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to DockerHub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Extract metadata for frontend | |
| id: meta-frontend | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ secrets.DOCKERHUB_USERNAME }}/collabydraw-frontend | |
| tags: | | |
| type=sha,prefix={{branch}}- | |
| type=ref,event=branch | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - name: Extract metadata for websocket | |
| id: meta-websocket | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ secrets.DOCKERHUB_USERNAME }}/collabydraw-websocket | |
| tags: | | |
| type=sha,prefix={{branch}}- | |
| type=ref,event=branch | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - name: Build and push Frontend image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile.frontend | |
| push: true | |
| tags: ${{ steps.meta-frontend.outputs.tags }} | |
| labels: ${{ steps.meta-frontend.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build and push WebSocket image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile.websocket | |
| push: true | |
| tags: ${{ steps.meta-websocket.outputs.tags }} | |
| labels: ${{ steps.meta-websocket.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Image digest | |
| run: | | |
| echo "Frontend: ${{ steps.meta-frontend.outputs.tags }}" | |
| echo "WebSocket: ${{ steps.meta-websocket.outputs.tags }}" | |
| deploy-to-ec2: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Create .env file for EC2 | |
| run: | | |
| cat > .env << EOF | |
| DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }} | |
| IMAGE_TAG=latest | |
| DB_NAME=${{ secrets.DB_NAME }} | |
| DB_USER=${{ secrets.DB_USER }} | |
| DB_PASSWORD=${{ secrets.DB_PASSWORD }} | |
| JWT_SECRET=${{ secrets.JWT_SECRET }} | |
| NEXTAUTH_SECRET=${{ secrets.NEXTAUTH_SECRET }} | |
| NEXTAUTH_URL=${{ secrets.NEXTAUTH_URL }} | |
| NEXT_PUBLIC_WS_URL=${{ secrets.NEXT_PUBLIC_WS_URL }} | |
| NEXT_PUBLIC_BASE_URL=${{ secrets.NEXT_PUBLIC_BASE_URL }} | |
| EOF | |
| - name: Copy files to EC2 | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| source: "docker-compose.yml,.env" | |
| target: "/home/${{ secrets.EC2_USER }}/collabydraw" | |
| - name: Deploy to EC2 | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| cd /home/${{ secrets.EC2_USER }}/collabydraw | |
| echo " Pulling latest images from DockerHub..." | |
| docker-compose pull | |
| echo " Stopping old containers..." | |
| docker-compose down | |
| echo " Starting new containers..." | |
| docker-compose up -d | |
| echo " Waiting for services to start..." | |
| sleep 15 | |
| echo " Cleaning up old images..." | |
| docker image prune -af --filter "until=24h" | |
| echo " Deployment completed!" | |
| echo " Container status:" | |
| docker-compose ps | |
| echo " Recent logs:" | |
| docker-compose logs --tail=50 | |
| - name: Verify deployment | |
| run: | | |
| echo " Deployment to EC2 completed successfully!" | |
| echo " Application should be running at: ${{ secrets.NEXT_PUBLIC_BASE_URL }}" |