diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.yml index 5f36e3fc..67906648 100644 --- a/.github/workflows/deploy-site.yml +++ b/.github/workflows/deploy-site.yml @@ -7,11 +7,9 @@ on: - 'docs-site/**' workflow_dispatch: -# Allow this job to clone the repo and create a page deployment +# Only the deploy job can publish. permissions: contents: read - pages: write - id-token: write jobs: build: @@ -23,10 +21,15 @@ jobs: uses: withastro/action@e84f40bd8d2caa9e768ec82ad30dd81f0b280853 # v6 with: path: ./docs-site # The root location of your Astro project inside the repository. + # Also add copies of the search index for other Valkey sites to consume. + build-cmd: npm run build && ./scripts/index-copies.sh build deploy: needs: build runs-on: ubuntu-latest + permissions: + pages: write + id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} diff --git a/.github/workflows/docs-site.yml b/.github/workflows/docs-site.yml new file mode 100644 index 00000000..23267f86 --- /dev/null +++ b/.github/workflows/docs-site.yml @@ -0,0 +1,35 @@ +name: Docs site + +on: + pull_request: + branches: [ main ] + paths: + - 'docs-site/**' + - '.github/workflows/docs-site.yml' + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + defaults: + run: + working-directory: docs-site + steps: + - name: Checkout code + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + persist-credentials: false + - name: Set up Node.js + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '24' + cache: npm + cache-dependency-path: docs-site/package-lock.json + - name: Install dependencies + run: npm ci + - name: Build the docs site + run: npm run build + - name: Check the Pagefind index version served by other sites is compatible with this site + run: ./scripts/index-copies.sh check diff --git a/docs-site/astro.config.mjs b/docs-site/astro.config.mjs index e9707c65..537af48d 100644 --- a/docs-site/astro.config.mjs +++ b/docs-site/astro.config.mjs @@ -8,6 +8,7 @@ export default defineConfig({ integrations: [ starlight({ title: 'Valkey Admin', + components: { Search: './src/components/Search.astro' }, head: [ { tag: 'script', diff --git a/docs-site/scripts/index-copies.sh b/docs-site/scripts/index-copies.sh new file mode 100755 index 00000000..4a6c5606 --- /dev/null +++ b/docs-site/scripts/index-copies.sh @@ -0,0 +1,129 @@ +#!/usr/bin/env bash +# Builds copies of this site's search index at /pagefind/v/, one for every Pagefind minor version from the +# oldest a Valkey site runs up to the newest release. Pagefind only merges indexes built with its own minor version, so +# this site must provide any version other sites may need. +# +# scripts/index-copies.sh build produces the necessary indexes for other sites to consume +# scripts/index-copies.sh check checks current Pagefind is compatible with indexes hosted by other sites +# +# Both fail if a site that has set up cross-site search has no copy for this build's Pagefind minor version yet. +# Redeploying that site usually adds the copy. +# +# Each Valkey site with cross-site search has a copy of this script. Keep the copies in sync. + +set -euo pipefail +case ${1:-} in build | check) ;; *) echo "usage: $0 build|check" >&2; exit 2 ;; esac + +cd "$(dirname "$0")/.." +OTHER_SITES=$(jq -r '.otherSites[]' src/cross-site-search.json) +TMP=$(mktemp -d) +trap 'rm -rf "$TMP"' EXIT + +minor_version() { cut -d. -f1-2 <<< "$1"; } # 1.5.2 -> 1.5 + +# Prints the Pagefind version of the index at $1, "none" if it has no usable index (a 404, or an entry file without a +# release number), or "down" for anything else, such as a 503 or no answer. +index_state() { + local status version + rm -f "$TMP/entry.json" # clean any prior state + status=$(curl -sS --retry 3 --retry-connrefused --max-time 20 -o "$TMP/entry.json" -w '%{http_code}' \ + "${1}pagefind-entry.json" 2>/dev/null) || status=failed + case $status in + 404) echo none; return ;; + 200) ;; + *) echo down; return ;; + esac + version=$(jq -r .version "$TMP/entry.json" 2>/dev/null) || version= + if [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "$version" + else + echo none + fi +} + +check_other_sites() { + local this_minor index version copy_version missing=() + this_minor=$(minor_version "$1") + for index in $OTHER_SITES; do + # Check whether the site has any Pagefind index at all. + version=$(index_state "$index") + case $version in + none) continue ;; + down) missing+=("$index"); continue ;; + esac + # Check whether the site publishes its own version-specific index version. + case $(index_state "${index}v$(minor_version "$version")/") in + none) continue ;; # hasn't set up cross-site search + down) missing+=("$index"); continue ;; + esac + # Check whether the needed version is available. + copy_version=$(index_state "${index}v${this_minor}/") + [ "$(minor_version "$copy_version")" = "$this_minor" ] || missing+=("$index") + done + [ "${#missing[@]}" -gt 0 ] || return 0 + + cat >&2 << EOF +::error::These sites have no copy of their search index for Pagefind ${this_minor} yet, or can't be reached: ${missing[*]} +Deployed now, this site's search would leave them out. Either: + - redeploy them first, which adds a ${this_minor} copy if ${this_minor} is within the versions they publish. + - keep this site on the Pagefind version it uses now. +EOF + return 1 +} + +# Index versions to publish from this site. Finds the oldest version a Valkey site uses, and gathers each minor version +# of Pagefind published since up to the newest release. +versions_to_copy() { + local in_use=("$1") index state oldest + for index in $OTHER_SITES; do + state=$(index_state "$index") + case $state in + none) echo "::warning::${index} has no index; its version isn't counted" >&2 ;; + # Without its version, this deploy could stop publishing the copy that site's search uses. + down) echo "::error::${index} can't be reached; re-run this deploy once it can" >&2; return 1 ;; + *) echo "${index} runs Pagefind ${state}" >&2; in_use+=("$state") ;; + esac + done + oldest=$(printf '%s\n' "${in_use[@]}" | sort -V | head -1) + curl -fsS --retry 3 --retry-connrefused --max-time 20 https://registry.npmjs.org/pagefind | jq -r --arg oldest "$(minor_version "$oldest")" ' + def minor: split(".")[:2] | map(tonumber); + .versions | keys + | map(select(test("^[0-9]+\\.[0-9]+\\.[0-9]+$"))) # releases, not pre-releases + | map(select(minor >= ($oldest | minor))) # from the oldest minor version in use + | group_by(minor) | map(max_by(split(".") | map(tonumber))) | .[] # the newest patch of each + ' +} + +# Downloads Pagefind $1 to $TMP/$1/pagefind. +download_pagefind() { + local dir asset url + dir=$TMP/$1 + case $(uname -s)-$(uname -m) in + Linux-x86_64 | Linux-aarch64) asset="pagefind-v$1-$(uname -m)-unknown-linux-musl.tar.gz" ;; + Darwin-arm64) asset="pagefind-v$1-aarch64-apple-darwin.tar.gz" ;; + Darwin-x86_64) asset="pagefind-v$1-x86_64-apple-darwin.tar.gz" ;; + *) echo "no Pagefind release for $(uname -s) $(uname -m)" >&2; return 1 ;; + esac + url="https://github.com/Pagefind/pagefind/releases/download/v$1/${asset}" + mkdir -p "$dir" + curl -fsSL --retry 3 --max-time 300 "$url" -o "$dir/$asset" + curl -fsSL --retry 3 --max-time 60 "$url.sha256" -o "$dir/$asset.sha256" + if command -v sha256sum > /dev/null; then + (cd "$dir" && sha256sum -c --quiet "$asset.sha256") + else + (cd "$dir" && shasum -a 256 -c --quiet "$asset.sha256") # macOS has shasum instead + fi + tar -xzf "$dir/$asset" -C "$dir" +} + +this_version=$(jq -er .version dist/pagefind/pagefind-entry.json) +check_other_sites "$this_version" +[ "$1" = build ] || exit 0 + +# Through a file, since a failure inside $(...) wouldn't stop the script. +versions_to_copy "$this_version" > "$TMP/versions" +for version in $(< "$TMP/versions"); do + echo "Building a Pagefind ${version} copy at pagefind/v$(minor_version "$version")/" + download_pagefind "$version" + "$TMP/$version/pagefind" --site dist --output-path "dist/pagefind/v$(minor_version "$version")" +done diff --git a/docs-site/src/components/Search.astro b/docs-site/src/components/Search.astro new file mode 100644 index 00000000..42d4682a --- /dev/null +++ b/docs-site/src/components/Search.astro @@ -0,0 +1,138 @@ +--- +// Adds the other Valkey sites' results to Starlight's search box. A stopgap until Pagefind PR #1361 and Starlight PR #4307 +// are released, after which Starlight's `pagefind.mergeIndex` setting can replace the + + diff --git a/docs-site/src/cross-site-search.json b/docs-site/src/cross-site-search.json new file mode 100644 index 00000000..2e462c4d --- /dev/null +++ b/docs-site/src/cross-site-search.json @@ -0,0 +1,5 @@ +{ + "label": "Valkey Admin", + "otherSites": ["https://valkey.io/pagefind/", "https://glide.valkey.io/pagefind/", "https://spring.valkey.io/pagefind/"], + "otherSitesIndexWeight": 0.1 +}