---
# vi: ts=2 sw=2 et:
# SPDX-License-Identifier: LGPL-2.1-or-later
# Simple boot tests that build and boot the mkosi images generated by the mkosi config files in mkosi.default.d/.
name: mkosi

on:
  push:
    branches:
      - main
      - v[0-9]+-stable
  pull_request:
    branches:
      - main
      - v[0-9]+-stable

permissions:
  contents: read

env:
  # Enable debug logging in systemd, but keep udev's log level to info,
  # since it's _very_ verbose in the QEMU task
  # Disable the ISC DHCP servers, as they are failing in Ubuntu
  KERNEL_CMDLINE: "systemd.unit=mkosi-check-and-shutdown.service !quiet systemd.log_level=debug systemd.log_target=console udev.log_level=info systemd.default_standard_output=journal+console systemd.mask=isc-dhcp-server6.service systemd.mask=isc-dhcp-server.service"

jobs:
  ci:
    runs-on: ubuntu-22.04
    concurrency:
      group: ${{ github.workflow }}-${{ matrix.distro }}-${{ matrix.release }}-${{ github.ref }}
      cancel-in-progress: true
    strategy:
      fail-fast: false
      matrix:
        include:
          - distro: arch
            release: rolling
          - distro: debian
            release: testing
          - distro: ubuntu
            release: jammy
          - distro: fedora
            release: "37"
          - distro: fedora
            release: rawhide
          - distro: opensuse
            release: tumbleweed
          - distro: centos_epel
            release: 9-stream
          - distro: centos_epel
            release: 8-stream

    steps:
    - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b
    - uses: systemd/mkosi@792cbc60eb2dc4a58d66bb3c212bf92f8d50f6ea

    # FIXME: temporary workaround for a file conflict between systemd (C9S) and
    # systemd-boot (EPEL9). Drop this once systemd in C9S is updated to v252
    # (should be done by the end of 2022).
    - name: Fix C9S/EPEL9
      if: ${{ matrix.release == '9-stream' }}
      run: sudo sed -i '/add_packages/s/systemd-boot/systemd/g' /usr/local/lib/python3.10/dist-packages/mkosi/__init__.py

    - name: Install
      run: sudo apt-get update && sudo apt-get install --no-install-recommends python3-pexpect python3-jinja2

    - name: Configure
      run: |
        tee mkosi.default <<- EOF
        [Distribution]
        Distribution=${{ matrix.distro }}
        Release=${{ matrix.release }}

        [Content]
        Environment=CI_BUILD=1

        [Output]
        KernelCommandLine=${{ env.KERNEL_CMDLINE }}
        EOF

    - name: Build ${{ matrix.distro }}
      run: sudo python3 -m mkosi build

    - name: Show ${{ matrix.distro }} image summary
      run: sudo python3 -m mkosi summary

    - name: Boot ${{ matrix.distro }} systemd-nspawn
      run: sudo python3 -m mkosi boot ${{ env.KERNEL_CMDLINE }}

    - name: Check ${{ matrix.distro }} systemd-nspawn
      run: sudo python3 -m mkosi shell bash -c "[[ -e /testok ]] || { cat /failed-services; exit 1; }"

    - name: Boot ${{ matrix.distro }} QEMU
      run: sudo timeout -k 30 10m python3 -m mkosi qemu

    - name: Check ${{ matrix.distro }} QEMU
      run: sudo python3 -m mkosi shell bash -c "[[ -e /testok ]] || { cat /failed-services; exit 1; }"
