name: Release on Merge to Master

on:
  push:
    branches:
      - master  # oder "main", je nach Repo
    paths-ignore:
      - '**.md'     # ignoriert reine Dokumentationsänderungen
      - '.github/**' # ignoriert Änderungen an Actions selbst

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write  # Berechtigung für Releases hinzufügen

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

      - name: Set up Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '21'

      - name: Build with Gradle
        run: ./gradlew clean build

      - name: Get version
        id: get_version
        run: |
          # Extrahiere die Version aus der build.gradle
          VERSION=$(./gradlew properties -q | grep "version:" | awk '{print $2}')
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.get_version.outputs.version }}
          name: "Release v${{ steps.get_version.outputs.version }}"
          body: |
            **v${{ steps.get_version.outputs.version }}**
            - Commit: ${{ github.sha }}
            - Branch: ${{ github.ref_name }}
            - Version: v${{ steps.get_version.outputs.version }}
          files: |
            build/libs/*.jar
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
