Skip to content

Commit bd62fd4

Browse files
committed
Added custom build script to dynamically set edit_uri at build time
1 parent 0fcbd2f commit bd62fd4

3 files changed

Lines changed: 66 additions & 4 deletions

File tree

.readthedocs/.readthedocs.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ build:
2121
os: ubuntu-24.04
2222
tools:
2323
python: "3.13"
24-
# jobs:
25-
# post_checkout: # MODIFY IF NEEDED: uncomment to cancel the RTD build
26-
# - exit 183
24+
jobs:
25+
# post_checkout: ['exit 183'] # MODIFY IF NEEDED: uncomment to cancel the RTD build
26+
build:
27+
html:
28+
- curl -sSL https://raw.githubusercontent.com/ACCESS-NRI/ACCESS-Hive-Docs/davide/rtd_custom_build/.readthedocs/scripts/custom_build.sh | bash # Custom build
29+
2730

2831
# Build documentation with Mkdocs
2932
mkdocs:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This is a custom build script for mkdocs that checks for the branch name and exports
2+
# a EDIT_URI env variable to be used in the mkdocs build to set edit_uri to the proper branch,
3+
# especially for PR preview builds.
4+
# If the build comes from a forked repo or from a tag, the EDIT_URI variable will not be set.
5+
# This is needed to avoid the link-check workflow to fail for new pages if the edit_uri points to
6+
# a non-existing file in the main branch.
7+
8+
set -e
9+
10+
if [ "$READTHEDOCS_VERSION_TYPE" == external ]; then # PR preview build
11+
# Download jq
12+
export JQ_EXE=$(pwd)/jq
13+
wget -q https://github.com/stedolan/jq/releases/latest/download/jq-linux64 -O "$JQ_EXE"
14+
chmod +x "$JQ_EXE"
15+
"$JQ_EXE" --version
16+
# Get full repo
17+
full_repo=$(sed -E 's|.*github\.com[:/](.+)$|\1|' <<< "$READTHEDOCS_GIT_CLONE_URL")
18+
# Remove .git suffix if present
19+
full_repo=${full_repo%.git}
20+
# Get repo and owner
21+
IFS='/' read owner repo <<< "$full_repo"
22+
# Check if the PR comes from a forked repo
23+
gh_api_url="https://api.github.com/repos/$owner/$repo"
24+
pr_info=$(curl -s "$gh_api_url/pulls/$READTHEDOCS_GIT_IDENTIFIER")
25+
pr_head_owner=$("$JQ_EXE" -r '.head.user.login' <<< "$pr_info")
26+
if [ "$owner" == "$pr_head_owner" ]; then # Not a fork
27+
# Get the branch name
28+
branch_name=$("$JQ_EXE" -r '.head.ref' <<< "$pr_info")
29+
fi
30+
elif git show-ref --heads "$READTHEDOCS_GIT_IDENTIFIER" --quiet; then # Branch build
31+
branch_name="$READTHEDOCS_GIT_IDENTIFIER"
32+
fi
33+
34+
# Download yq
35+
export YQ_EXE=$(pwd)/yq
36+
wget -q https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O "$YQ_EXE"
37+
chmod +x "$YQ_EXE"
38+
"$YQ_EXE" --version
39+
40+
# Get RTD config path
41+
rtd_config_path=$(find "$READTHEDOCS_REPOSITORY_PATH" -type f \( -name ".readthedocs.yaml" -o -name ".readthedocs.yml" \))
42+
# Get mkdocs config path
43+
mkdocs_path=$("$YQ_EXE" '.mkdocs.configuration' "$rtd_config_path")
44+
if [ -n "$branch_name" ]; then
45+
# Get docs_dir from mkdocs config or set to default 'docs'
46+
docs_dir=$("$YQ_EXE" '.docs_dir // "docs"' "$mkdocs_path")
47+
EDIT_URI="edit/$branch_name/$docs_dir"
48+
echo "This is a branch build. EDIT_URI set to to '$EDIT_URI'"
49+
export EDIT_URI
50+
fi
51+
52+
# Default RTD mkdocs build commands
53+
echo ======= START OF mkdocs.yml =======
54+
cat "$mkdocs_path"
55+
echo ======= ENF OF mkdocs.yml =======
56+
cmd="python -m mkdocs build --clean --site-dir $READTHEDOCS_OUTPUT/html --config-file $mkdocs_path"
57+
eval "$cmd"

mkdocs.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ site_url: !ENV READTHEDOCS_CANONICAL_URL
88
repo_url: https://github.com/ACCESS-NRI/ACCESS-Hive-Docs
99
repo_name: ACCESS-Hive-Docs
1010

11-
edit_uri: edit/development/docs/
11+
edit_uri: !ENV [EDIT_URI, ""]
12+
# EDIT_URI is set within the website build (ReadTheDocs) through the ACCESS-Hive-Docs repo script
13+
# https://github.com/ACCESS-NRI/ACCESS-Hive-Docs/blob/main/.readthedocs/scripts/custom_build.sh
1214

1315
theme:
1416
name: material

0 commit comments

Comments
 (0)