name: test-build
on:
  workflow_dispatch:
  pull_request:
  push:
  schedule:
    - cron: '30 3 * * 2'

concurrency:
  group: "${{ github.ref }}"
  cancel-in-progress: true
jobs:
  build-debian:
    strategy:
      # Keep other matrix jobs running, even if one fails.
      fail-fast: false
      matrix:
        host_release:
        - unstable
        - trixie
        - bookworm
        - bullseye

    # We want a working shell, qemu, python and docker. Specific version should not matter (much).
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - run: ./tests/gha-build-deb.sh
      name: "Build .deb for ${{matrix.host_release}}"
      env:
        HOST_RELEASE: ${{matrix.host_release}}

    - name: Archive built .deb
      uses: actions/upload-artifact@v4
      with:
        name: deb-${{matrix.host_release}}
        if-no-files-found: error
        path: |
          *.deb

  test-debian:
    needs: build-debian
    strategy:
      # Keep other matrix jobs running, even if one fails.
      fail-fast: false
      matrix:
        host_release:
        - unstable
        - trixie
        - bookworm
        - bullseye

        release:
        - trixie
        - bookworm
        - bullseye

        runner:
        - ubuntu-latest
        - ubuntu-24.04-arm

        exclude:
          # restrict arm tests to bookworm and newer, mostly to save time
          - runner: ubuntu-24.04-arm
            host_release: bullseye
          - runner: ubuntu-24.04-arm
            release: bullseye

    # We want a working shell, qemu, python and docker. Specific version should not matter (much).
    runs-on: ${{ matrix.runner }}

    steps:
    - uses: actions/checkout@v4

    - name: Download built deb
      uses: actions/download-artifact@v4
      with:
        name: deb-${{matrix.host_release}}

    - run: ./tests/build-vm-and-test.sh setup
      name: "Setup test environment"

    - run: ./tests/build-vm-and-test.sh run
      name: "Build VM image using grml-debootstrap on host ${{matrix.host_release}} for ${{matrix.release}}"
      env:
        HOST_RELEASE: ${{matrix.host_release}}
        RELEASE: ${{matrix.release}}

    - run: ./tests/build-vm-and-test.sh test
      id: build_vm_and_test_test
      name: "Test built VM image for ${{matrix.release}}"
      env:
        RELEASE: ${{matrix.release}}

    - name: Archive VM image on failure
      uses: actions/upload-artifact@v4
      if: always() && (steps.build_vm_and_test_test.outcome == 'failure')
      with:
        name: vm-image-${{matrix.host_release}}-${{matrix.release}}-${{matrix.runner}}
        if-no-files-found: error
        path: qemu.img
        retention-days: 5

    - name: Archive VM test results
      uses: actions/upload-artifact@v4
      if: always()
      with:
        name: vm-results-${{matrix.host_release}}-${{matrix.release}}-${{matrix.runner}}
        if-no-files-found: error
        path: tests/results/
