name: zephyr port

on:
  push:
  pull_request:
    paths:
      - '.github/workflows/ports_zephyr.yml'
      - 'tools/**'
      - 'py/**'
      - 'extmod/**'
      - 'shared/**'
      - 'lib/**'
      - 'ports/zephyr/**'
      - 'tests/**'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: jlumbroso/free-disk-space@main
      with:
        # Only free up a few things so this step runs quickly.
        # (android would save 9.6GiB, but takes about 13m)
        # (large-packages would save 4.6GiB, but takes about 3m)
        android: false
        dotnet: true
        haskell: true
        large-packages: false
        docker-images: false
        tool-cache: true
        swap-storage: false
    - uses: actions/checkout@v6
    - id: versions
      name: Read Zephyr version
      run: source tools/ci.sh && echo "ZEPHYR=$ZEPHYR_VERSION" | tee "$GITHUB_OUTPUT"
    - name: Cached Zephyr Workspace
      id: cache_workspace
      uses: actions/cache@v5
      with:
        # note that the Zephyr CI docker image is 15GB. At time of writing
        # GitHub caches are limited to 10GB total for a project. So we only
        # cache the "workspace"
        path: ./zephyrproject
        key: zephyr-workspace-${{ steps.versions.outputs.ZEPHYR }}
    - name: ccache
      uses: hendrikmuhs/ccache-action@v1.2
      with:
        key: zephyr
    - name: Install packages
      run: tools/ci.sh zephyr_setup
    - name: Install Zephyr
      if: steps.cache_workspace.outputs.cache-hit != 'true'
      run: tools/ci.sh zephyr_install
    - name: Build
      run: tools/ci.sh zephyr_build
    - name: Run main test suite
      run: tools/ci.sh zephyr_run_tests
    - name: Print failures
      if: failure()
      run: tests/run-tests.py --print-failures
