Skip to content

Commit 46a36dd

Browse files
committed
Fix matrix generation; add local test
1 parent e6a8860 commit 46a36dd

3 files changed

Lines changed: 70 additions & 3 deletions

File tree

.github/workflows/sync-base-images.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
# Download Hetzner bootimages, import as Docker images, push to GHCR (multi-arch).
33
# Runs monthly and on workflow_dispatch (manual). Image set from config/images.yaml.
4+
# Test the prepare step locally: ./test/scripts/test-prepare.sh (requires yq; optional jq to validate JSON).
45
name: Sync Hetzner Base Images
56

67
on:
@@ -31,14 +32,14 @@ jobs:
3132
echo "ERROR: config/images.yaml not found" >&2
3233
exit 1
3334
fi
34-
json=$(bash scripts/list.sh --json)
35+
json=$(bash scripts/list.sh --json | tr -d '\n')
3536
if [ -z "$json" ] || [ "$json" = "[]" ]; then
3637
echo "ERROR: list.sh produced no image/arch pairs (check config/images.yaml)" >&2
3738
exit 1
3839
fi
3940
echo "Matrix JSON length: ${#json} chars (output written to GITHUB_OUTPUT)"
40-
# Single-line matrix required for strategy.matrix fromJson(); % must be escaped for GITHUB_OUTPUT
41-
printf '%s\n' "matrix=${json//%/%25}" >> "$GITHUB_OUTPUT"
41+
# Single-line matrix required: GITHUB_OUTPUT is newline-delimited and fromJson() needs valid JSON
42+
printf 'matrix=%s\n' "${json//%/%25}" >> "$GITHUB_OUTPUT"
4243
images=$(yq -r '.images | keys | .[]' config/images.yaml | sort -u)
4344
echo "names<<NAMES_EOF" >> "$GITHUB_OUTPUT"
4445
echo "$images" >> "$GITHUB_OUTPUT"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.env
2+
.github-output
3+
.local/

test/scripts/test-prepare.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)