name: CI Tutorials

on:
  push:
    branches: [ main, develop ]
    tags: [ 'sdk-v*' ]
  pull_request:

# env:
#   APPS_EXCLUDED: |
#     ScrollMenu

jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      cmake_apps: ${{ steps.generate.outputs.cmake_apps }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Generate cmake apps list
        id: generate
        run: |
          python .github/scripts/generate-tutorials-app-list.py > cmake_apps.json
          cmake_apps=$(jq -c '.cmake_apps' cmake_apps.json)
          echo "cmake_apps=$cmake_apps" >> $GITHUB_OUTPUT

  cmake-build:
    runs-on: ubuntu-latest
    container: xanderhendriks/stm32cubeide:16.0
    needs: prepare
    # A backstop only; see apps-ci.yml's cmake-build.
    timeout-minutes: 30
    strategy:
      # One tutorial's setup failing is not the others' news, and the default
      # would cancel them. See the note in apps-ci.yml's cmake-build.
      fail-fast: false
      matrix:
        app: ${{ fromJson(needs.prepare.outputs.cmake_apps) }}
    steps:
      - name: Setup GIT
        run: |
          # Same image and the same ~87 MB of packages as apps-ci.yml's
          # cmake-build, so the same two measures: the Azure mirror for
          # throughput, and By-Hash so a mid-publish archive stays
          # self-consistent. See that job for the full note.
          find /etc/apt -type f \( -name '*.list' -o -name '*.sources' \) \
            -exec sed -i -E 's#(https?://)(security|archive)\.ubuntu\.com/ubuntu#\1azure.archive.ubuntu.com/ubuntu#g' {} +
          apt-get -o Acquire::By-Hash=yes update
          apt-get install -y git python3-full python3-pip python-is-python3 cmake

      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          # The LVGL GUI variants are built from the LVGL submodule (ThirdParty/lvgl).
          submodules: recursive

      - name: Install scripts requirements
        run: python3 -m pip install --break-system-packages -r Utilities/Scripts/app_packer/requirements.txt

      - name: Build ${{ matrix.app }}
        # A tutorial may carry more than one app project (a TouchGFX GUI and
        # an LVGL GUI over the same service); every *-CMake directory is built.
        env:
          # Through the environment, not expanded into the script: the value
          # is a directory name from the checkout.
          APP: ${{ matrix.app }}
        run: |
          echo "Setup environment"
          . ./Utilities/Scripts/build-cube/find-cube.sh
          export UNA_SDK=$GITHUB_WORKSPACE
          PROJECTS=$(python3 .github/scripts/generate-tutorials-app-list.py --app "$APP")
          if [ -z "$PROJECTS" ]; then
            printf 'No *-CMake project found under Docs/Tutorials/%s\n' "$APP"
            exit 1
          fi
          # One project per line from the script; read whole lines so a
          # path is never split on whitespace. (POSIX sh: the container's
          # default shell is not bash.)
          printf '%s\n' "$PROJECTS" | while IFS= read -r CMAKE_ROOT; do
            [ -n "$CMAKE_ROOT" ] || continue
            if [ ! -d "$CMAKE_ROOT" ]; then
              printf 'Missing CMake project: %s\n' "$CMAKE_ROOT" >&2
              exit 1
            fi
            echo "Build $CMAKE_ROOT"
            mkdir -p "$CMAKE_ROOT/build"
            (cd "$CMAKE_ROOT/build" && cmake .. && make -j"$(nproc)")
          done

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.app }}-output
          # The TouchGFX build's Output/ and the LVGL build's OutputLVGL/.
          path: |
            Docs/Tutorials/${{ matrix.app }}/Output/
            Docs/Tutorials/${{ matrix.app }}/OutputLVGL/

  release:
    runs-on: ubuntu-latest
    needs: cmake-build
    if: startsWith(github.ref, 'refs/tags/sdk-v')
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Create release archive
        run: |
          zip -r tutorials-release.zip artifacts/

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: tutorials-release.zip
          generate_release_notes: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}