# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Build Main

on:
  workflow_dispatch:
  push:
    branches:
        - main
        - release-prep
        - ci-**
    tags:
      - wanaku-*
    paths-ignore:
      - .github/**
      - docs/**
      - artwork/**
      - README.md
      - docker-*
      - index.md
      - jbang-catalog.json
      - Makefile
      - LICENSE
      - CLAUDE.md
      - CONTRIBUTING.md
      - jreleaser.yml
      - glama.json
      - SECURITY.md
      - .*

env:
  PROJECTS: ${{ github.workspace }}
  CONTAINER_REGISTRY: ${{ vars.CONTAINER_REGISTRY || 'quay.io' }}
  CONTAINER_GROUP: ${{ vars.CONTAINER_GROUP || 'wanaku' }}

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, ubuntu-24.04-arm]
    runs-on: ${{ matrix.os }}
    env:
      QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }}
      QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }}
    steps:
    - uses: actions/checkout@v6
    - name: Set up JDK 21
      uses: actions/setup-java@v5
      with:
        java-version: '21'
        distribution: 'temurin'
        cache: maven
    - name: Checkout Wanaku Capabilities SDK Main Project
      uses: actions/checkout@v6
      with:
        repository: wanaku-ai/wanaku-capabilities-java-sdk
        persist-credentials: false
        ref: main
        path: wanaku-capabilities-java-sdk
    - name: Build Wanaku Capabilities SDK Main Project
      run: mvn --no-transfer-progress -DskipTests clean install
      working-directory: ${{ github.workspace }}/wanaku-capabilities-java-sdk
    - name: Set arch
      id: arch
      run: echo "arch=$(uname -m)" >> $GITHUB_OUTPUT
    - name: Login to Container Registry
      if: env.QUAY_USERNAME != '' && env.QUAY_PASSWORD != ''
      uses: docker/login-action@v3
      with:
        registry: quay.io
        username: ${{ secrets.QUAY_USERNAME }}
        password: ${{ secrets.QUAY_PASSWORD }}
    - name: Build and Publish (x86)
      if: matrix.os == 'ubuntu-latest' && env.QUAY_USERNAME != '' && env.QUAY_PASSWORD != ''
      run: |
        mvn -B --no-transfer-progress clean package \
          -Dquarkus.container-image.build=true \
          -Dquarkus.container-image.push=true \
          -Dquarkus.container-image.group=${{ env.CONTAINER_GROUP }} \
          -Dquarkus.container-image.tag=${{ github.ref_name }}-${{ steps.arch.outputs.arch }} \
          -Dquarkus.jib.platforms=linux/amd64 \
          -Pdist
    - name: Build and Publish (arm)
      if: matrix.os == 'ubuntu-24.04-arm' && env.QUAY_USERNAME != '' && env.QUAY_PASSWORD != ''
      run: |
        mvn -B --no-transfer-progress clean package \
          -Dquarkus.container-image.build=true \
          -Dquarkus.container-image.push=true \
          -Dquarkus.container-image.group=${{ env.CONTAINER_GROUP }} \
          -Dquarkus.container-image.tag=${{ github.ref_name }}-${{ steps.arch.outputs.arch }} \
          -Dquarkus.jib.platforms=linux/arm64/v8 \
          -Pdist


  createManifests:
    needs: build
    runs-on: ubuntu-latest
    env:
      QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }}
      QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }}
    steps:
      - uses: actions/checkout@v6
      - name: Login to Container Registry
        if: env.QUAY_USERNAME != '' && env.QUAY_PASSWORD != ''
        uses: docker/login-action@v3
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_USERNAME }}
          password: ${{ secrets.QUAY_PASSWORD }}
      - name: Create and push multi-arch manifests
        if: env.QUAY_USERNAME != '' && env.QUAY_PASSWORD != ''
        run: |
          ./build-manifests.sh ${{ github.ref_name }} ${{ env.CONTAINER_REGISTRY }}/${{ env.CONTAINER_GROUP }}
