#!/usr/bin/env bash # Build the longread WASM SPA, upload it as an immutable release tree under # s3://threadbare.substandard.blog/releases//, then run `tofu apply`, # which only flips CloudFront's origin_path to the new tree (the sha is handed # to tofu as threadbare_release_sha in infra/terraform.tfvars.json, patched here). # # The subdomain shares infra/terraform.tfvars.json with deploy-site.sh. That # script owns release_sha; this one preserves it, and reads the value from # tofu state when the file is new so a fresh worktree can deploy the SPA # without redeploying the frontpage. # # Extra arguments are passed through to `tofu apply`. The apply is # auto-approved by default; pass -auto-approve=false to review it first. set -euo pipefail tofu_args=() auto_approve=0 for arg in "$@"; do tofu_args+=("${arg}") case "${arg}" in -auto-approve|--auto-approve|-auto-approve=*|--auto-approve=*) auto_approve=1 ;; esac done if (( auto_approve == 0 )); then tofu_args=(-auto-approve "${tofu_args[@]}") fi repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" bucket="threadbare.substandard.blog" tfvars="${repo_root}/infra/terraform.tfvars.json" if ! identity="$(aws sts get-caller-identity --output text --query Arn 2>&1)"; then echo "deploy-threadbare: AWS credential check failed:" >&2 echo " ${identity}" >&2 echo "deploy-threadbare: configure credentials for the substandard.blog account, then re-run." >&2 exit 1 fi echo "deploy-threadbare: deploying as ${identity}" npm --prefix "${repo_root}/threadbare" ci npm --prefix "${repo_root}/threadbare" run build release="$(git -C "${repo_root}" rev-parse --short=12 HEAD)" if [[ -n "$(git -C "${repo_root}" status --porcelain --untracked-files=no)" ]]; then release="${release}-dirty" echo "deploy-threadbare: uncommitted changes; deploying as ${release}" fi prefix="releases/${release}" echo "deploy-threadbare: release ${release}" # The frontpage's release_sha has no default in infra/main.tf; preserve the # value already in tfvars, or recover it from state in a fresh worktree. if ! node -e ' const fs = require("node:fs") const file = process.argv[1] const vars = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, "utf8")) : {} process.exit(vars.release_sha ? 0 : 1) ' "${tfvars}" 2>/dev/null; then existing="$(tofu -chdir="${repo_root}/infra" output -raw release_sha 2>/dev/null || true)" if [[ -z "${existing}" ]]; then echo "deploy-threadbare: no release_sha in ${tfvars} or tofu state." >&2 echo "deploy-threadbare: run scripts/deploy-site.sh once, then re-run this script." >&2 exit 1 fi node "${repo_root}/scripts/patch-tfvars.mjs" "${tfvars}" release_sha "${existing}" fi node "${repo_root}/scripts/patch-tfvars.mjs" \ "${tfvars}" threadbare_release_sha "${release}" if ! aws s3api head-bucket --bucket "${bucket}" >/dev/null 2>&1; then echo "deploy-threadbare: s3://${bucket} does not exist yet (first deploy?)." >&2 echo "deploy-threadbare: run \`tofu -chdir=infra apply\` to create the infra" >&2 echo "deploy-threadbare: (infra/terraform.tfvars.json is already written), then re-run this script." >&2 exit 1 fi content_type() { case "$1" in *.html) echo "text/html; charset=utf-8" ;; *.css) echo "text/css; charset=utf-8" ;; *.js) echo "text/javascript; charset=utf-8" ;; *.json) echo "application/json" ;; *.wasm) echo "application/wasm" ;; *.png) echo "image/png" ;; *.svg) echo "image/svg+xml" ;; *.ico) echo "image/x-icon" ;; *.txt) echo "text/plain; charset=utf-8" ;; *.woff) echo "font/woff" ;; *.woff2) echo "font/woff2" ;; *.webp) echo "image/webp" ;; *) echo "application/octet-stream" ;; esac } upload() { local file="$1" key="$2" local args=(--content-type "$(content_type "${key}")") # Vite puts a content hash in every asset filename, so those can be cached # forever. index.html names them, so it must be revalidated every time. if [[ "${key}" == assets/* ]]; then args+=(--cache-control "public, max-age=31536000, immutable") elif [[ "${key}" == index.html ]]; then args+=(--cache-control "no-cache") elif [[ "${key}" == client-metadata.json ]]; then # The OAuth authorization server fetches this by URL. Keep changes visible. args+=(--cache-control "public, max-age=300") fi aws s3 cp --no-progress "${file}" "s3://${bucket}/${prefix}/${key}" "${args[@]}" } upload_from_dist() { upload "$1" "${1#"${repo_root}/threadbare/dist/"}" } export -f upload upload_from_dist content_type export bucket prefix repo_root find "${repo_root}/threadbare/dist" -type f -print0 \ | xargs -0 -r -P 10 -I {} bash -c 'upload_from_dist "$@"' _ {} echo "deploy-threadbare: uploaded release to s3://${bucket}/${prefix}/" tofu -chdir="${repo_root}/infra" apply "${tofu_args[@]}" dist_id="$(tofu -chdir="${repo_root}/infra" output -raw threadbare_distribution_id)" echo "deploy-threadbare: waiting for CloudFront distribution ${dist_id} to deploy" aws cloudfront wait distribution-deployed --id "${dist_id}" invalidation="$(aws cloudfront create-invalidation --distribution-id "${dist_id}" \ --paths "/*" --output text --query Invalidation.Id)" echo "deploy-threadbare: waiting for invalidation ${invalidation}" aws cloudfront wait invalidation-completed --distribution-id "${dist_id}" --id "${invalidation}" # CloudFront can still serve a cached 403 briefly after the invalidation # completes, so retry the live checks before failing the deploy. site="https://threadbare.substandard.blog" tmp="$(mktemp -d)" trap 'rm -rf "${tmp}"' EXIT metadata_matches() { local status status="$(curl -sS --max-time 30 -o "${tmp}/client-metadata.json" \ -w '%{http_code}' "${site}/client-metadata.json")" || return 1 [[ "${status}" == "200" ]] || return 1 node -e ' const fs = require("node:fs") const [built, live] = process.argv.slice(1).map((file) => JSON.parse(fs.readFileSync(file, "utf8"))) process.exit(JSON.stringify(built) === JSON.stringify(live) ? 0 : 1) ' "${repo_root}/threadbare/dist/client-metadata.json" "${tmp}/client-metadata.json" } homepage_is_html() { local status status="$(curl -sS --max-time 30 -o "${tmp}/index.html" \ -w '%{http_code} %{content_type}' "${site}/")" || return 1 [[ "${status}" == "200 text/html"* ]] } verified=false for attempt in 1 2 3 4 5 6; do if metadata_matches && homepage_is_html; then verified=true break fi echo "deploy-threadbare: ${site} not live yet (attempt ${attempt}/6)" sleep 10 done if [[ "${verified}" != true ]]; then echo "deploy-threadbare: deployed, but ${site} is not serving this release:" >&2 echo " expected /client-metadata.json to match threadbare/dist/client-metadata.json" >&2 echo " and / to return 200 HTML." >&2 exit 1 fi echo "deploy-threadbare: verified ${site} serves release ${release}"