diff --git a/.github/actions/build-site/action.yml b/.github/actions/build-site/action.yml new file mode 100644 index 00000000..553ca5d5 --- /dev/null +++ b/.github/actions/build-site/action.yml @@ -0,0 +1,67 @@ +name: Build the website +description: > + Builds a checkout of this repository into /public, with its topics and + commands from valkey-doc and the command metadata from valkey and the modules, checked out + next to it in the workspace. + +inputs: + path: + description: The website checkout to build, relative to the workspace (e.g. website). + required: true + checkout-content: + description: Check out the content repositories. Set to false if an earlier build in the job already did. + default: 'true' + +runs: + using: composite + steps: + - name: Checkout valkey + if: inputs.checkout-content == 'true' + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + repository: valkey-io/valkey + path: valkey + + - name: Checkout valkey-bloom + if: inputs.checkout-content == 'true' + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + repository: valkey-io/valkey-bloom + path: valkey-bloom + + - name: Checkout valkey-doc + if: inputs.checkout-content == 'true' + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + repository: valkey-io/valkey-doc + path: valkey-doc + + - name: Checkout valkey-json + if: inputs.checkout-content == 'true' + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + repository: valkey-io/valkey-json + path: valkey-json + + - name: Checkout valkey-search + if: inputs.checkout-content == 'true' + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + repository: valkey-io/valkey-search + path: valkey-search + + - name: Init commands and topics + shell: bash + working-directory: ${{ inputs.path }} + run: | + ./build/init-topics.sh ../valkey-doc/topics + ./build/init-commands.sh ../valkey-doc/commands \ + ../valkey/src/commands ../valkey-bloom/src/commands \ + ../valkey-json/src/commands ../valkey-search/src/commands + + - name: Build only + uses: shalzz/zola-deploy-action@1be083648a1db2853ce4970e381740c0e2fd06de # v0.23.6 + env: + BUILD_DIR: ${{ inputs.path }} + BUILD_ONLY: true + BUILD_THEMES: false diff --git a/.github/actions/check-links/action.yml b/.github/actions/check-links/action.yml new file mode 100644 index 00000000..159d22b0 --- /dev/null +++ b/.github/actions/check-links/action.yml @@ -0,0 +1,46 @@ +name: Check the website's links +description: > + Checks a built site (/public) for links to missing pages or #anchors. If base-path is + provided, only surfaces issues that don't appear in both. + +inputs: + path: + description: The website to check the build for, relative to the workspace (e.g. website). + required: true + base-path: + description: A checkout of the base commit to compare with. If it has no public directory, every broken link is listed. + default: '' + +runs: + using: composite + steps: + - name: Install lychee + shell: bash + env: + LYCHEE_VERSION: v0.24.2 + LYCHEE_SHA256: 1f4e0ef7f6554a6ed33dd7ac144fb2e1bbed98598e7af973042fc5cd43951c9a + run: | + curl -sSfL -o "$RUNNER_TEMP/lychee.tar.gz" \ + "https://github.com/lycheeverse/lychee/releases/download/lychee-${LYCHEE_VERSION}/lychee-x86_64-unknown-linux-gnu.tar.gz" + echo "$LYCHEE_SHA256 $RUNNER_TEMP/lychee.tar.gz" | sha256sum -c - + tar -xzf "$RUNNER_TEMP/lychee.tar.gz" -C "$RUNNER_TEMP" --strip-components=1 lychee-x86_64-unknown-linux-gnu/lychee + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + + - name: Check links + shell: bash + env: + CHECKOUT: ${{ inputs.path }} + BASE_CHECKOUT: ${{ inputs.base-path }} + run: | + status=0 + for check in broken-links unconverted-doc-links; do + echo "build/$check.sh:" + before=- + if [ -n "$BASE_CHECKOUT" ]; then + before=$RUNNER_TEMP/$check-before.txt + if [ -d "$BASE_CHECKOUT/public" ]; then "$CHECKOUT/build/$check.sh" "$BASE_CHECKOUT/public" > "$before"; fi + fi + "$CHECKOUT/build/$check.sh" "$CHECKOUT/public" > "$RUNNER_TEMP/$check.txt" + "$CHECKOUT/build/compare-link-failures.sh" "$before" "$RUNNER_TEMP/$check.txt" || status=1 + done + exit "$status" diff --git a/.github/workflows/link-check.yml b/.github/workflows/link-check.yml new file mode 100644 index 00000000..5f02e126 --- /dev/null +++ b/.github/workflows/link-check.yml @@ -0,0 +1,59 @@ +# Builds the site before and after the PR. Fails if the PR breaks internal links. +# Links already broken before the PR are listed but don't fail the check. + +name: Link check + +on: + pull_request: + branches: ["main"] + +permissions: + contents: read + +concurrency: + group: link-check-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + internal-links: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + path: website + fetch-depth: 0 # for the base commit + + - name: Check out the base commit + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + run: git -C website worktree add --detach ../base "$BASE_SHA" + + # Each side uses its own build-site, so changes to the build itself are checked too. + # If the base doesn't build or has no build-site, the checks list every broken link + # instead. + - name: Build this PR + id: head + uses: ./website/.github/actions/build-site + with: + path: website + + - name: Build the base + id: base + if: hashFiles('base/.github/actions/build-site/action.yml') != '' + continue-on-error: true + uses: ./base/.github/actions/build-site + with: + path: base + checkout-content: false + + # A failed build can leave part of a site, which could hide links the PR breaks. + - name: Discard a failed base build + if: steps.base.outcome == 'failure' + run: rm -rf base/public + + - name: Check links + uses: ./website/.github/actions/check-links + with: + path: website + base-path: base diff --git a/.github/workflows/zola-deploy.yml b/.github/workflows/zola-deploy.yml index dba6400f..0fb8da39 100644 --- a/.github/workflows/zola-deploy.yml +++ b/.github/workflows/zola-deploy.yml @@ -27,50 +27,10 @@ jobs: ref: ${{ github.event_name != 'pull_request' && 'main' || '' }} path: website - - name: Checkout valkey-doc - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - with: - repository: valkey-io/valkey-doc - path: valkey-doc - - - name: Checkout valkey - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - with: - repository: valkey-io/valkey - path: valkey - - - name: Checkout valkey-bloom - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - with: - repository: valkey-io/valkey-bloom - path: valkey-bloom - - - name: Checkout valkey-search - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - with: - repository: valkey-io/valkey-search - path: valkey-search - - - name: Checkout valkey-json - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + - name: Build + uses: ./website/.github/actions/build-site with: - repository: valkey-io/valkey-json - path: valkey-json - - - name: Init commands and topics - run: | - cd website - ./build/init-topics.sh ../valkey-doc/topics - ./build/init-commands.sh ../valkey-doc/commands \ - ../valkey/src/commands ../valkey-bloom/src/commands \ - ../valkey-json/src/commands ../valkey-search/src/commands - - - name: Build only - uses: shalzz/zola-deploy-action@1be083648a1db2853ce4970e381740c0e2fd06de # v0.23.6 - env: - BUILD_DIR: website - BUILD_ONLY: true - BUILD_THEMES: false + path: website - name: Upload artifact if: github.event_name != 'pull_request' @@ -78,7 +38,8 @@ jobs: with: path: ./website/public - validate: + # Deploys wait for these. + blocking-checks: needs: build if: github.event_name != 'pull_request' uses: ./.github/workflows/validate-libraries.yml @@ -93,8 +54,36 @@ jobs: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest - needs: [build, validate] + needs: [build, blocking-checks] steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4 + + # Deploys don't wait for these. The target of these checks comes from other repositories + # (valkey-doc, valkey, etc.), so blocking would hold back site updates until external fixes could + # be made. The checks here are intended as primarily informational. + non-blocking-checks: + needs: build + if: github.event_name != 'pull_request' + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + ref: ${{ needs.build.outputs.commit }} + + - name: Download the built site + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + name: github-pages + + - name: Unpack the built site + run: mkdir public && tar -xf artifact.tar -C public + + - name: Check links + uses: ./.github/actions/check-links + with: + path: . diff --git a/README.md b/README.md index fc63930c..f4e83bfc 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,17 @@ Point your browser at `http://127.0.0.1:1111/commands/` and you should see the f All files created in this process are ignored by git. Commit your changes to your local copy of `valkey-io/valkey-doc` for description changes and `valkey-io/valkey` for command JSON changes (if you have any). +### Checking links + +Pull requests and deploys check the built site for links to missing pages or `#anchors`, and for valkey-doc links that weren't converted to site URLs. +To run the checks locally (needs [lychee](https://lychee.cli.rs/) and jq), set up the topics and commands as above, then: + +```shell +zola build +build/broken-links.sh public +build/unconverted-doc-links.sh public +``` + ## License This project is licensed under the BSD-3-Clause License. diff --git a/build/broken-links.sh b/build/broken-links.sh new file mode 100755 index 00000000..73e5750d --- /dev/null +++ b/build/broken-links.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Lists links in a built site to missing pages or #anchors. +# +# build/broken-links.sh public +set -euo pipefail +site=$(cd "${1:?usage: broken-links.sh }" && pwd -P) +report=$(mktemp) + +status=0 +# --offline skips links written as https://valkey.io/..., so point them at the build. +lychee --offline --no-progress --format json --output "$report" --config "$(dirname "$0")/lychee.toml" \ + --root-dir "$site" --remap "^https://valkey\.io/([^?#]*)[^#]*(#.*)?$ file://$site/\$1\$2" \ + "$site" > /dev/null || status=$? +case $status in + 0 | 2) ;; # 2 means lychee found broken links + *) echo "lychee itself failed (exit code $status)" >&2; exit 1 ;; +esac + +jq -r --arg root "$site" '.error_map | to_entries[] | .key as $page | .value[] + | (.url | ltrimstr("file://" + $root)) as $link + | ($page | ltrimstr($root) | rtrimstr("index.html")) as $on + | (if (.status.text | startswith("Cannot find fragment")) then "but that page has no #\($link | split("#") | last) anchor" + elif (.status.text | startswith("File not found")) then "but no such page exists" + else .status.text end) as $problem + | [$link, "\($on) links to \($link), \($problem)"] | @tsv' "$report" \ + | sort -u diff --git a/build/compare-link-failures.sh b/build/compare-link-failures.sh new file mode 100755 index 00000000..2f587bcf --- /dev/null +++ b/build/compare-link-failures.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# Prints failures from broken-links.sh or unconverted-doc-links.sh. +# +# build/compare-link-failures.sh +# Fails if a link in isn't in . If doesn't exist (the base +# didn't build), lists everything as below. +# build/compare-link-failures.sh - +# Fails if there are any failures. +# +# Compares the first field of each failure. broken-links.sh puts only the link there, so a new +# page that inherits a dead footer link doesn't fail. +set -euo pipefail +before=${1:?usage: compare-link-failures.sh } after=${2:?} + +if [ "$before" != - ] && [ ! -e "$before" ]; then + echo "The base commit didn't build, so every broken link is listed." + before=- +fi + +if [ "$before" = - ]; then + if [ ! -s "$after" ]; then + echo "No broken links." + exit 0 + fi + echo "Broken links ($(wc -l < "$after" | tr -d ' ')):" + cut -f2 "$after" | sed 's/^/::error::/' + exit 1 +fi + +new_links=$(comm -13 <(cut -f1 "$before" | sort -u) <(cut -f1 "$after" | sort -u)) + +# Each failure is "linkmessage". Group the messages by whether their link is new. +new=() already=() +while IFS=$'\t' read -r link message; do + if grep -qxF -- "$link" <<< "$new_links"; then + new+=("$message") + else + already+=("$message") + fi +done < "$after" + +if [ ${#new[@]} -gt 0 ]; then + echo "Broken by this change (${#new[@]}):" + printf '::error::%s\n' "${new[@]}" +else + echo "This change doesn't break any links." +fi +if [ ${#already[@]} -gt 0 ]; then + echo "Already broken before this change (${#already[@]}):" + printf ' %s\n' "${already[@]}" +fi +if [ ${#new[@]} -gt 0 ]; then + exit 1 +fi diff --git a/build/lychee.toml b/build/lychee.toml new file mode 100644 index 00000000..5b044913 --- /dev/null +++ b/build/lychee.toml @@ -0,0 +1,7 @@ +# Used by build/broken-links.sh. + +# Not "full", because lychee's #:~:text= fragment checking is new and was unreliable in testing. +include_fragments = "anchor-only" + +# Zola writes /topics/foo/ as topics/foo/index.html. +index_files = ["index.html"] diff --git a/build/unconverted-doc-links.sh b/build/unconverted-doc-links.sh new file mode 100755 index 00000000..f1517506 --- /dev/null +++ b/build/unconverted-doc-links.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Lists valkey-doc links that fix_links (templates/macros/) didn't convert to site URLs. +# The browser resolves these against the page URL, so they can open the wrong page. +# For example, ../clients/ on /topics/installation/ opens /topics/clients/. +# +# build/unconverted-doc-links.sh public +set -euo pipefail +cd "${1:?usage: unconverted-doc-links.sh }" + +# hrefs on topic and command pages that still start with ../ or point at a .md file, as +# valkey-doc writes them. [^":] skips links with a scheme, such as https://…/README.md. +unconverted='href="(\.\./[^"]*|[^":]*\.md(#[^"]*)?)"' + +# grep prints one "topics/x/index.html:href=\"../y.md\"" per match. It exits 1 when nothing +# matches, which means the site is clean. +# Keyed by page too, since the same link opens a different page under topics and commands. +{ grep -roE "$unconverted" topics commands --include=index.html || [ $? -eq 1 ]; } | + while IFS= read -r match; do + page=/${match%%index.html:*} # topics/x/index.html:… → /topics/x/ + link=${match#*:href=\"} # …:href="../y.md" → ../y.md" + link=${link%\"} # → ../y.md + printf '%s\t%s links to %s, a valkey-doc link that was not converted to a site URL\n' \ + "$page $link" "$page" "$link" + done | sort -u