11#! /bin/bash
2- # This script runs in a loop (configurable with LOOP), checks for updates to the
3- # Hugo docs theme or to the docs on certain branches and rebuilds the public
4- # folder for them. It has be made more generalized, so that we don't have to
5- # hardcode versions.
62
7- # Warning - Changes should not be made on the server on which this script is running
8- # becauses this script does git checkout and merge.
3+ # Hugo Multi-Version Documentation Build Script
4+ # This script builds documentation from multiple branches
5+ # Usage: ./build.sh [baseURL]
6+ # Example: ./build.sh http://localhost:1313
7+ # ./build.sh https://docs.dgraph.io
98
109set -e
1110
12- # Important for clean builds on Netlify
13- if ! git remote | grep -q origin ; then
14- git remote add origin https://github.com/dgraph-io/dgraph-docs.git
15- git fetch --all
16- fi
17-
18- GREEN=' \033[32;1m'
19- RESET=' \033[0m'
20- HOST=" ${HOST:- https:// dgraph.io/ docs} "
21- # Name of output public directory
22- PUBLIC=" ${PUBLIC:- public} "
23- # LOOP true makes this script run in a loop to check for updates
24- LOOP=" ${LOOP:- true} "
25- # Binary of hugo command to run.
26- HUGO=" ${HUGO:- hugo} "
27- THEME_BRANCH=" ${THEME_BRANCH:- main} "
28-
29- # Place the latest version at the beginning so that version selector can
30- # append '(latest)' to the version string, followed by the main version,
31- # and then the older versions in descending order, such that the
32- # build script can place the artifact in an appropriate location.
33-
34- VERSIONS_ARRAY=(
35- ' main'
36- )
37-
38- joinVersions () {
39- versions=$( printf " ,%s" " ${VERSIONS_ARRAY[@]} " )
40- echo " ${versions: 1} "
41- }
42-
43- function version { echo " $@ " | gawk -F. ' { printf("%03d%03d%03d\n", $1,$2,$3); }' ; }
44-
45- rebuild () {
46- echo -e " $( date) $GREEN Updating docs for branch: $1 .$RESET "
47-
48- # The latest documentation is generated in the root of /public dir
49- # Older documentations are generated in their respective `/public/vx.x.x` dirs
50- dir=' '
51- if [[ $2 != " ${VERSIONS_ARRAY[0]} " ]]; then
52- dir=$2
53- fi
54-
55- VERSION_STRING=$( joinVersions)
56- # In Unix environments, env variables should also be exported to be seen by Hugo
57- export CURRENT_BRANCH=${1}
58- export CURRENT_VERSION=${2}
59- export CURRENT_LATEST_TAG=${3}
60- export VERSIONS=${VERSION_STRING}
61- export DGRAPH_ENDPOINT=${DGRAPH_ENDPOINT:- " https://play.dgraph.io/query?latency=true" }
62- export CANONICAL_PATH=" $HOST "
63-
64- HUGO_TITLE=" Dgraph Doc ${2} " \
65- CANONICAL_PATH=${HOST} \
66- VERSIONS=${VERSION_STRING} \
67- CURRENT_BRANCH=${1} \
68- CURRENT_LATEST_TAG=${3} \
69- CURRENT_VERSION=${2} ${HUGO} \
70- --destination=" ${PUBLIC} " /" $dir " \
71- --baseURL=" $HOST " /" $dir " 1> /dev/null
72- }
73-
74- branchUpdated ()
75- {
76- local branch=" $1 "
77- git checkout -q " $1 "
78- UPSTREAM=$( git rev-parse " @{u}" )
79- LOCAL=$( git rev-parse " @" )
11+ # Configuration
12+ OUTPUT_DIR=" public"
13+ TEMP_DIR=" .hugo-versions-temp"
14+ VERSION_BRANCHES=(" main" " release/v24.1" ) # Add your version branches here
15+ # CURRENT_BRANCH is the first one
16+ CURRENT_BRANCH=${VERSION_BRANCHES[0]}
17+
18+ # Parse baseURL parameter (default: http://localhost:1313)
19+ BASE_URL_PARAM=${1:- " http://localhost:1313" }
20+
21+ DGRAPH_ENDPOINT=${DGRAPH_ENDPOINT:- " https://play.dgraph.io/query?latency=true" }
22+ HUGO_TITLE=" Dgraph documentation"
23+ # Colors for output
24+ GREEN=' \033[0;32m'
25+ BLUE=' \033[0;34m'
26+ NC=' \033[0m' # No Color
27+
28+ echo -e " ${BLUE} Starting multi-version Hugo build...${NC} "
29+
30+ # Clean up previous builds
31+ rm -rf " $OUTPUT_DIR "
32+ mkdir -p " $OUTPUT_DIR "
33+
34+ # Get current branch
35+ CURRENT_BRANCH=$( git rev-parse --abbrev-ref HEAD)
36+
37+ # Build all versions in a loop
38+ for branch in " ${VERSION_BRANCHES[@]} " ; do
39+ # Determine version: if branch doesn't have version prefix, use the branch name
40+ if [[ " $branch " =~ ^release/ ]]; then
41+ VERSION=$( echo " $branch " | sed ' s|release/||' )
42+ else
43+ VERSION=$branch
44+ fi
45+
46+ echo -e " ${GREEN} Building version $VERSION from branch $branch ...${NC} "
47+
48+ # Determine output directory and baseURL: main branch goes to public, others to public/VERSION
49+ if [ " $branch " = " $CURRENT_BRANCH " ]; then
50+ BUILD_OUTPUT_DIR=" $OUTPUT_DIR "
51+ BASE_URL=" $BASE_URL_PARAM /"
52+ else
53+ BUILD_OUTPUT_DIR=" $OUTPUT_DIR /$VERSION "
54+ BASE_URL=" $BASE_URL_PARAM /$VERSION /"
55+ fi
56+
57+ # If building the current branch, use current directory; otherwise use worktree
58+ if [ " $branch " = " $CURRENT_BRANCH " ]; then
59+ echo " Building from current branch, using current directory..."
60+ hugo --minify -d " $BUILD_OUTPUT_DIR " --baseURL " $BASE_URL "
61+ else
62+ # Create temporary directory for this version
63+ BRANCH_DIR=" $TEMP_DIR /$VERSION "
64+
65+ # Remove worktree if it already exists
66+ if [ -d " $BRANCH_DIR " ]; then
67+ git worktree remove " $BRANCH_DIR " --force 2> /dev/null || rm -rf " $BRANCH_DIR "
68+ fi
69+
70+ # Clone the specific branch into temp directory
71+ git worktree add " $BRANCH_DIR " " $branch "
72+
73+ # Build Hugo site for this version
74+ pushd " $BRANCH_DIR "
75+ hugo --minify -d " ../../$BUILD_OUTPUT_DIR " --baseURL " $BASE_URL "
76+ popd
77+
78+ # Clean up worktree
79+ git worktree remove " $BRANCH_DIR "
80+ fi
81+
82+ echo -e " ${GREEN} Version $VERSION built successfully${NC} "
83+ done
8084
81- if [ " $LOCAL " != " $UPSTREAM " ] ; then
82- git merge -q origin/" $branch "
83- return 0
84- else
85- return 1
86- fi
87- }
85+ # Clean up temp directory
86+ rm -rf " $TEMP_DIR "
8887
89- publicFolder ()
88+ # Create versions.json for the version selector
89+ echo -e " ${BLUE} Creating versions.json...${NC} "
90+ cat > " $OUTPUT_DIR /versions.json" << EOF
9091{
91- dir=' '
92- if [[ $1 == " ${VERSIONS_ARRAY[0]} " ]]; then
93- echo " ${PUBLIC} "
94- else
95- echo " ${PUBLIC} /$1 "
96- fi
97- }
98-
99- checkAndUpdate ()
100- {
101- local version=" $1 "
102- local branch=" "
103- local tag=" $2 "
104-
105- if [[ $version == " main" ]]; then
106- branch=" main"
107- else
108- branch=" release/$version "
109- fi
92+ "versions": [
93+ EOF
94+
95+ # Add version entries
96+ FIRST=true
97+ for branch in " ${VERSION_BRANCHES[@]} " ; do
98+ # Determine version: if branch doesn't have version prefix, use "main"
99+ if [[ " $branch " =~ ^release/ ]]; then
100+ VERSION=$( echo " $branch " | sed ' s|release/||' )
101+ VERSION_TITLE=" $VERSION "
102+ VERSION_URL=" /$VERSION /"
103+ else
104+ VERSION=" main"
105+ VERSION_TITLE=" Latest (main)"
106+ VERSION_URL=" /"
107+ fi
108+
109+ if [ " $FIRST " = true ]; then
110+ FIRST=false
111+ cat >> " $OUTPUT_DIR /versions.json" << EOF
112+ {
113+ "version": "$VERSION ",
114+ "title": "$VERSION_TITLE ",
115+ "url": "$VERSION_URL "
116+ }
117+ EOF
118+ else
119+ cat >> " $OUTPUT_DIR /versions.json" << EOF
120+ ,
121+ {
122+ "version": "$VERSION ",
123+ "title": "$VERSION_TITLE ",
124+ "url": "$VERSION_URL "
125+ }
126+ EOF
127+ fi
128+ done
110129
111- if branchUpdated " $branch " ; then
112- git merge -q origin/" $branch "
113- rebuild " $branch " $version " $tag "
114- fi
130+ cat >> " $OUTPUT_DIR /versions.json" << EOF
115131
116- folder=$( publicFolder " $version " )
117- if [ " $firstRun " = 1 ] || [ " $themeUpdated " = 0 ] || [ ! -d " $folder " ] ; then
118- rebuild " $branch " $version " $tag "
119- fi
132+ ]
120133}
121-
122-
123- firstRun=1
124- while true ; do
125- # Lets move to the docs directory.
126- pushd " $( dirname " $0 " ) /.." > /dev/null
127-
128- currentBranch=$( git rev-parse --abbrev-ref HEAD)
129-
130- if [ " $firstRun " = 1 ];
131- then
132- # Theme is now included in the repository
133- echo -e " $( date) $GREEN Using theme from repository.$RESET "
134- fi
135-
136- # Theme is now included in the repository, no need to check for updates
137-
138- echo -e " $( date) Starting to check branches."
139- git remote update > /dev/null
140-
141- for version in " ${VERSIONS_ARRAY[@]} "
142- do
143- latest_version=$( curl -s https://get.dgraph.io/latest | grep -o ' "latest": *"[^"]*' | grep -o ' [^"]*$' | sed " /^v21.12/d" | sed " /^v20.03/d" | sed " /^v20.07/d" | grep " $version " | sort | uniq | head -n1)
144- SETO=" ${latest_version:- main} "
145- checkAndUpdate " $version " " $SETO "
146- echo " version => '$version '"
147- echo " latest_version => '$SETO '"
148- latest_version=' '
149- done
150-
151- echo -e " $( date) Done checking branches.\n"
152-
153- git checkout -q " $currentBranch "
154- popd > /dev/null
155-
156- firstRun=0
157- if ! $LOOP ; then
158- exit
159- fi
160- sleep 60
161- done
134+ EOF
135+
136+ echo -e " ${GREEN} Build complete! Documentation available in $OUTPUT_DIR /${NC} "
137+ echo -e " ${BLUE} Versions built:${NC} "
138+ for branch in " ${VERSION_BRANCHES[@]} " ; do
139+ # Determine version: if branch doesn't have version prefix, use "main"
140+ if [[ " $branch " =~ ^release/ ]]; then
141+ VERSION=$( echo " $branch " | sed ' s|release/||' )
142+ VERSION_URL=" /$VERSION /"
143+ else
144+ VERSION=" main"
145+ VERSION_URL=" /"
146+ fi
147+ echo " - $VERSION -> $VERSION_URL "
148+ done
0 commit comments