on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build-and-push:
    name: 🐳 Build & Push Docker Image with Commit Hash Tag
    runs-on: docker

    steps:
      - name: 📦 Cloner le dépôt
        uses: actions/checkout@v3

      - name: ⚙️ Récupérer le hash court du commit
        id: vars
        run: echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITEA_OUTPUT

      - name: 🔐 Connexion au registre Docker (Gitea)
        uses: docker/login-action@v3
        with:
          registry: ${{ vars.REGISTRY_URL }}
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}

      - name: ⚙️ Préparer Buildx
        uses: docker/setup-buildx-action@v3

      - name: 🐳 Construire & pousser l’image avec Buildx
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: |
            ${{ vars.REGISTRY_URL }}/${{ vars.REGISTRY_REPOSITORY }}:${{ steps.vars.outputs.COMMIT_HASH }}
            ${{ vars.REGISTRY_URL }}/${{ vars.REGISTRY_REPOSITORY }}:latest
          platforms: linux/amd64
