|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Run the prepare job logic locally. Writes the same outputs to a file so you can |
| 3 | +# verify matrix/names and simulate fromJson() without running the full workflow. |
| 4 | +# Usage: ./test/scripts/test-prepare.sh [output-file] |
| 5 | +# Default output: .github-output (repo root). Pass - to print only to stdout. |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +REPO_ROOT="$(cd "$(dirname "$0")/../.." && pwd)" |
| 10 | +OUTPUT_FILE="${1:-$REPO_ROOT/.github-output}" |
| 11 | + |
| 12 | +cd "$REPO_ROOT" |
| 13 | + |
| 14 | +if [ ! -f config/images.yaml ]; then |
| 15 | + echo "ERROR: config/images.yaml not found" >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if ! command -v yq &>/dev/null; then |
| 20 | + echo "ERROR: yq not installed. Install from https://github.com/mikefarah/yq" >&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +# Same logic as .github/workflows/sync-base-images.yml prepare job |
| 25 | +json=$(bash scripts/list.sh --json | tr -d '\n') |
| 26 | +if [ -z "$json" ] || [ "$json" = "[]" ]; then |
| 27 | + echo "ERROR: list.sh produced no image/arch pairs (check config/images.yaml)" >&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +echo "Matrix JSON length: ${#json} chars" |
| 32 | + |
| 33 | +# Build GITHUB_OUTPUT-style file (same as in workflow) |
| 34 | +if [ "$OUTPUT_FILE" = "-" ]; then |
| 35 | + TARGET="/dev/stdout" |
| 36 | +else |
| 37 | + TARGET="$OUTPUT_FILE" |
| 38 | + : > "$TARGET" |
| 39 | +fi |
| 40 | + |
| 41 | +printf 'matrix=%s\n' "${json//%/%25}" >> "$TARGET" |
| 42 | +images=$(yq -r '.images | keys | .[]' config/images.yaml | sort -u) |
| 43 | +echo "names<<NAMES_EOF" >> "$TARGET" |
| 44 | +echo "$images" >> "$TARGET" |
| 45 | +echo "NAMES_EOF" >> "$TARGET" |
| 46 | + |
| 47 | +if [ "$OUTPUT_FILE" != "-" ]; then |
| 48 | + echo "Wrote outputs to $OUTPUT_FILE" |
| 49 | +fi |
| 50 | + |
| 51 | +# Validate: simulate fromJson() on matrix (requires jq) |
| 52 | +if command -v jq &>/dev/null; then |
| 53 | + if echo "$json" | jq -e . >/dev/null 2>&1; then |
| 54 | + count=$(echo "$json" | jq 'length') |
| 55 | + echo "Valid JSON matrix: $count entries (fromJson would succeed)" |
| 56 | + echo "First 3 entries:" |
| 57 | + echo "$json" | jq -c '.[:3][]' |
| 58 | + else |
| 59 | + echo "WARNING: matrix value is not valid JSON (fromJson would fail)" >&2 |
| 60 | + echo "Raw (first 120 chars): ${json:0:120}..." >&2 |
| 61 | + fi |
| 62 | +else |
| 63 | + echo "Tip: install jq to validate that the matrix is valid JSON for fromJson()" |
| 64 | +fi |
0 commit comments