name: Deploy to blazor.radzen.com

on:
  workflow_dispatch:

concurrency:
  group: blazor-radzen-prod
  cancel-in-progress: true

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    permissions:
      contents: read
      packages: write

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

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Compute image tag (timestamp)
        id: meta
        run: |
          TAG=$(date -u +"%Y%m%d%H%M%S")
          IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${TAG}"
          echo "image=$IMAGE" >> "$GITHUB_OUTPUT"

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

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ steps.meta.outputs.image }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Push to dokku
        uses: dokku/github-action@master
        with:
          git_remote_url: ${{ secrets.DOKKU_REPO }}
          ssh_private_key: ${{ secrets.DOKKU_SSH_PRIVATE_KEY }}
          deploy_docker_image: ${{ steps.meta.outputs.image }}

      - name: Prune GHCR versions (keep 1)
        uses: actions/delete-package-versions@v5
        with:
          package-name: radzen-blazor
          package-type: container
          min-versions-to-keep: 1
          delete-only-pre-release-versions: false
