name: Publish Java package to the Maven Central Repository

on:
  push:
    tags:
      - v*-java
  workflow_dispatch:

jobs:
  publish:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: java/
    steps:
      - uses: actions/checkout@v4
      - name: Set up Java version
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: 8
          cache: 'gradle'
      - name: Import GPG Key
        run: |
          mkdir -p ~/.gnupg/
          printf "${GPG_SIGNING_KEY}" | base64 --decode > ~/.gnupg/private.key
          md5sum ~/.gnupg/private.key
          gpg --import --no-tty --batch --yes ~/.gnupg/private.key
          echo use-agent >> ~/.gnupg/gpg.conf
          echo pinentry-mode loopback >> ~/.gnupg/gpg.conf
          echo allow-loopback-pinentry >> ~/.gnupg/gpg-agent.conf
          echo RELOADAGENT | gpg-connect-agent
          gpg --list-keys
        env:
          GPG_SIGNING_KEY: ${{ secrets.GPG_SIGNING_KEY }}
      - name: Publish to Maven Central with Gradle
        env:
          OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
          OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }}
          GPG_KEYNAME: ${{ secrets.GPG_KEYNAME }}
          GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
        run: ./gradlew :capi-param-builder:publishToSonatype closeSonatypeStagingRepository
      - name: Maven central approval
        run:
          echo "Please login to https://central.sonatype.com/ and approve the staging repository"
