name: CI

on:
  pull_request:
    types: [opened, synchronize, reopened, labeled]
  push:
    branches:
      - main
      - ci-work
  workflow_dispatch:

concurrency:
  group: ci-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  # Gate all builds through this check to prevent wasted runs.  Only run on
  # 'labeled' events when the label is actually 'ci:main'.  Concurrency control
  # above handles canceling the 'opened' event when 'labeled' arrives quickly
  # after (e.g., when creating a PR with ci:main already attached). See #1154.
  check-trigger:
    if: |
      startsWith(github.repository, 'kernelkit/') &&
      (github.event_name != 'pull_request' ||
       github.event.action != 'labeled' ||
       github.event.label.name == 'ci:main')
    runs-on: ubuntu-latest
    outputs:
      x86_64_target: ${{ steps.set-targets.outputs.x86_64_target }}
      aarch64_target: ${{ steps.set-targets.outputs.aarch64_target }}
      arm_target: ${{ steps.set-targets.outputs.arm_target }}
    steps:
      - run: |
          echo "Triggering build, logging meta data ..."
          echo "Event  : ${{ github.event_name }}"
          echo "Action : ${{ github.event.action }}"
          echo "Ref    : ${{ github.ref }}"
          echo "PR     : ${{ github.event.pull_request.number }}"
          echo "Label  : ${{ github.event.label.name }}"
      - id: set-targets
        run: |
          if [[ "${{ github.event_name }}" == "pull_request" ]] &&           \
             ! echo '${{ toJSON(github.event.pull_request.labels.*.name) }}' \
             | grep -q "ci:main"; then
            echo "x86_64_target=x86_64_minimal" >> $GITHUB_OUTPUT
            echo "aarch64_target=aarch64_minimal" >> $GITHUB_OUTPUT
            echo "arm_target=arm_minimal" >> $GITHUB_OUTPUT
          else
            echo "x86_64_target=x86_64" >> $GITHUB_OUTPUT
            echo "aarch64_target=aarch64" >> $GITHUB_OUTPUT
            echo "arm_target=arm" >> $GITHUB_OUTPUT
          fi
  unit-test:
    needs: check-trigger
    uses: ./.github/workflows/unit-test.yml
    with:
      name: "infix"
      target: ${{ needs.check-trigger.outputs.x86_64_target }}

  build-x86_64:
    needs: [check-trigger, unit-test]
    uses: ./.github/workflows/build.yml
    with:
      name: "infix"
      target: ${{ needs.check-trigger.outputs.x86_64_target }}

  build-aarch64:
    needs: [check-trigger, unit-test]
    uses: ./.github/workflows/build.yml
    with:
      name: "infix"
      target: ${{ needs.check-trigger.outputs.aarch64_target }}

  build-arm:
    needs: [check-trigger, unit-test]
    uses: ./.github/workflows/build.yml
    with:
      name: "infix"
      target: ${{ needs.check-trigger.outputs.arm_target }}

  test-run-x86_64:
    needs: [check-trigger, build-x86_64]
    uses: ./.github/workflows/test.yml
    with:
      target: ${{ needs.check-trigger.outputs.x86_64_target }}
      name: "infix"

  test-publish-x86_64:
    if: ${{ github.repository_owner == 'kernelkit' && github.ref_name == 'main' }}
    needs: test-run-x86_64
    uses: ./.github/workflows/publish.yml
