name: Demo

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - main

jobs:
  path-filter:
    runs-on: ubuntu-latest
    outputs:
      changed: ${{ steps.filter.outputs.changed }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            changed:
              - '.github/workflows/demo.yml'
              - 'demo/**'

  build:
    needs: [ "path-filter" ]
    if: needs.path-filter.outputs.changed == 'true'
    strategy:
      matrix:
        demo:
          - name:  homeassistant
            flake: basic
          - name:  homeassistant
            flake: ldap

          - name:  nextcloud
            flake: basic
          - name:  nextcloud
            flake: ldap
          - name:  nextcloud
            flake: sso

    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install nix
        uses: cachix/install-nix-action@v20

      - uses: cachix/cachix-action@v14
        with:
          name: selfhostblocks
          authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'

      - name: Build ${{ matrix.demo.name }} .#${{ matrix.demo.flake }}
        run: |
          cd demo/${{ matrix.demo.name }}
          nix \
            --print-build-logs \
            --option keep-going true \
            --show-trace \
            build .#nixosConfigurations.${{ matrix.demo.flake }}.config.system.build.vm

  result:
    runs-on: ubuntu-latest
    needs: [ "build" ]
    if: '!cancelled()'
    steps:
      - run: |
          result="${{ needs.tests.result }}"
          if [[ $result == "success" || $result == "skipped" ]]; then
            exit 0
          else
            exit 1
          fi