Skip to content

refactor(ci): generate unit test workflow matrix dynamically #211

refactor(ci): generate unit test workflow matrix dynamically

refactor(ci): generate unit test workflow matrix dynamically #211

Workflow file for this run

name: Unit Tests
on:
workflow_dispatch:
inputs:
clearCaches:
description: "Clear workflow caches where possible"
required: false
type: string
pull_request:
push:
# Ignore merge queue branches on push; avoids merge_group+push concurrency race since ref is same
branches-ignore:
- 'gh-readonly-queue/**'
merge_group:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# We want to generate our matrix dynamically
# Initial job generates the matrix as a JSON, and following job will use deserialize and use the result
matrix_prep:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.build-matrix.outputs.result }}
steps:
- id: build-matrix
uses: actions/github-script@v7
with:
# Notes:
# 1) 'os'
# - https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
# - macOS 14 is in beta and runs on Apple Silicon [M1]
# 2) 'include'
# - define 'name' so we don't need to update branch protection rules if the os changes
script: |
return {
"os": ["ubuntu-latest", "macos-14", "windows-latest"],
"include": [
{"os": "ubuntu-latest", "name": "ubuntu"},
{"os": "macos-14", "name": "macos"},
{"os": "windows-latest", "name": "windows"}
]
}
- name: Debug Output
run: echo "${{ steps.build-matrix.outputs.result }}"
# This uses the matrix generated from the matrix-prep stage
# it will run unit tests on whatever OS combinations are desired
unit:
name: JUnit Tests (${{ matrix.name }})
needs: matrix_prep
timeout-minutes: 40
strategy:
fail-fast: false
matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
runs-on: ${{ matrix.os }}
#env:
# CODACY_TOKEN: ${{ secrets.CODACY_TOKEN }}
steps:
- name: Configure Windows Pagefile
uses: al-cheb/configure-pagefile-action@v1.4
if: contains(matrix.os, 'windows')
with:
minimum-size: 8GB
maximum-size: 12GB
disk-root: "C:"
- uses: actions/checkout@v4
with:
fetch-depth: 50
- name: Configure JDK
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21" # also change jitpack.yml
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
timeout-minutes: 5
with:
# Only write to the cache for builds on the 'main' branches, stops branches evicting main cache
# Builds on other branches will only read from main branch cache writes
# Comment this and the with: above out for performance testing on a branch
cache-read-only: ${{ github.ref != 'refs/heads/main' }}
# gradle-home-cache-cleanup is temporarily disabled to investigate cache pollution issues
# Requested in: https://github.com/gradle/actions/issues/167#issuecomment-2052352341
# gradle-home-cache-cleanup: true
- name: Clear Caches Optionally
if: "${{ github.event.inputs.clearCaches != '' }}"
shell: bash
run: |
du -sk ~/.gradle
rm -fr ~/.gradle
du -sk ~/.gradle || echo ~/.gradle is gone
- name: Gradle Dependency Download
uses: nick-invision/retry@v3
with:
timeout_minutes: 10
retry_wait_seconds: 60
max_attempts: 3
command: ./gradlew robolectricSdkDownload --daemon
- name: Run Unit Tests
uses: gradle/gradle-build-action@v3
with:
arguments: jacocoUnitTestReport --daemon
- name: Store Logcat as Artifact
# cancelled() handles test timeouts
# remove when test timeouts cause a failure()
if: failure() || cancelled()
uses: actions/upload-artifact@v3
with:
name: logcat-${{ matrix.name }}
# look for the `<system-out>` element in the XML files in `test-results`
# The folder contains far too much data:
# * .bin files
# * XML rather than TXT
# * Files are mostly JaCoCo issues logged to stderr (#16180)
# * All tests are logged, rather than just failures
# despite this, it's a great start
# look to see if there's a dependency we can use, arther than improving this
path: |
**/build/test-results/
- name: Stop Gradle
if: contains(matrix.os, 'windows')
uses: gradle/gradle-build-action@v3
with:
arguments: --stop
- uses: codecov/codecov-action@v3
with:
verbose: true