|  | 
| 1 |  | -# It automatically builds your Docker image and pushes it to GHCR | 
| 2 |  | -name: Build and Push Docker Image | 
| 3 | 1 | 
 | 
| 4 |  | -# We will run this workflow every time you push to the 'main' branch | 
|  | 2 | +name: Build and Deploy Bot | 
|  | 3 | + | 
| 5 | 4 | on: | 
| 6 | 5 |   push: | 
| 7 | 6 |     branches: [ "main" ] | 
| 8 | 7 | 
 | 
| 9 | 8 | jobs: | 
| 10 |  | -  build-and-push: | 
| 11 |  | -    runs-on: ubuntu-latest  | 
|  | 9 | +  # ---------------------------------- | 
|  | 10 | +  # JOB 1: BUILD THE DOCKER IMAGE | 
|  | 11 | +  # ---------------------------------- | 
|  | 12 | +  build: | 
|  | 13 | +    runs-on: ubuntu-latest | 
| 12 | 14 |     permissions: | 
| 13 | 15 |       contents: read | 
| 14 |  | -      packages: write # Give the job permission to write to GHCR | 
|  | 16 | +      packages: write | 
| 15 | 17 | 
 | 
| 16 | 18 |     steps: | 
| 17 | 19 |       - name: Checkout repository | 
|  | 
| 30 | 32 |           username: ${{ github.actor }} | 
| 31 | 33 |           password: ${{ secrets.GITHUB_TOKEN }} | 
| 32 | 34 | 
 | 
| 33 |  | -      # Step to generate lowercase repository name | 
| 34 | 35 |       - name: Set lowercase repository name | 
| 35 | 36 |         run: echo "repo=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV | 
| 36 | 37 | 
 | 
|  | 
| 39 | 40 |         with: | 
| 40 | 41 |           context: . | 
| 41 | 42 |           file: ./Dockerfile | 
| 42 |  | -          platforms: linux/amd64,linux/arm64 # Build for multiple architectures | 
|  | 43 | +          platforms: linux/amd64,linux/arm64 | 
| 43 | 44 |           push: true | 
| 44 | 45 |           tags: | | 
| 45 | 46 |             ghcr.io/${{ env.repo }}:latest | 
| 46 | 47 |             ghcr.io/${{ env.repo }}:${{ github.sha }} | 
| 47 | 48 | 
 | 
|  | 49 | +  # ---------------------------------- | 
|  | 50 | +  # JOB 2: DEPLOY THE IMAGE TO THE VM | 
|  | 51 | +  # ---------------------------------- | 
|  | 52 | +  deploy: | 
|  | 53 | +    needs: build  | 
|  | 54 | +    runs-on: ubuntu-latest | 
|  | 55 | + | 
|  | 56 | +    steps: | 
|  | 57 | +      - name: Deploy to VM | 
|  | 58 | +        uses: appleboy/ssh-action@v1.0.3  | 
|  | 59 | +        with: | 
|  | 60 | +          host: ${{ secrets.VM_HOST }} | 
|  | 61 | +          username: ${{ secrets.VM_USER }} | 
|  | 62 | +          key: ${{ secrets.SSH_PRIVATE_KEY }} | 
|  | 63 | +          port: 22 | 
|  | 64 | +          script: | | 
|  | 65 | +            echo "--- Starting deployment ---" | 
|  | 66 | +             | 
|  | 67 | +            # Log in to GHCR (uses the GH_PAT secret) | 
|  | 68 | +            echo "${{ secrets.GH_PAT }}" | sudo docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin | 
|  | 69 | +             | 
|  | 70 | +            # Pull the new image (the one 'build' just created) | 
|  | 71 | +            sudo docker pull ghcr.io/rajat069/leetcode-reminder-bot:latest | 
|  | 72 | +             | 
|  | 73 | +            # Stop and remove the old container | 
|  | 74 | +            # '|| true' means "don't fail if the container doesn't exist" | 
|  | 75 | +            sudo docker stop leetcode-bot || true | 
|  | 76 | +            sudo docker rm leetcode-bot || true | 
|  | 77 | +             | 
|  | 78 | +            # Run the new container with all your flags | 
|  | 79 | +            sudo docker run \ | 
|  | 80 | +                --detach \ | 
|  | 81 | +                --restart=always \ | 
|  | 82 | +                --name leetcode-bot \ | 
|  | 83 | +                --dns=8.8.8.8 \ | 
|  | 84 | +                --env-file /home/admin-rj/leetcode-env.env \ | 
|  | 85 | +                -v /home/admin-rj/users.json:/app/users.json \ | 
|  | 86 | +                ghcr.io/rajat069/leetcode-reminder-bot:latest | 
|  | 87 | +             | 
|  | 88 | +            # Clean up old, unused images | 
|  | 89 | +            sudo docker image prune -f | 
|  | 90 | +             | 
|  | 91 | +            echo "--- Deployment successful! ---" | 
|  | 92 | +
 | 
0 commit comments