diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a914f40ac2..e0a32122e9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -53,6 +53,7 @@ jobs: - lint - test - unused-media + - test-go-redirects steps: - name: Checkout diff --git a/Dockerfile b/Dockerfile index 2026f6a27e..0d67086695 100644 --- a/Dockerfile +++ b/Dockerfile @@ -95,3 +95,12 @@ COPY --from=pagefind /pagefind . FROM scratch AS release COPY --from=build /out / COPY --from=pagefind /pagefind /pagefind + +FROM alpine:${ALPINE_VERSION} AS test-go-redirects +WORKDIR /work +RUN apk add yq +COPY --from=build /out ./public +RUN --mount=type=bind,target=. <<"EOT" +set -ex +./scripts/test_go_redirects.sh +EOT diff --git a/docker-bake.hcl b/docker-bake.hcl index 12c327b361..18e64f5a83 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -33,7 +33,7 @@ target "release" { } group "validate" { - targets = ["lint", "test", "unused-media"] + targets = ["lint", "test", "unused-media", "test-go-redirects"] } target "test" { @@ -51,6 +51,11 @@ target "unused-media" { output = ["type=cacheonly"] } +target "test-go-redirects" { + target = "test-go-redirects" + output = ["type=cacheonly"] +} + # # releaser targets are defined in _releaser/Dockerfile # and are used for Netlify and AWS S3 deployment diff --git a/scripts/test_go_redirects.sh b/scripts/test_go_redirects.sh new file mode 100755 index 0000000000..70dab0fb70 --- /dev/null +++ b/scripts/test_go_redirects.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env sh + +echo "Testing redirects in data/redirects.yml..." + +# Get all redirects from data/redirects.yml +REDIRECTS=$(yq eval 'keys | .[]' data/redirects.yml) +LOCAL_REDIRECTS=$(echo $REDIRECTS | awk 'BEGIN { RS = " " } /^\// { print $1 }') + +echo "Checking for duplicate redirects..." +DUPLICATES=$(echo $LOCAL_REDIRECTS | tr ' ' '\n' | sort | uniq -d) +if [ -n "$DUPLICATES" ]; then + echo "Duplicate redirects found:" + echo $DUPLICATES + exit 1 +fi + +echo "Checking for redirects to nonexistent paths..." +for file in $(echo REDIRECTS | awk 'BEGIN { RS = " " } /^\// { print $1 }'); do + if [ ! -e "./public/${file%/*}/index.html" ]; then + echo "Redirect to nonexistent path: $file" + exit 1 + fi +done + +echo "All redirects are valid."