#!/usr/bin/env bash # # Sync the two swisskyrepo mirrors (PayloadsAllTheThings, InternalAllTheThings) # into the site, prove the result builds, and optionally publish it. # # .github/workflows/sync-mirrors.yml runs this same script with --commit, so a # sync you watch here is the sync the scheduled run performs. The workflow # pushes from a second job that holds the write token and runs none of this; # --push below is for publishing from your own machine. # set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")/.." PATT_PATHS=(src/content/payloads vendor/PayloadsAllTheThings payloads-manifest.json) IATT_PATHS=(src/content/internal internal-manifest.json) usage() { cat <<'EOF' Sync the PayloadsAllTheThings and InternalAllTheThings mirrors into the site. scripts/sync-mirrors.sh sync + validate, leave it for you to read scripts/sync-mirrors.sh --commit ... and commit, one commit per upstream scripts/sync-mirrors.sh --push ... and push to main, which deploys scripts/sync-mirrors.sh --no-checks skip the build and tests (a quick look only) Nothing is committed unless you ask for it, so the bare form is safe to run whenever you want to see what upstream has done since the last sync. EOF } commit=false push=false checks=true while [ $# -gt 0 ]; do case "$1" in --commit) commit=true ;; --push) commit=true; push=true ;; --no-checks) checks=false ;; -h|--help) usage; exit 0 ;; *) echo "unknown option: $1" >&2; usage >&2; exit 2 ;; esac shift done for tool in git python3 node npm; do command -v "$tool" >/dev/null || { echo "missing required tool: $tool" >&2; exit 1; } done work=$(mktemp -d) trap 'rm -rf "$work"' EXIT say() { printf '\n\033[1m==> %s\033[0m\n' "$1"; } # Report to the workflow when running under Actions, so it knows whether there # is anything to deploy. A no-op outside CI. emit_changed() { if [ -n "${GITHUB_OUTPUT:-}" ]; then echo "changed=$1" >> "$GITHUB_OUTPUT" fi } say 'Cloning upstreams at HEAD' git clone --quiet --depth 1 https://github.com/swisskyrepo/PayloadsAllTheThings "$work/patt" git clone --quiet --depth 1 https://github.com/swisskyrepo/InternalAllTheThings "$work/iatt" # 12-char short shas match the pin convention already in the mirrors; a full # 40-char sha would rewrite sourceUrl in every file. PATT_SHA=$(git -C "$work/patt" rev-parse --short=12 HEAD) IATT_SHA=$(git -C "$work/iatt" rev-parse --short=12 HEAD) echo "PayloadsAllTheThings @ $PATT_SHA" echo "InternalAllTheThings @ $IATT_SHA" say 'Regenerating the mirrors' python3 scripts/sync-payloads.py "$work/patt" "$PATT_SHA" python3 scripts/sync-internal.py "$work/iatt" "$IATT_SHA" # Count before building. The build writes dist/ and public/pagefind/, both # gitignored, but naming the paths keeps this honest if that ever changes. patt_files=$(git status --porcelain -- "${PATT_PATHS[@]}" | wc -l | tr -d ' ') iatt_files=$(git status --porcelain -- "${IATT_PATHS[@]}" | wc -l | tr -d ' ') say 'What changed' if [ "$patt_files" -eq 0 ] && [ "$iatt_files" -eq 0 ]; then echo 'Nothing. Both mirrors are already current.' emit_changed false exit 0 fi echo "PayloadsAllTheThings: $patt_files file(s)" echo "InternalAllTheThings: $iatt_files file(s)" printf '\nRead it with: git diff -- %s %s\n' "${PATT_PATHS[*]}" "${IATT_PATHS[*]}" if [ "$checks" = true ]; then say 'Validating' # The site has to build and the suite has to pass before any of this is # allowed to land. With no review gate on the scheduled sync, these are the # only thing standing between upstream and the live vault. [ -d node_modules ] || npm ci npm run build node --test else printf '\nSkipping the build and tests. Do not publish a sync you have not validated.\n' fi if [ "$commit" = false ]; then say 'Done' echo 'Left in the working tree, uncommitted. Re-run with --commit or --push to publish.' emit_changed true exit 0 fi say 'Committing' # Fall back to the Actions identity only when the environment has none of its own. git config user.name >/dev/null 2>&1 || git config user.name 'github-actions[bot]' git config user.email >/dev/null 2>&1 || git config user.email '41898282+github-actions[bot]@users.noreply.github.com' committed=false # Commit one tree, by pathspec, so an index that already had something staged # in it cannot smuggle unrelated changes into a sync commit. commit_tree() { local msg="$1"; shift git add -A -- "$@" if git diff --cached --quiet -- "$@"; then echo "nothing to commit for '$msg' (already at this state)" return 0 fi git commit -q -m "$msg" -- "$@" echo "$msg" committed=true } if [ "$patt_files" -gt 0 ]; then commit_tree "sync: PayloadsAllTheThings @ ${PATT_SHA}" "${PATT_PATHS[@]}" fi if [ "$iatt_files" -gt 0 ]; then commit_tree "sync: InternalAllTheThings @ ${IATT_SHA}" "${IATT_PATHS[@]}" fi if [ "$committed" = false ]; then say 'Done' echo 'Nothing was committed, so there is nothing to publish.' emit_changed false exit 0 fi if [ "$push" = true ]; then say 'Pushing to main' # Retry once through a rebase in case main moved while the build was running. git push origin HEAD:main || { git fetch origin main git rebase origin/main git push origin HEAD:main } fi emit_changed true say 'Done'