|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Resolve which targets CI should run, from the registry in targets.json. |
| 5 | +# |
| 6 | +# Inputs: |
| 7 | +# INPUT_TARGETS workflow_dispatch CSV. Empty => default set (default=true). |
| 8 | +# Outputs (GITHUB_OUTPUT): |
| 9 | +# enabled_targets CSV of selected ids; gates the qemu/vm jobs in make.yml. |
| 10 | +# native_matrix JSON array of enabled kind=native entries; consumed as the |
| 11 | +# native job's strategy.matrix.include (empty => job skipped). |
| 12 | +# |
| 13 | +# targets.json is the single source of truth. Opt-in targets (default=false, |
| 14 | +# e.g. netbsd, dragonflybsd) are excluded from the default and must be named |
| 15 | +# explicitly via INPUT_TARGETS. |
| 16 | + |
| 17 | +CI_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 18 | +REGISTRY="$CI_ROOT/targets.json" |
| 19 | + |
| 20 | +VALID_TARGETS="$(jq -r '[.targets[].id] | join(",")' "$REGISTRY")" |
| 21 | +DEFAULT="$(jq -r '[.targets[] | select(.default) | .id] | join(",")' "$REGISTRY")" |
| 22 | + |
| 23 | +if [ -z "${INPUT_TARGETS// /}" ]; then |
| 24 | + TARGETS="$DEFAULT" |
| 25 | + SOURCE="default" |
| 26 | +else |
| 27 | + TARGETS="${INPUT_TARGETS// /}" |
| 28 | + SOURCE="workflow_dispatch input" |
| 29 | +fi |
| 30 | + |
| 31 | +# Warn (don't fail) on unknown ids so a typo is visible but harmless: an |
| 32 | +# unrecognised id simply matches no job. |
| 33 | +IFS=',' read -r -a _selected <<< "$TARGETS" |
| 34 | +IFS=',' read -r -a _valid <<< "$VALID_TARGETS" |
| 35 | +for _id in "${_selected[@]}"; do |
| 36 | + [ -z "$_id" ] && continue |
| 37 | + _found=0 |
| 38 | + for _v in "${_valid[@]}"; do |
| 39 | + if [ "$_id" = "$_v" ]; then |
| 40 | + _found=1 |
| 41 | + break |
| 42 | + fi |
| 43 | + done |
| 44 | + if [ "$_found" -eq 0 ]; then |
| 45 | + echo "::warning::Unknown target id \"${_id}\" (valid: ${VALID_TARGETS})" |
| 46 | + fi |
| 47 | +done |
| 48 | + |
| 49 | +# Filter the registry to enabled native entries. Bind .id before switching the |
| 50 | +# pipe context to the split list; index() returns null when absent (falsy) and |
| 51 | +# an integer otherwise (0 is truthy in jq). |
| 52 | +NATIVE_MATRIX="$(jq -c --arg ids "$TARGETS" \ |
| 53 | + '[.targets[] | select(.kind == "native") | select(.id as $i | ($ids | split(",") | index($i)))]' \ |
| 54 | + "$REGISTRY")" |
| 55 | + |
| 56 | +{ |
| 57 | + echo "enabled_targets=${TARGETS}" |
| 58 | + echo "native_matrix=${NATIVE_MATRIX}" |
| 59 | +} >> "${GITHUB_OUTPUT:?GITHUB_OUTPUT is required}" |
| 60 | + |
| 61 | +echo "Enabled targets (${SOURCE}): ${TARGETS}" |
| 62 | +echo "Native matrix: ${NATIVE_MATRIX}" |
0 commit comments