|
1 | 1 | #!/usr/bin/env sh |
2 | | - |
3 | 2 | # Discover dockerfiles: dockerfiles/Dockerfile-gemc-<tag>-<os> |
4 | | -# Outputs (for GitHub Actions): |
5 | | -# matrix: {"include":[{"file":..., "tag":..., "os":..., "latest":true|false}, ...]} |
6 | | -# image : ghcr.io/<owner>/gemc |
| 3 | +# Outputs for GitHub Actions: |
| 4 | +# - matrix: {"include":[{"file":..., "tag":..., "os":..., "latest":true|false}, ...]} |
| 5 | +# - image : ghcr.io/<owner>/gemc |
7 | 6 | # Works on macOS (BSD utils), Linux, and GHA. |
8 | | -# Example: |
9 | | -# GITHUB_REPOSITORY_OWNER="gemc" ./ci/discover-docker-matrix.sh |
10 | 7 |
|
11 | 8 | set -eu |
12 | 9 |
|
13 | 10 | DIR="${1:-dockerfiles}" |
14 | 11 |
|
15 | | -# Lowercase owner without bash-4 features |
16 | | -OWNER="${GITHUB_REPOSITORY_OWNER:-unknown}" |
| 12 | +# Determine owner (prefer GITHUB_REPOSITORY_OWNER; fallback to GITHUB_REPOSITORY) |
| 13 | +OWNER="${GITHUB_REPOSITORY_OWNER:-}" |
| 14 | +if [ -z "$OWNER" ] && [ -n "${GITHUB_REPOSITORY:-}" ]; then |
| 15 | + OWNER="${GITHUB_REPOSITORY%%/*}" |
| 16 | +fi |
| 17 | +[ -z "$OWNER" ] && OWNER="unknown" |
17 | 18 | OWNER_LOWER=$(printf '%s' "$OWNER" | tr '[:upper:]' '[:lower:]') |
18 | 19 | IMAGE="ghcr.io/$OWNER_LOWER/gemc" |
19 | 20 |
|
20 | | -# Collect matches (handle spaces via NUL separators) |
21 | 21 | TMP_TSV="$(mktemp)" |
22 | | -FOUND=0 |
23 | | -# macOS/BSD find supports -print0; if not, fall back without it |
24 | | -if find "$DIR" -type f -name 'Dockerfile-gemc-*' -print0 >/dev/null 2>&1; then |
25 | | - find "$DIR" -type f -name 'Dockerfile-gemc-*' -print0 \ |
26 | | - | while IFS= read -r -d '' F; do |
27 | | - FOUND=$((FOUND+1)) # this won't persist outside subshell; we’ll recount later |
28 | | - BASE=$(basename "$F") # Dockerfile-gemc-dev3-ubuntu24 |
29 | | - REST=${BASE#Dockerfile-} # gemc-dev3-ubuntu24 |
30 | | - case "$REST" in |
31 | | - gemc-*-*) : ;; |
32 | | - *) continue ;; |
33 | | - esac |
34 | | - REST2=${REST#gemc-} # dev3-ubuntu24 |
35 | | - TAG=${REST2%%-*} # dev3 |
36 | | - OS=${REST2#*-} # ubuntu24 (may contain dashes) |
37 | | - # trailing number as weight (dev3 -> 3; dev -> 0) |
38 | | - DIGITS=$(printf '%s\n' "$TAG" | sed -n 's/.*\([0-9][0-9]*\)$/\1/p') |
39 | | - [ -z "$DIGITS" ] && DIGITS=0 |
40 | | - printf '%s\t%s\t%s\t%s\n' "$F" "$TAG" "$OS" "$DIGITS" >> "$TMP_TSV" |
41 | | - done |
42 | | -else |
43 | | - # Fallback (no -print0) |
44 | | - find "$DIR" -type f -name 'Dockerfile-gemc-*' \ |
45 | | - | while IFS= read -r F; do |
46 | | - BASE=$(basename "$F") |
47 | | - REST=${BASE#Dockerfile-} |
48 | | - case "$REST" in |
49 | | - gemc-*-*) : ;; |
50 | | - *) continue ;; |
51 | | - esac |
52 | | - REST2=${REST#gemc-} |
53 | | - TAG=${REST2%%-*} |
54 | | - OS=${REST2#*-} |
55 | | - DIGITS=$(printf '%s\n' "$TAG" | sed -n 's/.*\([0-9][0-9]*\)$/\1/p') |
56 | | - [ -z "$DIGITS" ] && DIGITS=0 |
57 | | - printf '%s\t%s\t%s\t%s\n' "$F" "$TAG" "$OS" "$DIGITS" >> "$TMP_TSV" |
58 | | - done |
| 22 | + |
| 23 | +# Glob-based enumeration (portable; avoids read -d / -print0) |
| 24 | +set -- "$DIR"/Dockerfile-gemc-* |
| 25 | +if [ "$1" = "$DIR/Dockerfile-gemc-*" ]; then |
| 26 | + echo "No matching dockerfiles found under '$DIR' (expected Dockerfile-gemc-<tag>-<os>)." >&2 |
| 27 | + echo "[local] matrix={\"include\":[]}" |
| 28 | + echo "[local] image=$IMAGE" |
| 29 | + exit 1 |
59 | 30 | fi |
60 | 31 |
|
| 32 | +for F in "$@"; do |
| 33 | + [ -f "$F" ] || continue |
| 34 | + BASE=${F##*/} # Dockerfile-gemc-dev3-ubuntu24 |
| 35 | + REST=${BASE#Dockerfile-} # gemc-dev3-ubuntu24 |
| 36 | + case "$REST" in |
| 37 | + gemc-*-*) ;; |
| 38 | + *) continue ;; |
| 39 | + esac |
| 40 | + REST2=${REST#gemc-} # dev3-ubuntu24 |
| 41 | + TAG=${REST2%%-*} # dev3 |
| 42 | + OS=${REST2#*-} # ubuntu24 (may contain dashes) |
| 43 | + |
| 44 | + # trailing digits in TAG as weight (dev3 -> 3; dev -> 0) |
| 45 | + DIGITS=$(printf '%s\n' "$TAG" | sed -n 's/.*\([0-9][0-9]*\)$/\1/p') |
| 46 | + [ -z "$DIGITS" ] && DIGITS=0 |
| 47 | + |
| 48 | + printf '%s\t%s\t%s\t%s\n' "$F" "$TAG" "$OS" "$DIGITS" >> "$TMP_TSV" |
| 49 | +done |
| 50 | + |
61 | 51 | # If nothing was written, bail out clearly |
62 | 52 | if [ ! -s "$TMP_TSV" ]; then |
63 | | - echo "No matching dockerfiles found under '$DIR' (expected Dockerfile-gemc-<tag>-<os>)." >&2 |
64 | | - echo "Examples: Dockerfile-gemc-dev3-ubuntu24, Dockerfile-gemc-dev3-fedora40" >&2 |
| 53 | + echo "No usable dockerfiles after parsing." >&2 |
65 | 54 | echo "[local] matrix={\"include\":[]}" |
66 | 55 | echo "[local] image=$IMAGE" |
67 | 56 | exit 1 |
68 | 57 | fi |
69 | 58 |
|
70 | | -# Build JSON with BSD/POSIX awk (no non-portable features) |
| 59 | +# Build JSON with POSIX/BSD awk |
71 | 60 | JSON=$( |
72 | 61 | awk -F '\t' ' |
73 | 62 | BEGIN { n=0 } |
|
0 commit comments