# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path

name: Release Artifacts to Maven Central

on:
  release:
    types: [ created ]


#on:
#  pull_request:
#    types: [ opened, reopened ]
#    branches: [ "main" ]


jobs:
  build:

    runs-on: ubuntu-latest

    permissions:
      contents: write
      packages: write

    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
          settings-path: ${{ github.workspace }} # location for the settings.xml file

      - 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: 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: 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: central
          regex: false

      - name: Publish to Maven Central
        run: mvn deploy -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }} -Drepo.id=central -Drepo.user=${{ secrets.MVN_USER }} -Drepo.pass=${{ secrets.MVN_PASS }}

      - name: Upload Release Asset - JAR
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: target/ollama4j-${{ github.ref_name }}.jar
          asset_name: ollama4j-${{ github.ref_name }}.jar
          asset_content_type: application/x-jar

      - name: Upload Release Asset - Javadoc JAR
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: target/ollama4j-${{ github.ref_name }}-javadoc.jar
          asset_name: ollama4j-${{ github.ref_name }}-javadoc.jar
          asset_content_type: application/x-jar

      - name: Upload Release Asset - Sources JAR
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: target/ollama4j-${{ github.ref_name }}-sources.jar
          asset_name: ollama4j-${{ github.ref_name }}-sources.jar
          asset_content_type: application/x-jar