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 "
0 commit comments