github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/wiki/scripts/build.sh (about) 1 #!/bin/bash 2 # This script runs in a loop, checks for updates to the Hugo docs theme or 3 # to the docs on certain branches and rebuilds the public folder for them. 4 # It has be made more generalized, so that we don't have to hardcode versions. 5 6 # Warning - Changes should not be made on the server on which this script is running 7 # becauses this script does git checkout and merge. 8 9 set -e 10 11 GREEN='\033[32;1m' 12 RESET='\033[0m' 13 HOST="${HOST:-https://dgraph.io/docs}" 14 # Name of output public directory 15 PUBLIC="${PUBLIC:-public}" 16 # LOOP true makes this script run in a loop to check for updates 17 LOOP="${LOOP:-true}" 18 # Binary of hugo command to run. 19 HUGO="${HUGO:-hugo}" 20 21 # TODO - Maybe get list of released versions from Github API and filter 22 # those which have docs. 23 24 # Place the latest version at the beginning so that version selector can 25 # append '(latest)' to the version string, followed by the master version, 26 # and then the older versions in descending order, such that the 27 # build script can place the artifact in an appropriate location. 28 VERSIONS_ARRAY=( 29 'v1.1.0' 30 'master' 31 'v1.0.17' 32 'v1.0.16' 33 'v1.0.15' 34 'v1.0.14' 35 'v1.0.13' 36 'v1.0.12' 37 'v1.0.11' 38 'v1.0.10' 39 'v1.0.9' 40 'v1.0.8' 41 'v1.0.7' 42 'v1.0.6' 43 'v1.0.5' 44 'v1.0.4' 45 'v1.0.3' 46 'v1.0.2' 47 'v1.0.1' 48 'v1.0.0' 49 'v0.9.4' 50 'v0.9.3' 51 'v0.9.2' 52 'v0.9.1' 53 'v0.9.0' 54 'v0.8.3' 55 'v0.8.2' 56 'v0.8.1' 57 'v0.8.0' 58 ) 59 60 joinVersions() { 61 versions=$(printf ",%s" "${VERSIONS_ARRAY[@]}") 62 echo "${versions:1}" 63 } 64 65 function version { echo "$@" | gawk -F. '{ printf("%03d%03d%03d\n", $1,$2,$3); }'; } 66 67 rebuild() { 68 echo -e "$(date) $GREEN Updating docs for branch: $1.$RESET" 69 70 # The latest documentation is generated in the root of /public dir 71 # Older documentations are generated in their respective `/public/vx.x.x` dirs 72 dir='' 73 if [[ $2 != "${VERSIONS_ARRAY[0]}" ]]; then 74 dir=$2 75 fi 76 77 VERSION_STRING=$(joinVersions) 78 # In Unix environments, env variables should also be exported to be seen by Hugo 79 export CURRENT_BRANCH=${1} 80 export CURRENT_VERSION=${2} 81 export VERSIONS=${VERSION_STRING} 82 export DGRAPH_ENDPOINT=${DGRAPH_ENDPOINT:-"https://play.dgraph.io/query?latency=true"} 83 84 cmd=hugo_0.19 85 # Hugo broke backward compatibility, so files for version > 1.0.5 can use newer hugo (v0.38 onwards) but files in 86 # older versions have to use hugo v0.19 87 # If branch is master or version is >= 1.0.5 then use newer hugo 88 if [ "$CURRENT_VERSION" = "master" ] || [ "$(version "${CURRENT_VERSION:1}")" -ge "$(version "1.0.5")" ]; then 89 cmd=hugo 90 fi 91 92 HUGO_TITLE="Dgraph Doc ${2}"\ 93 VERSIONS=${VERSION_STRING}\ 94 CURRENT_BRANCH=${1}\ 95 CURRENT_VERSION=${2} $cmd\ 96 --destination=public/"$dir"\ 97 --baseURL="$HOST"/"$dir" 1> /dev/null 98 } 99 100 branchUpdated() 101 { 102 local branch="$1" 103 git checkout -q "$1" 104 UPSTREAM=$(git rev-parse "@{u}") 105 LOCAL=$(git rev-parse "@") 106 107 if [ "$LOCAL" != "$UPSTREAM" ] ; then 108 git merge -q origin/"$branch" 109 return 0 110 else 111 return 1 112 fi 113 } 114 115 publicFolder() 116 { 117 dir='' 118 if [[ $1 == "${VERSIONS_ARRAY[0]}" ]]; then 119 echo "public" 120 else 121 echo "public/$1" 122 fi 123 } 124 125 checkAndUpdate() 126 { 127 local version="$1" 128 local branch="" 129 130 if [[ $version == "master" ]]; then 131 branch="master" 132 else 133 branch="release/$version" 134 fi 135 136 if branchUpdated "$branch" ; then 137 git merge -q origin/"$branch" 138 rebuild "$branch" "$version" 139 fi 140 141 folder=$(publicFolder "$version") 142 if [ "$firstRun" = 1 ] || [ "$themeUpdated" = 0 ] || [ ! -d "$folder" ] ; then 143 rebuild "$branch" "$version" 144 fi 145 } 146 147 148 firstRun=1 149 while true; do 150 # Lets move to the docs directory. 151 pushd "$(dirname "$0")/.." > /dev/null 152 153 currentBranch=$(git rev-parse --abbrev-ref HEAD) 154 155 # Lets check if the theme was updated. 156 pushd themes/hugo-docs > /dev/null 157 git remote update > /dev/null 158 themeUpdated=1 159 if branchUpdated "master" ; then 160 echo -e "$(date) $GREEN Theme has been updated. Now will update the docs.$RESET" 161 themeUpdated=0 162 fi 163 popd > /dev/null 164 165 # Now lets check the theme. 166 echo -e "$(date) Starting to check branches." 167 git remote update > /dev/null 168 169 for version in "${VERSIONS_ARRAY[@]}" 170 do 171 checkAndUpdate "$version" 172 done 173 174 echo -e "$(date) Done checking branches.\n" 175 176 git checkout -q "$currentBranch" 177 popd > /dev/null 178 179 firstRun=0 180 sleep 60 181 done