name: Create & Publish Docker Image

on:
  push:
    tags:
      - "v*.*.*"

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

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

      - name: Log In to the Container Registry
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Node 22.x
        uses: actions/setup-node@v4
        with:
          node-version: 22.x

      - name: Install Dependencies
        run: pnpm install
      
      - name: Build the Prod Docker Image
        if: github.ref == 'refs/heads/main'
        run: |
          TAG="${GITHUB_REF_NAME:1}"
          echo "Using tag: $TAG"
          export TAG
          pnpm run docker:build:push
      - name: Build the Beta Docker Image
        if: github.ref != 'refs/heads/main'
        run: |
          TAG="${GITHUB_REF_NAME:1}"
          echo "Using tag: $TAG"
          export TAG
          pnpm run docker:build-beta:push
      
        
