name: "examples: build/host-tests"

on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened, labeled]

jobs:
  build_all_examples:
    if: contains(github.event.pull_request.labels.*.name, 'examples') || github.event_name == 'push'
    name: Build examples
    strategy:
      matrix:
        idf_ver: ["latest", "release-v5.1", "release-v5.2", "release-v5.3"]
        include:
          - idf_ver: "latest"
            warning: "Warning: The smallest app partition is nearly full"
    runs-on: ubuntu-22.04
    container: espressif/idf:${{ matrix.idf_ver }}
    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@v4
      - name: Build with IDF-${{ matrix.idf_ver }}
        env:
          EXPECTED_WARNING: ${{ matrix.warning }}
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          python -m pip install idf-build-apps
          # Build default configs for all targets
          python ./ci/build_apps.py examples -m examples/.build-test-rules.yml -d -c

  build_and_run_on_host:
    if: contains(github.event.pull_request.labels.*.name, 'examples') || github.event_name == 'push'
    name: Build and run examples on linux
    strategy:
      matrix:
        idf_ver: ["latest"]
    runs-on: ubuntu-22.04
    container: espressif/idf:${{ matrix.idf_ver }}
    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@v4
      - name: Build with IDF-${{ matrix.idf_ver }}
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          python -m pip install idf-build-apps
          python ./ci/build_apps.py examples/mqtt -l -t linux
          timeout 5 ./examples/mqtt/build_linux_default/esp_mqtt_demo.elf | tee test.log  || true
          grep 'MQTT_EVENT_DATA' test.log
