name: release-artifacts

on:
  workflow_dispatch:
    inputs:
      currentDevelopmentVersion:
        description: 'The current development version'
        required: true

jobs:
  release-build:
    name: Release artifacts
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
    strategy:
      matrix:
        os: [ubuntu-latest, ubuntu-24.04-arm]
        experimental: [ false ]
        include:
          - os: [ macos-latest ]
            experimental: true
      fail-fast: true

    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          ref: wanaku-${{ github.event.inputs.currentDevelopmentVersion }}

      - uses: graalvm/setup-graalvm@v1.3.4
        with:
          java-version: '21'
          distribution: 'graalvm'
          github-token: ${{ secrets.GITHUB_TOKEN }}
          native-image-job-reports: 'true'
          cache: maven
      - name: Set arch
        id: arch
        run: echo "arch=$(uname -m)" >> $GITHUB_OUTPUT
      - name: Login to Container Registry
        if: matrix.os == 'ubuntu-latest' || matrix.os == 'ubuntu-24.04-arm'
        uses: docker/login-action@v3
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_USERNAME }}
          password: ${{ secrets.QUAY_PASSWORD }}

      - name: Create a release build with container push (x86)
        if: matrix.os == 'ubuntu-latest'
        run: |
          mvn --no-transfer-progress -Pdist -Dnative -DskipTests \
          -Dquarkus.container-image.build=true \
          -Dquarkus.container-image.push=true \
          -Dquarkus.container-image.tag=${{ github.event.inputs.currentDevelopmentVersion }}-${{ steps.arch.outputs.arch }} \
          -Dquarkus.jib.platforms=linux/amd64 \
          clean package
      - name: Create a release build with container push (x86)
        if: matrix.os == 'ubuntu-24.04-arm'
        run: |
          mvn --no-transfer-progress -Pdist -Dnative -DskipTests \
          -Dquarkus.container-image.build=true \
          -Dquarkus.container-image.push=true \
          -Dquarkus.container-image.tag=${{ github.event.inputs.currentDevelopmentVersion }}-${{ steps.arch.outputs.arch }} \
          -Dquarkus.jib.platforms=linux/arm64/v8 \
          clean package
      - name: Create a release build with native executables (macos)
        if: matrix.os != 'ubuntu-latest' || matrix.os != 'ubuntu-24.04-arm'
        run: |
          mvn --no-transfer-progress -Pdist -Dnative -DskipTests clean package
      - name: Run JReleaser
        if: matrix.os == 'ubuntu-latest' || matrix.os == 'ubuntu-24.04-arm'
        uses: jreleaser/release-action@v2
        env:
          JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          JRELEASER_PROJECT_VERSION: ${{ github.event.inputs.currentDevelopmentVersion }}
          JRELEASER_SELECT_CURRENT_PLATFORM: true
          JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.GPG_PUBLIC_KEY }}
          JRELEASER_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
          JRELEASER_GPG_SECRET_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
      - name: Run JReleaser (Cli - macOS)
        if: matrix.os != 'ubuntu-latest' && matrix.os != 'ubuntu-24.04-arm'
        uses: jreleaser/release-action@v2
        with:
          arguments: 'full-release --exclude-distribution=cli'
        env:
          JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          JRELEASER_PROJECT_VERSION: ${{ github.event.inputs.currentDevelopmentVersion }}
          JRELEASER_SELECT_CURRENT_PLATFORM: true
          JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.GPG_PUBLIC_KEY }}
          JRELEASER_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
          JRELEASER_GPG_SECRET_KEY: ${{ secrets.GPG_PRIVATE_KEY }}

      # Persist logs

      - name: JReleaser release output
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: jreleaser-release-${{ matrix.os }}
          path: |
            out/jreleaser/trace.log
            out/jreleaser/output.properties

  createManifests:
    needs: release-build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Login to Container Registry
        if: github.repository == 'wanaku-ai/wanaku'
        uses: docker/login-action@v3
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_USERNAME }}
          password: ${{ secrets.QUAY_PASSWORD }}
      - name: Build and Publish (x86)
        if: github.repository == 'wanaku-ai/wanaku'
        run: |
          ./build-manifests.sh ${{ github.event.inputs.currentDevelopmentVersion }}