name: Release Artifacts to GitHub Maven Packages

on:
  release:
    types: [ created ]

jobs:
  build:

    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v5
      - name: Set up JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: '21'
          distribution: 'oracle'
          server-id: github
          settings-path: ${{ github.workspace }}

      - name: maven-settings-xml-action
        uses: whelk-io/maven-settings-xml-action@v22
        with:
          servers: '[{ "id": "${repo.id}", "username": "${repo.user}", "password": "${repo.pass}" }]'

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "ollama4j-revision"
          replace: ${{ github.ref_name }}
          regex: false

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "mvn-repo-id"
          replace: github
          regex: false

      - name: Import GPG key
        uses: crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}
      - name: List keys
        run: gpg -K

      - name: Build with Maven
        run: mvn --file pom.xml -U clean package -Punit-tests

      - name: Publish to GitHub Packages Apache Maven
        run: mvn deploy -Punit-tests -s $GITHUB_WORKSPACE/settings.xml -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }} -Drepo.id=github -Drepo.user=${{ secrets.GH_MVN_USER }} -Drepo.pass=${{ secrets.GH_MVN_PASS }} -DaltDeploymentRepository=github::default::https://maven.pkg.github.com/ollama4j/ollama4j
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}