name: publish-public-repo

on:
  push:
    branches: [ main, "rework/v2" ]
  workflow_dispatch:

jobs:
  deploy:
    # Pushes the maven repo layout to Pinont/singularity-maven (Pages-served,
    # anonymous reads). Accumulative: previously published versions are kept.
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup JDK 25
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: '25'
      - name: Stage maven repo layout
        run: mvn -q clean deploy -DskipTests -DaltDeploymentRepository=pages::default::file:${{ github.workspace }}/staging
      - name: Clone target repo (gh-pages branch)
        run: |
          git clone --depth 1 --branch gh-pages "https://x-access-token:${{ secrets.MAVEN_REPO_TOKEN }}@github.com/Pinont/singularity-maven.git" target || {
            cd target 2>/dev/null || git clone "https://x-access-token:${{ secrets.MAVEN_REPO_TOKEN }}@github.com/Pinont/singularity-maven.git" target
            cd target && git checkout -b gh-pages || true
          }
      - name: Sync artifacts in (accumulative)
        run: |
          mkdir -p target/repo
          cp -R staging/. target/repo/
          cd target
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add -A
          git diff --cached --quiet && echo "No changes" || git commit -m "publish ${{ github.sha }}"
          git push origin gh-pages
      - name: Trigger Pages rebuild
        run: |
          curl -s -X POST \
            -H "Authorization: Bearer ${{ secrets.MAVEN_REPO_TOKEN }}" \
            -H "Accept: application/vnd.github+json" \
            https://api.github.com/repos/Pinont/singularity-maven/pages/builds -o /dev/null -w "%{http_code}\n"
