#!/usr/bin/env bash # Copies the release-publishing values into GitHub Actions repository secrets # on the macOS build mirror. # # Where each value comes from (first match wins, per value): # 1. the process environment, when the variable is set (even to an empty # string, which is then refused). A value taken from the environment is # never written to a file. # R2_ACCOUNT_ID R2_BUCKET R2_ACCESS_KEY_ID R2_SECRET_ACCESS_KEY # R2_PUBLIC_URL ATP_APP_PASSWORD SPINDLE_SERVER_OWNER # 2. otherwise the local files, read only for the values still missing: # ~/.config/peek-releases-r2.env R2_* values # ~/.config/entangle/env TANGLED_APP_PASSWORD (-> ATP_APP_PASSWORD) # ~/.config/spindle/spindle.conf SPINDLE_SERVER_OWNER (the owner DID) # The repo DID: TANGLED_REPO_REPO_DID from the environment, otherwise read # from the public sh.tangled.repo record (PDS_ENDPOINT from the environment, # otherwise resolved from the owner DID at plc.directory). # # Refusal rule: all eight values (credentials and config alike) must be # non-empty and must not be an unresolved reference -- pass://, op://, # secret://, ${...}, {{...}} or a REPLACE_ME-style placeholder. A refused value # stops the script before any network call or `gh` invocation; the message # names the variable and the reason, never the value. # # Values reach `gh secret set` on stdin only. Nothing prints a value; the # listing shows names and lengths. This script never calls `gh auth`. # # Usage: # scripts/set-github-secrets.sh # set the secrets # scripts/set-github-secrets.sh --dry-run # names and lengths only. NOT an # # offline test: it still makes # # public network lookups. # scripts/set-github-secrets.sh --offline # the offline mode (use this to # # test). No DNS, no HTTP, no `gh` # # call; lists names and lengths. # # Needs TANGLED_REPO_REPO_DID # # (PDS_ENDPOINT is only used for # # a lookup, which offline never # # does) or stops saying so. # # Set GITHUB_REPO to override the target (default autonome/peek-builds). set -euo pipefail repo="${GITHUB_REPO:-autonome/peek-builds}" dry_run=0 offline=0 case "${1:-}" in --dry-run) dry_run=1 ;; --offline) offline=1 ;; "") ;; *) echo "usage: $0 [--dry-run | --offline]" >&2 exit 2 ;; esac r2_env="${HOME}/.config/peek-releases-r2.env" atproto_env="${HOME}/.config/entangle/env" spindle_conf="${HOME}/.config/spindle/spindle.conf" read_env() { # [ -f "$1" ] || return 0 awk -F= -v key="$2" '$1 == key { sub(/^[^=]*=/, ""); print; exit }' "$1" } # resolve : environment first, then the file. resolve() { if [ -n "${!1+x}" ]; then printf '%s' "${!1}" else read_env "$2" "$3" fi } # refuse_reason : prints why a value is unusable; prints nothing if fine. refuse_reason() { local v=$1 if [ -z "$v" ]; then echo "it is empty" return fi case "$v" in pass://* | op://* | secret://*) echo "it is an unresolved vault reference" ;; *'${'*'}'* | *'{{'*'}}'*) echo "it is an unresolved template reference" ;; *) if grep -Eqi '(^|[^a-z0-9])(replace|change)[_-]?me([^a-z0-9]|$)' <<<"$v"; then echo "it is a placeholder" fi ;; esac } names=( TANGLED_REPO_DID TANGLED_REPO_REPO_DID R2_ACCOUNT_ID R2_BUCKET R2_ACCESS_KEY_ID R2_SECRET_ACCESS_KEY R2_PUBLIC_URL ATP_APP_PASSWORD ) owner=$(resolve SPINDLE_SERVER_OWNER "$spindle_conf" SPINDLE_SERVER_OWNER) repo_did="" if [ -n "${TANGLED_REPO_REPO_DID+x}" ]; then repo_did=$TANGLED_REPO_REPO_DID fi # Everything that needs no network is checked first. refused=0 check() { # local why why=$(refuse_reason "$2") if [ -n "$why" ]; then echo "refusing $1: $why" >&2 refused=1 fi } values=( "$owner" "$repo_did" "$(resolve R2_ACCOUNT_ID "$r2_env" R2_ACCOUNT_ID)" "$(resolve R2_BUCKET "$r2_env" R2_BUCKET)" "$(resolve R2_ACCESS_KEY_ID "$r2_env" R2_ACCESS_KEY_ID)" "$(resolve R2_SECRET_ACCESS_KEY "$r2_env" R2_SECRET_ACCESS_KEY)" "$(resolve R2_PUBLIC_URL "$r2_env" R2_PUBLIC_URL)" "$(resolve ATP_APP_PASSWORD "$atproto_env" TANGLED_APP_PASSWORD)" ) for i in "${!names[@]}"; do # The repo DID may still be fetched below, unless it was supplied or this is # an offline run (which cannot fetch it). if [ "$i" -eq 1 ] && [ -z "${TANGLED_REPO_REPO_DID+x}" ] && [ "$offline" -eq 0 ]; then continue fi label=${names[$i]} [ "$i" -eq 0 ] && label="SPINDLE_SERVER_OWNER (secret ${names[$i]})" check "$label" "${values[$i]}" done if [ "$refused" -eq 1 ]; then if [ -z "${TANGLED_REPO_REPO_DID+x}" ] && [ "$offline" -eq 1 ]; then echo "offline: TANGLED_REPO_REPO_DID would have been fetched from the public sh.tangled.repo record; supply it (and PDS_ENDPOINT if known)" >&2 fi exit 1 fi if [ -z "${TANGLED_REPO_REPO_DID+x}" ]; then pds="${PDS_ENDPOINT:-}" if [ -z "$pds" ]; then did_doc=$(curl -fsS "https://plc.directory/$owner") pds=$(jq -r '.service[] | select(.id == "#atproto_pds") | .serviceEndpoint' <<<"$did_doc") fi [ -n "$pds" ] || { echo "could not resolve the PDS for the owner DID" >&2 exit 1 } # The sh.tangled.repo record for burrito.space/peek. Reading it (rather than # listing every repo) keeps the repo DID in sync with Tangled without paging # through the owner's whole repo list. repo_record=$(curl -fsS -G \ --data-urlencode "repo=$owner" \ --data-urlencode "collection=sh.tangled.repo" \ --data-urlencode "rkey=3mcpaj34xj622" \ "$pds/xrpc/com.atproto.repo.getRecord") repo_did=$(jq -r '.value.repoDid // empty' <<<"$repo_record") values[1]=$repo_did why=$(refuse_reason "$repo_did") if [ -n "$why" ]; then echo "refusing TANGLED_REPO_REPO_DID: $why (could not read the peek sh.tangled.repo record)" >&2 exit 1 fi fi echo "target: $repo" if [ "$offline" -eq 1 ]; then echo "offline: no network lookups and no gh calls; names and lengths only" fi for i in "${!names[@]}"; do name=${names[$i]} value=${values[$i]} if [ "$dry_run" -eq 1 ] || [ "$offline" -eq 1 ]; then printf '%s (%d chars)\n' "$name" "${#value}" else printf '%s' "$value" | gh secret set "$name" --repo "$repo" >/dev/null printf '%s set\n' "$name" fi done