name: Maven Release
on:
  workflow_dispatch:
    inputs:
      libToRelease:
        required: true
        type: string
        description: 'Library to release, e.g :jvm-libs:blob-compressor'
      version:
        required: true
        type: string
        description: 'Release semantic version: e.g "1.0.0"'

jobs:
  release:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-med
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

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

      # Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies.
      # See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@8379f6a1328ee0e06e2bb424dadb7b159856a326 # 4.4.0

      - name: Clean
        # ./gradlew clean is necessary because the build is cached
        # and cause issues with JReleaser
        run: ./gradlew clean

      - name: Build
        # ./gradlew clean is necessary because the build is cached
        # and cause issues with JReleaser
        run: ./gradlew ${{github.event.inputs.libToRelease}}:build
        env:
          GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN_RELEASE_ACCESS }}

      - name: Deploy to Local File System
        run: ./gradlew ${{github.event.inputs.libToRelease}}:publish -Pversion=${{github.event.inputs.version}}
        env:
          GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN_RELEASE_ACCESS }}


      - name: Build and Release to Maven Central
        # ./gradlew clean is necessary because the build is cached
        # and cause issues with JReleaser
        run: ./gradlew --stacktrace ${{github.event.inputs.libToRelease}}:jreleaserRelease -Pversion=${{github.event.inputs.version}}
        env:
          GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN_RELEASE_ACCESS }}
          JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          JRELEASER_GITHUB_USERNAME: ${{ github.actor }}
          JRELEASER_GITHUB_EMAIL: ${{ github.actor }}@users.noreply.github.com
          JRELEASER_GPG_PASSPHRASE: ${{ secrets.JRELEASER_GPG_PASSPHRASE }}
          JRELEASER_GPG_SECRET_KEY: ${{ secrets.JRELEASER_GPG_SECRET_KEY }}
          JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.JRELEASER_GPG_PUBLIC_KEY }}
          JRELEASER_MAVENCENTRAL_USERNAME: ${{ secrets.JRELEASER_MAVENCENTRAL_USERNAME }}
          JRELEASER_MAVENCENTRAL_PASSWORD: ${{ secrets.JRELEASER_MAVENCENTRAL_PASSWORD }}

      # Persist logs
      - name: JReleaser release output
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: jreleaser-release
          path: |
            build/jreleaser/trace.log
            build/jreleaser/output.properties
