|
| 1 | +#!/bin/bash -e |
| 2 | +set -o pipefail |
| 3 | + |
| 4 | +this=`basename $0` |
| 5 | + |
| 6 | +usage () { |
| 7 | +cat << EOF |
| 8 | +Usage: $this [-h] [-a] [SITE_SUBDIR] |
| 9 | +
|
| 10 | +Options: |
| 11 | + -h show this help and exit |
| 12 | + -a amend (with --reset-author) instead of creating a new commit |
| 13 | + -p REMOTE do git push to remote repo |
| 14 | +EOF |
| 15 | +} |
| 16 | + |
| 17 | +# Helper function for detecting available versions from the current directory |
| 18 | +create_versions_js() { |
| 19 | + local _baseurl="/node-feature-discovery" |
| 20 | + |
| 21 | + echo -e "function getVersionListItems() {\n return [" |
| 22 | + # 'stable' is a symlink pointing to the latest version |
| 23 | + [ -f stable ] && echo " { name: 'stable', url: '$_baseurl/stable' }," |
| 24 | + for f in `ls -d */ | tr -d /` ; do |
| 25 | + echo " { name: '$f', url: '$_baseurl/$f' }," |
| 26 | + done |
| 27 | + echo -e " ];\n}" |
| 28 | +} |
| 29 | + |
| 30 | +# |
| 31 | +# Argument parsing |
| 32 | +# |
| 33 | +while getopts "hap:" opt; do |
| 34 | + case $opt in |
| 35 | + h) usage |
| 36 | + exit 0 |
| 37 | + ;; |
| 38 | + a) amend="--amend --reset-author" |
| 39 | + ;; |
| 40 | + p) push_remote="$OPTARG" |
| 41 | + ;; |
| 42 | + *) usage |
| 43 | + exit 1 |
| 44 | + ;; |
| 45 | + esac |
| 46 | +done |
| 47 | +shift "$((OPTIND - 1))" |
| 48 | + |
| 49 | +site_subdir="$1" |
| 50 | + |
| 51 | +# Check that no extra args were provided |
| 52 | +if [ $# -gt 1 ]; then |
| 53 | + echo "ERROR: extra positional arguments: $@" |
| 54 | + usage |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +# |
| 59 | +# Build the documentation |
| 60 | +# |
| 61 | +build_dir="docs/_site" |
| 62 | +echo "Creating new Git worktree at $build_dir" |
| 63 | +git worktree add "$build_dir" gh-pages |
| 64 | + |
| 65 | +# Drop worktree on exit |
| 66 | +trap "echo 'Removing Git worktree $build_dir'; git worktree remove '$build_dir'" EXIT |
| 67 | + |
| 68 | +# Parse subdir name from GITHUB_REF |
| 69 | +if [ -z "$site_subdir" ]; then |
| 70 | + case "$GITHUB_REF" in |
| 71 | + refs/tags/*) |
| 72 | + _base_ref=${GITHUB_REF#refs/tags/} |
| 73 | + ;; |
| 74 | + refs/heads/*) |
| 75 | + _base_ref=${GITHUB_REF#refs/heads/} |
| 76 | + ;; |
| 77 | + *) _base_ref= |
| 78 | + esac |
| 79 | + echo "Parsed baseref: '$_base_ref'" |
| 80 | + |
| 81 | + case "$GITHUB_REF" in |
| 82 | + refs/tags/v*) |
| 83 | + _version=${GITHUB_REF#refs/tags/v} |
| 84 | + ;; |
| 85 | + refs/heads/release-*) |
| 86 | + _version=${GITHUB_REF#refs/heads/release-} |
| 87 | + ;; |
| 88 | + *) _version= |
| 89 | + esac |
| 90 | + echo "Detected version: '$_version'" |
| 91 | + |
| 92 | + _version=`echo -n $_version | sed -nE s'!^([0-9]+\.[0-9]+).*$!\1!p'` |
| 93 | + |
| 94 | + # User version as the subdir |
| 95 | + site_subdir=${_version:+v$_version} |
| 96 | + # Fallback to base-ref i.e. name of the branch or tag |
| 97 | + site_subdir=${site_subdir:-$_base_ref} |
| 98 | +fi |
| 99 | + |
| 100 | +# Default to 'master' if no subdir was given and we couldn't parse |
| 101 | +# it |
| 102 | +site_subdir=${site_subdir:-master} |
| 103 | + |
| 104 | +# Check if this ref is for a released version |
| 105 | +if [ "$site_subdir" != "master" ]; then |
| 106 | + _base_tag=`git describe --abbrev=0 || :` |
| 107 | + case "$_base_tag" in |
| 108 | + $site_subdir*) |
| 109 | + ;; |
| 110 | + *) |
| 111 | + echo "Not a released version. Parsed release branch is $site_subdir but based on tag $_base_tag. Stopping here." |
| 112 | + echo "SHA `git describe` (`git rev-parse HEAD`)" |
| 113 | + exit 0 |
| 114 | + ;; |
| 115 | + esac |
| 116 | +fi |
| 117 | + |
| 118 | +echo "Updating site subdir: '$site_subdir'" |
| 119 | + |
| 120 | +export SITE_DESTDIR="_site/$site_subdir" |
| 121 | +export SITE_BASEURL="/node-feature-discovery/$site_subdir" |
| 122 | +export JEKYLL_ENV=production |
| 123 | +make site-build |
| 124 | + |
| 125 | +# |
| 126 | +# Update gh-pages branch |
| 127 | +# |
| 128 | +if [ -n "$_GIT_TAG" ]; then |
| 129 | + commit_hash=${GIT_TAG:10} |
| 130 | +else |
| 131 | + commit_hash=`git describe --dirty --always` |
| 132 | +fi |
| 133 | + |
| 134 | +# Switch to work in the gh-pages worktree |
| 135 | +cd "$build_dir" |
| 136 | + |
| 137 | +_stable=`(ls -d1 v*/ || :) | sort -n | tail -n1` |
| 138 | +[ -n "$_stable" ] && ln -sfT "$_stable" stable |
| 139 | + |
| 140 | +# Detect existing versions from the gh-pages branch |
| 141 | +create_versions_js > versions.js |
| 142 | + |
| 143 | +cat > index.html << EOF |
| 144 | +<meta http-equiv="refresh" content="0; URL='stable/'"/> |
| 145 | +EOF |
| 146 | + |
| 147 | +if [ -z "`git status --short`" ]; then |
| 148 | + echo "No new content, gh-pages branch already up-to-date" |
| 149 | + exit 0 |
| 150 | +fi |
| 151 | + |
| 152 | +# Create a new commit |
| 153 | +commit_msg=`echo -e "Update documentation for $site_subdir\n\nAuto-generated from $commit_hash by '$this'"` |
| 154 | + |
| 155 | +echo "Committing changes..." |
| 156 | +git add . |
| 157 | +git commit $amend -m "$commit_msg" |
| 158 | + |
| 159 | +cd - |
| 160 | + |
| 161 | +echo "gh-pages branch successfully updated" |
| 162 | + |
| 163 | +if [ -n "$push_remote" ]; then |
| 164 | + echo "Pushing gh-pages to $push_remote" |
| 165 | + git push ${amend+-f} "$push_remote" gh-pages |
| 166 | +fi |
0 commit comments