name: Deploy images on release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
jobs:
  docker-build-and-push-client:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Extract version from tag
        id: extract_tag
        run: echo "version=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Client Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate-client:${{ steps.extract_tag.outputs.version }} \
            -f ./docker/dist/client.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            .

      - name: Push Client Docker image
        run: |
          docker push ghcr.io/bluewave-labs/checkmate-client:${{ steps.extract_tag.outputs.version }}

  docker-build-and-push-server:
    needs: docker-build-and-push-client
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Extract version
        id: extract_tag
        run: echo "version=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Server Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate-backend:${{ steps.extract_tag.outputs.version }} \
            -f ./docker/dist/server.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            .

      - name: Push Server Docker image
        run: |
          docker push ghcr.io/bluewave-labs/checkmate-backend:${{ steps.extract_tag.outputs.version }}

      - name: Build Mongo Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate-mongo:${{ steps.extract_tag.outputs.version }} \
            -f ./docker/dist/mongoDB.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            .

      - name: Push MongoDB Docker image
        run: |
          docker push ghcr.io/bluewave-labs/checkmate-mongo:${{ steps.extract_tag.outputs.version }}

  docker-build-and-push-server-mono-multiarch:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Extract version
        id: extract_tag
        run: echo "version=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push multi-arch Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./docker/dist-arm/server.Dockerfile
          push: true
          tags: |
            ghcr.io/bluewave-labs/checkmate-backend-mono-multiarch:${{ steps.extract_tag.outputs.version }}
          platforms: linux/amd64,linux/arm64
          labels: |
            org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate
          build-args: |
            VITE_APP_VERSION=${{ steps.extract_tag.outputs.version }}

  docker-build-and-push-server-mono:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Extract version
        id: extract_tag
        run: echo "version=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Server Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate-backend-mono:${{ steps.extract_tag.outputs.version }} \
            -f ./docker/dist-mono/server.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            --build-arg VITE_APP_VERSION=${{ steps.extract_tag.outputs.version }} \
            .

      - name: Push Server Docker image
        run: docker push ghcr.io/bluewave-labs/checkmate-backend-mono:${{ steps.extract_tag.outputs.version }}
