#!/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` (e.g. -auto-approve). set -euo pipefail 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") 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 "$@" 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: created CloudFront invalidation ${invalidation}"