name: CI

on: [push, pull_request, workflow_dispatch]

jobs:

  collect-environments:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'

    - name: Find environments
      id: envs
      run: |
        if [[ $(< config/ci_exclude.json) != '[]' ]]; then echo "::warning title=Some environments were skipped::Environments not built: $(< config/ci_exclude.json)"; fi
        echo "envs=$(tools/show_envs.py | jq -c --argfile ex config/ci_exclude.json '. - $ex')" >> $GITHUB_OUTPUT

    outputs:
      envs: ${{ steps.envs.outputs.envs }}

  build-environment:
    runs-on: ubuntu-latest

    needs: [collect-environments]

    strategy:
      fail-fast: false
      matrix:
        envname: ${{ fromJson(needs.collect-environments.outputs.envs) }}

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'

    - name: Install PlatformIO
      run: |
        curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py -o get-platformio.py
        python get-platformio.py
        echo "/home/runner/.platformio/penv/bin" >> $GITHUB_PATH

    - name: Copy secrets and clear SSID
      run: |
        grep -v "^#define cszSSID" include/secrets.example.h > include/secrets.h
        echo '#define cszSSID ""' >> include/secrets.h

    - name: "Build environment: ${{ matrix.envname }}"
      run: pio run -e ${{ matrix.envname }}

  call-web-installer:
    if: ( github.repository_owner == 'rbergen' && github.ref == 'refs/heads/staging' )

    needs: [build-environment]

    uses: ./.github/workflows/web_installer.yml
    with:
      release-name: ${{ github.ref_name }} commit ${{ github.sha }}
    secrets: inherit