name: Build and Push Docker Images
run-name: Build Backend & Frontend by @${{ github.actor }}

on: [push]

env:
  # Passe dies an deine Registry an.
  # Wenn du die Gitea-interne Registry nutzt, ist es meist einfach der Hostname deiner Gitea-Instanz.
  # Beispiel: gitea.deine-domain.de
  REGISTRY_HOST: git.federspiel.tech
  # Der Name des Repos (z.B. user/repo)
  IMAGE_NAME: ${{ github.repository }}
  ACTOR: flfeders

jobs:
  build-backend:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3

      - name: Log in to Docker Registry
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY_HOST }}
          # Gitea stellt secrets.GITHUB_TOKEN automatisch bereit (wie GitLab CI_JOB_TOKEN)
          username: ${{ env.ACTOR }}
          password: ${{ vars.CI_TOKEN }}

      - name: Extract metadata (tags, labels) for Backend
        id: meta-backend
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY_HOST }}/${{ env.IMAGE_NAME }}/backend
          tags: |
            type=ref,event=branch
            type=ref,event=tag
            type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}

      - name: Build and push Backend
        uses: docker/build-push-action@v4
        with:
          context: ./backend # Hier wird der Ordner gewechselt (wie 'cd backend')
          push: true
          tags: ${{ steps.meta-backend.outputs.tags }}
          labels: ${{ steps.meta-backend.outputs.labels }}

  build-frontend:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3

      - name: Log in to Docker Registry
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY_HOST }}
          username: ${{ env.ACTOR }}
          password: ${{ vars.CI_TOKEN }}

      - name: Extract metadata (tags, labels) for Frontend
        id: meta-frontend
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY_HOST }}/${{ env.IMAGE_NAME }}/frontend
          tags: |
            type=ref,event=branch
            type=ref,event=tag
            type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}

      - name: Build and push Frontend
        uses: docker/build-push-action@v4
        with:
          context: ./frontend # Hier wird der Ordner gewechselt (wie 'cd frontend')
          push: true
          tags: ${{ steps.meta-frontend.outputs.tags }}
          labels: ${{ steps.meta-frontend.outputs.labels }}