name: Publish Image

on:
  workflow_call:
    inputs:
      build_mode:
        description: "Build mode (development or production)"
        type: string
        default: production
  workflow_dispatch:
    inputs:
      build_mode:
        description: "Build mode (development or production)"
        type: string
        default: production

env:
  REGISTRY: git.labs.respellion.tech
  IMAGE: respellion/learning-platform/learning-platform

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Login to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: respellion-bot
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE }}
          tags: |
            type=sha,format=short,prefix=
            type=raw,value=latest
            type=semver,pattern={{version}}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            BUILD_MODE=${{ inputs.build_mode }}
