name: Publish Docker to GHCR

on:
  workflow_dispatch:
    inputs:
      stack_env:
        description: "stack env"
        required: true
        type: choice
        default: "dev"
        options:
          - dev
          - prod
          - stg
      tag:
        description: "Image tag (e.g. 1.0.0)"
        required: true
        default: "1.0.0"


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

jobs:
  publish:
    name: Build & Push to GHCR ${{ github.repository }}:${{ github.event.inputs.stack_env }}-${{ github.event.inputs.tag }}
    runs-on: ubuntu-latest
    environment: ${{ vars.PORTAINER_ENV || 'portainer' }}
    permissions:
      contents: read
      packages: write
    steps:
      - name: Free disk space
        run: |
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /usr/local/lib/android
          sudo rm -rf /opt/ghc
          sudo rm -rf /opt/hostedtoolcache/CodeQL
          sudo docker image prune --all --force
          df -h

      - name: Checkout branch ${{ github.event.inputs.stack_env }}
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.inputs.stack_env }}

      - name: Checkout scripts from main
        uses: actions/checkout@v4
        with:
          ref: main
          path: .ci
          sparse-checkout: .github/workflows/script

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

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

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

      - name: Generate image metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=${{ github.event.inputs.stack_env }}-${{ github.event.inputs.tag }}
            type=raw,value=${{ github.event.inputs.stack_env }}-latest

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/amd64
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          no-cache: true

      - name: Notify success
        if: success()
        run: bash ./.ci/.github/workflows/script/notify.sh
        env:
          TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
          TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
          NOTIFY_STATUS: success
          NOTIFY_WORKFLOW: "Publish Docker"
          NOTIFY_DETAIL: "Image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.event.inputs.stack_env }}-${{ github.event.inputs.tag }}"

      - name: Notify failure
        if: failure()
        run: bash ./.ci/.github/workflows/script/notify.sh
        env:
          TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
          TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
          NOTIFY_STATUS: failure
          NOTIFY_WORKFLOW: "Publish Docker"
          NOTIFY_DETAIL: "Image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.event.inputs.stack_env }}-${{ github.event.inputs.tag }}"
