|
1 | 1 | #!/usr/bin/env bash |
2 | | -# -e — Exit on error |
3 | | -# -u — Treat unset variables as an error |
4 | | -# -o pipefail — Prevent errors in a pipeline from being masked |
5 | 2 | set -euo pipefail |
6 | 3 |
|
7 | | -get_ubuntu_lts() { echo "24.04"; } |
8 | | -get_fedora_latest() { echo "40"; } |
9 | | -get_arch_latest() { echo "latest"; } |
| 4 | +get_ubuntu_lts() { echo "24.04"; } |
| 5 | +get_fedora_latest() { echo "40"; } |
| 6 | +get_arch_latest() { echo "latest"; } |
10 | 7 | get_almalinux_latest() { echo "9.4"; } |
11 | | -get_debian_latest() { echo "13"; } |
12 | | -get_rhel_latest() { echo "9.4"; } |
13 | | -get_geant4_tag() { echo "11.3.2"; } |
14 | | - |
15 | | -#!/usr/bin/env bash |
16 | | -set -euo pipefail |
17 | | - |
18 | | -# ... your get_* functions stay the same ... |
| 8 | +get_debian_latest() { echo "13"; } |
| 9 | +get_rhel_latest() { echo "9.4"; } |
| 10 | +get_geant4_tag() { echo "11.3.2"; } |
19 | 11 |
|
| 12 | +# Pretty-print with jq if available, otherwise emit compact JSON |
20 | 13 | build_matrix() { |
21 | | - local geant4_tag |
22 | | - geant4_tag="$(get_geant4_tag)" |
23 | | - cat <<EOF |
24 | | -{ |
25 | | - "include": [ |
26 | | - { "distro": "ubuntu", "docker_from": "ubuntu:$(get_ubuntu_lts)", "geant4_tag": "${geant4_tag}" }, |
27 | | - { "distro": "fedora", "docker_from": "fedora:$(get_fedora_latest)", "geant4_tag": "${geant4_tag}" }, |
28 | | - { "distro": "arch", "docker_from": "archlinux:$(get_arch_latest)", "geant4_tag": "${geant4_tag}" }, |
29 | | - { "distro": "almalinux", "docker_from": "almalinux:$(get_almalinux_latest)", "geant4_tag": "${geant4_tag}" }, |
30 | | - { "distro": "debian", "docker_from": "debian:$(get_debian_latest)", "geant4_tag": "${geant4_tag}" }, |
31 | | - { "distro": "rhel", "docker_from": "redhat/ubi9:$(get_rhel_latest)","geant4_tag": "${geant4_tag}" } |
32 | | - ] |
33 | | -} |
| 14 | + local json |
| 15 | + json=$( |
| 16 | + cat <<EOF |
| 17 | +{"include":[ |
| 18 | + {"distro":"ubuntu","docker_from":"ubuntu:$(get_ubuntu_lts)","geant4_tag":"$(get_geant4_tag)"}, |
| 19 | + {"distro":"fedora","docker_from":"fedora:$(get_fedora_latest)","geant4_tag":"$(get_geant4_tag)"}, |
| 20 | + {"distro":"arch","docker_from":"archlinux:$(get_arch_latest)","geant4_tag":"$(get_geant4_tag)"}, |
| 21 | + {"distro":"almalinux","docker_from":"almalinux:$(get_almalinux_latest)","geant4_tag":"$(get_geant4_tag)"}, |
| 22 | + {"distro":"debian","docker_from":"debian:$(get_debian_latest)","geant4_tag":"$(get_geant4_tag)"}, |
| 23 | + {"distro":"rhel","docker_from":"redhat/ubi9:$(get_rhel_latest)","geant4_tag":"$(get_geant4_tag)"} |
| 24 | +]} |
34 | 25 | EOF |
| 26 | + ) |
| 27 | + if command -v jq >/dev/null 2>&1; then |
| 28 | + printf '%s' "$json" | jq . |
| 29 | + else |
| 30 | + printf '%s' "$json" |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +# portable lowercasing (works on old bash, dash, zsh) |
| 35 | +lc() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; } |
| 36 | + |
| 37 | +# Build a clean GHCR image ref: ghcr.io/<owner>/<repo> |
| 38 | +build_image_ref() { |
| 39 | + # Owner from env (Actions sets this). Fallback for local runs. |
| 40 | + local owner="${GITHUB_REPOSITORY_OWNER:-JeffersonLab}" |
| 41 | + |
| 42 | + # Repo name = LAST segment of GITHUB_REPOSITORY (strip any "owner/" prefix). |
| 43 | + # Fallback to a default if env is missing during local runs. |
| 44 | + local repo_full="${GITHUB_REPOSITORY:-jeffersonlab/geant4-docker}" |
| 45 | + local repo="${repo_full##*/}" |
| 46 | + |
| 47 | + # Lowercase both parts (GHCR requires lowercase) |
| 48 | + printf 'ghcr.io/%s/%s' "$(lc "$owner")" "$(lc "$repo")" |
35 | 49 | } |
36 | 50 |
|
37 | 51 | main() { |
38 | | - # Compute image ref (lowercase) for GHCR |
39 | | - local owner repo owner_lc repo_lc image |
40 | | - owner="${GITHUB_REPOSITORY_OWNER:-JeffersonLab}" |
41 | | - repo="${GITHUB_REPOSITORY##*/:-geant4}" |
42 | | - owner_lc="$(echo "$owner" | tr '[:upper:]' '[:lower:]')" |
43 | | - repo_lc="$(echo "$repo" | tr '[:upper:]' '[:lower:]')" |
44 | | - image="ghcr.io/${owner_lc}/${repo_lc}" |
45 | | - |
46 | | - if [[ -n "${GITHUB_OUTPUT:-}" ]]; then |
47 | | - # Running in GitHub Actions: write multi-line outputs |
48 | | - local DELIM="MATRIX_$(date +%s%N)" |
49 | | - { |
50 | | - echo "matrix<<$DELIM" |
51 | | - build_matrix |
52 | | - echo "$DELIM" |
53 | | - echo "image=$image" |
54 | | - } >> "$GITHUB_OUTPUT" |
55 | | - else |
56 | | - # Local run: print JSON to stdout for inspection |
57 | | - build_matrix |
58 | | - echo "# image=$image" >&2 |
59 | | - fi |
| 52 | + # Resolve owner/repo and force lowercase (portable) |
| 53 | + local owner repo owner_lc repo_lc image |
| 54 | + owner="${GITHUB_REPOSITORY_OWNER:-JeffersonLab}" |
| 55 | + repo="${GITHUB_REPOSITORY##*/}" |
| 56 | + : "${repo:=geant4}" |
| 57 | + |
| 58 | + owner_lc="$(printf '%s' "$owner" | tr '[:upper:]' '[:lower:]')" |
| 59 | + repo_lc="$(printf '%s' "$repo" | tr '[:upper:]' '[:lower:]')" |
| 60 | + image="$(build_image_ref)" |
| 61 | + |
| 62 | + if [[ -n "${GITHUB_OUTPUT:-}" ]]; then |
| 63 | + DELIM="MATRIX_$(date +%s%N)" |
| 64 | + { |
| 65 | + echo "matrix<<$DELIM" |
| 66 | + build_matrix |
| 67 | + echo "$DELIM" |
| 68 | + echo "image=$image" |
| 69 | + } >>"$GITHUB_OUTPUT" |
| 70 | + else |
| 71 | + build_matrix |
| 72 | + echo "# image=$image" >&2 |
| 73 | + fi |
| 74 | + |
| 75 | + if [[ -n "${GITHUB_OUTPUT:-}" ]]; then |
| 76 | + local DELIM="MATRIX_$(date +%s%N)" |
| 77 | + { |
| 78 | + echo "matrix<<$DELIM" |
| 79 | + build_matrix |
| 80 | + echo "$DELIM" |
| 81 | + echo "image=$image" |
| 82 | + } >>"$GITHUB_OUTPUT" |
| 83 | + else |
| 84 | + build_matrix |
| 85 | + echo "# image=$image" >&2 |
| 86 | + fi |
60 | 87 | } |
61 | 88 |
|
62 | 89 | main "$@" |
0 commit comments