name: Build

on:
  push:
    paths:
      - ".github/workflows/build.yml"
      - "app/**"
  pull_request:
    paths:
      - ".github/workflows/build.yml"
      - "app/**"

jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: docker.io/zmkfirmware/zmk-build-arm:2.5
    strategy:
      matrix:
        board:
          - bluemicro840_v1
          - nice_nano
          - nice_nano_v2
          - nrfmicro_13
          - proton_c
        shield:
          - bfo9000_left
          - bfo9000_right
          - boardsource3x4
          - corne_left
          - corne_right
          - cradio_left
          - cradio_right
          - crbn
          - eek
          - helix_left
          - helix_right
          - iris_left
          - iris_right
          - jian_left
          - jian_right
          - jorne_left
          - jorne_right
          - kyria_left
          - kyria_right
          - lily58_left
          - lily58_right
          - microdox_left
          - microdox_right
          - nibble
          - qaz
          - quefrency_left
          - quefrency_right
          - reviung41
          - romac
          - romac_plus
          - settings_reset
          - sofle_left
          - sofle_right
          - splitreus62_left
          - splitreus62_right
          - tg4x
          - tidbit
        cmake-args: [""]
        include:
          - board: bdn9_rev2
          - board: dz60rgb_rev1
          - board: nrf52840_m2
            shield: m60
          - board: planck_rev6
          - board: proton_c
            shield: clueboard_california
          - board: nice_nano_v2
            shield: kyria_left
            cmake-args: -DCONFIG_ZMK_DISPLAY=y
            skip-archive: true
          - board: nice_nano_v2
            shield: kyria_right
            cmake-args: -DCONFIG_ZMK_DISPLAY=y
            skip-archive: true
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Cache west modules
        uses: actions/cache@v2
        env:
          cache-name: cache-zephyr-modules
        with:
          path: |
            modules/
            tools/
            zephyr/
            bootloader/
          key: 4-${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('app/west.yml') }}
          restore-keys: |
            4-${{ runner.os }}-build-${{ env.cache-name }}-
            4-${{ runner.os }}-build-
            4-${{ runner.os }}-
        timeout-minutes: 2
        continue-on-error: true
      - name: Initialize workspace (west init)
        run: west init -l app
      - name: Update modules (west update)
        run: west update
      - name: Export Zephyr CMake package (west zephyr-export)
        run: west zephyr-export
      - name: Prepare variables
        id: variables
        run: |
          SHIELD_ARG=
          ARTIFACT_NAME="${{ matrix.board }}"

          if [ -n "${{ matrix.shield }}" ]; then
            SHIELD_ARG="-DSHIELD=${{ matrix.shield }}"
            ARTIFACT_NAME="${ARTIFACT_NAME}-${{ matrix.shield }}"
          fi

          ARTIFACT_NAME="${ARTIFACT_NAME}-zmk"

          echo ::set-output name=shield-arg::${SHIELD_ARG}
          echo ::set-output name=artifact-name::${ARTIFACT_NAME}
      - name: Build (west build)
        run: west build -s app -b ${{ matrix.board }} -- ${{ steps.variables.outputs.shield-arg }} ${{ matrix.cmake-args }}
      - name: Archive artifacts
        if: ${{ !matrix.skip-archive }}
        uses: actions/upload-artifact@v2
        with:
          name: "${{ steps.variables.outputs.artifact-name }}"
          path: |
            build/zephyr/zmk.hex
            build/zephyr/zmk.uf2
        continue-on-error: true
