Skip to content

Commit 6221ef3

Browse files
authored
Merge pull request #34 from Priyankasaggu11929/fix-netlify-mdbook-build
improve mdbook book setup and fix netlify build
2 parents 5c51123 + 5a19a28 commit 6221ef3

File tree

4 files changed

+173
-7
lines changed

4 files changed

+173
-7
lines changed

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,23 @@ $(GOLANGCI_LINT): # Build golangci-lint from tools folder.
306306

307307
$(GOLANGCI_LINT_KAL): $(GOLANGCI_LINT) # Build golangci-lint-kal from custom configuration.
308308
cd $(TOOLS_DIR); $(GOLANGCI_LINT) custom
309+
310+
311+
## --------------------------------------
312+
## Documentation
313+
## --------------------------------------
314+
315+
##@ docs
316+
317+
MDBOOK_VERSION ?= 0.5.2
318+
GO_VERSION ?= 1.25.5
319+
MDBOOK_SCRIPT := $(ROOT_DIR)/docs/book/install-and-build-mdbook.sh
320+
321+
322+
.PHONY: docs
323+
docs: ## Build the mdBook locally using the same script Netlify uses.
324+
GO_VERSION=$(GO_VERSION) MDBOOK_VERSION=$(MDBOOK_VERSION) $(MDBOOK_SCRIPT)
325+
326+
.PHONY: docs-serve
327+
docs-serve: ## Serve mdBook locally.
328+
GO_VERSION=$(GO_VERSION) MDBOOK_VERSION=$(MDBOOK_VERSION) $(MDBOOK_SCRIPT) serve docs/book --open

docs/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## Documentation (mdBook)
2+
3+
This repository’s documentation is built using [**mdBook**](https://github.com/rust-lang-nursery/mdBook).
4+
5+
Local builds and hosted builds use the same build script [1] to ensure consistent behavior.
6+
7+
## Local documentation workflows
8+
9+
All documentation targets **must be run from the repository root**.
10+
11+
### Available Makefile targets
12+
13+
```bash
14+
make docs # Build the mdBook locally using the shared build script
15+
make docs-serve # Serve the mdBook locally
16+
```
17+
18+
## Build script contract
19+
20+
The documentation build is driven by:
21+
22+
```
23+
docs/book/install-and-build-mdbook.sh
24+
```
25+
26+
This script defines how mdBook is installed and executed.
27+
28+
### Environment variables
29+
30+
The build relies on the following environment variables:
31+
32+
| Variable | Description |
33+
| ---------------- | -------------------------------- |
34+
| `GO_VERSION` | Go version used during the build |
35+
| `MDBOOK_VERSION` | mdBook version to install/use |
36+
37+
38+
## Version synchronization requirements
39+
40+
The environment variables used for documentation builds must be kept **consistent across the repository**.
41+
42+
When either `GO_VERSION` or `MDBOOK_VERSION` is changed, the update **must be applied in all of the following locations**:
43+
44+
1. **Makefile**
45+
Used for local documentation builds and serving.
46+
47+
2. **`netlify.toml`**
48+
Used for hosted and preview builds.
49+
50+
3. **`docs/book/install-and-build-mdbook.sh`**
51+
Used as the canonical build script.
52+
53+
## Editing documentation
54+
55+
1. Make changes under:
56+
57+
```
58+
docs/book/src/
59+
```
60+
2. Preview changes locally:
61+
62+
```bash
63+
make docs-serve
64+
```
65+
3. Commit only source files; generated output is produced during the build.
66+
67+
---
68+
69+
[1] [`docs/book/install-and-build-mdbook.sh`](book/install-and-build-mdbook.sh)
70+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
# Copyright The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
#
18+
# Note - This script is adapted from https://github.com/kubernetes-sigs/kubebuilder/blob/master/docs/book/install-and-build.sh
19+
20+
set -e
21+
22+
# The following code is required to allow the preview works with an upper go version
23+
# More info : https://community.netlify.com/t/go-version-1-13/5680
24+
# Get the directory that this script file is in
25+
THIS_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
26+
27+
cd "$THIS_DIR"
28+
29+
if [[ -n "$(command -v gimme)" ]]; then
30+
GO_VERSION=${GO_VERSION:-stable} # Use the provided GO_VERSION or default to 'stable'
31+
eval "$(gimme $GO_VERSION)"
32+
fi
33+
echo go version
34+
35+
# detect OS + ARCH for mdBook
36+
os=$(go env GOOS)
37+
arch=$(go env GOARCH)
38+
39+
# translate arch to rust's conventions (if we can)
40+
if [[ ${arch} == "amd64" || ${arch} == "x86" ]]; then
41+
arch="x86_64"
42+
elif [[ ${arch} == "arm64" ]]; then
43+
arch="aarch64"
44+
fi
45+
46+
# translate os to rust's conventions (if we can)
47+
ext="tar.gz"
48+
cmd="tar -C /tmp -xzvf"
49+
case ${os} in
50+
windows)
51+
target="pc-windows-msvc"
52+
ext="zip"
53+
cmd="unzip -d /tmp"
54+
;;
55+
darwin)
56+
target="apple-darwin"
57+
;;
58+
linux)
59+
# works for linux, too
60+
target="unknown-${os}-musl"
61+
;;
62+
*)
63+
target="unknown-${os}"
64+
;;
65+
esac
66+
67+
# grab mdbook
68+
# we hardcode linux/amd64 since rust uses a different naming scheme and it's a pain to tran
69+
MDBOOK_VERSION=${MDBOOK_VERSION:-"0.5.0"}
70+
MDBOOK_BASENAME="mdBook-v${MDBOOK_VERSION}-${arch}-${target}"
71+
MDBOOK_URL="https://github.com/rust-lang/mdBook/releases/download/v${MDBOOK_VERSION}/${MDBOOK_BASENAME}.${ext}"
72+
73+
echo "downloading ${MDBOOK_BASENAME}.${ext} from ${MDBOOK_URL}"
74+
set -x
75+
curl -fL -o /tmp/mdbook.${ext} "${MDBOOK_URL}"
76+
${cmd} /tmp/mdbook.${ext}
77+
chmod +x /tmp/mdbook
78+
79+
verb=${1:-build}
80+
/tmp/mdbook ${verb}

netlify.toml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
[build]
22
base = "docs/book"
3-
command = "mdbook build"
4-
publish = "docs/book/book"
5-
3+
command = "GO_VERSION=1.25.5 MDBOOK_VERSION=0.5.2 ./install-and-build-mdbook.sh"
4+
publish = "book"
65

76
[context.deploy-preview]
8-
command = "mdbook build"
9-
10-
[context.branch-deploy]
11-
command = "mdbook build"
7+
command = "GO_VERSION=1.25.5 MDBOOK_VERSION=0.5.2 ./install-and-build-mdbook.sh"
128

139
[[redirects]]
1410
from = "/*"

0 commit comments

Comments
 (0)