From 894924dde136d06cfe8a862a6630652c29ab459c Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 16 Sep 2026 19:34:55 +0200 Subject: [PATCH] Allow staged replacement of publication builds --- README.md | 5 ++++- beffroi.sh | 2 +- scripts/build-publication.sh | 42 ++++++++++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b66a4d7..abcdbfc 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,9 @@ JSON files, which is useful for downstream automation: ```bash ./beffroi.sh all build/publication --include-json + +Use `--replace` to rebuild an existing publication directory at the same path. +The previous build stays in place until the new PDFs are ready. ``` Jenkins runs the same build and publishes AES-encrypted `Reports.zip` and @@ -79,7 +82,7 @@ destination. ./beffroi.sh diagnostic-pdf DIAGNOSTIC.json OUTPUT.pdf ./beffroi.sh capture-pdf CAPTURE OUTPUT.pdf ./beffroi.sh diagnostic CAPTURE EVIDENCE_ROOT OUTPUT_DIR REFERENCE_TIME TITLE AUTHOR DATE -./beffroi.sh all NEW_OUTPUT_DIR [--include-json] +./beffroi.sh all OUTPUT_DIR [--include-json] [--replace] ./beffroi.sh package BUILD_DIR ARTIFACT_DIR ./beffroi.sh test ./beffroi.sh bootstrap diff --git a/beffroi.sh b/beffroi.sh index b4afb0d..e4b2a34 100755 --- a/beffroi.sh +++ b/beffroi.sh @@ -9,7 +9,7 @@ Usage: ./beffroi.sh diagnostic-pdf DIAGNOSTIC.json OUTPUT.pdf ./beffroi.sh capture-pdf CAPTURE OUTPUT.pdf ./beffroi.sh diagnostic CAPTURE EVIDENCE_ROOT OUTPUT_DIR REFERENCE_TIME TITLE AUTHOR DATE - ./beffroi.sh all NEW_OUTPUT_DIR [--include-json] + ./beffroi.sh all OUTPUT_DIR [--include-json] [--replace] ./beffroi.sh package BUILD_DIR ARTIFACT_DIR ./beffroi.sh test ./beffroi.sh bootstrap diff --git a/scripts/build-publication.sh b/scripts/build-publication.sh index 4da9680..c6c4ae3 100755 --- a/scripts/build-publication.sh +++ b/scripts/build-publication.sh @@ -2,7 +2,7 @@ set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")/.." usage() { - echo "Usage: $0 NEW_OUTPUT_ROOT [--include-json]" >&2 + echo "Usage: $0 OUTPUT_ROOT [--include-json] [--replace]" >&2 exit 2 } @@ -10,24 +10,41 @@ output_root=${1:-} [[ -n "$output_root" ]] || usage shift -include_json=false +include_json=false replace=false while (($#)); do case $1 in --include-json) include_json=true ;; + --replace) replace=true ;; *) usage ;; esac shift done -[[ ! -e "$output_root" ]]||{ echo "$output_root: output root already exists" >&2; exit 1; } -mkdir -p "$output_root/reports" "$output_root/diagnostics" +[[ ! -L "$output_root" ]] || { echo "$output_root: symlink output root is not supported" >&2; exit 1; } +output_root=$(realpath -m "$output_root") +root=$(pwd -P) +[[ "$output_root" != "$root" && "$root" != "$output_root"/* ]] || { echo "$output_root: output root cannot contain the repository" >&2; exit 1; } +[[ "$output_root" != "$root/reports" && "$output_root" != "$root/reports"/* && "$output_root" != "$root/reporter" && "$output_root" != "$root/reporter"/* ]] || { echo "$output_root: output root cannot be inside a source component" >&2; exit 1; } +[[ ! -e "$output_root" || -d "$output_root" ]] || { echo "$output_root: output root is not a directory" >&2; exit 1; } +[[ ! -e "$output_root" || "$replace" == true ]] || { echo "$output_root: output root already exists; use --replace" >&2; exit 1; } +output_parent=$(dirname "$output_root") +mkdir -p "$output_parent" +output_name=$(basename "$output_root") +stage=$(mktemp -d "$output_parent/.${output_name}.build.XXXXXXXX") +backup= +cleanup() { + if [[ -n "$backup" && -d "$backup" && ! -e "$output_root" ]]; then mv -- "$backup" "$output_root"; fi + [[ ! -d "$stage" ]] || rm -rf -- "$stage" +} +trap cleanup EXIT +mkdir -p "$stage/reports" "$stage/diagnostics" reporter/tools/validate-all.sh reports/reports -reporter/tools/render-all.sh reports/reports --output-dir "$output_root/reports/pdfs" +reporter/tools/render-all.sh reports/reports --output-dir "$stage/reports/pdfs" count=0 while IFS= read -r -d '' source; do relative=${source#reports/diagnostics/} - output="$output_root/diagnostics/${relative%.json}.pdf" + output="$stage/diagnostics/${relative%.json}.pdf" mkdir -p "$(dirname "$output")" scripts/render-capture.sh "$source" "$output" ((count += 1)) @@ -36,12 +53,19 @@ done < <(find reports/diagnostics -type f -name '*.json' -print0 | sort -z) echo "Rendered $count diagnostic PDFs into $output_root/diagnostics" if [[ "$include_json" == true ]]; then - reporter/tools/export-json-all.sh reports/reports --output-dir "$output_root/reports/pdfs" + reporter/tools/export-json-all.sh reports/reports --output-dir "$stage/reports/pdfs" while IFS= read -r -d '' source; do relative=${source#reports/diagnostics/} - mkdir -p "$output_root/diagnostics/$(dirname "$relative")" - cp "$source" "$output_root/diagnostics/$relative" + mkdir -p "$stage/diagnostics/$(dirname "$relative")" + cp "$source" "$stage/diagnostics/$relative" done < <(find reports/diagnostics -type f -name '*.json' -print0) fi +if [[ -d "$output_root" ]]; then + backup=$(mktemp -d "$output_parent/.${output_name}.old.XXXXXXXX") + rmdir -- "$backup" + mv -- "$output_root" "$backup" +fi +mv -- "$stage" "$output_root" +if [[ -n "$backup" ]]; then rm -rf -- "$backup"; backup=; fi echo "Built publishable reports under $output_root"