1+ #! /usr/bin/env bash
2+
3+ # This script sets up API documentation bundles for deployment to Github Pages.
4+ # It expects the following structure:
5+ #
6+ # In development branches:
7+ #
8+ # * ./docs/openapi.yaml - OpenAPI spec in
9+ # * ./docs/gh-pages - Any assets that should be copied to gh-pages root
10+ #
11+ # In Github Pages, it will generate:
12+ #
13+ # * ./ - Files from ./docs/gh-pages will be copied
14+ # * ./branches/<branch>/... - Deployment bundles including an index.html
15+ # and a snapshot of the Open API spec.
16+
17+ set -eo pipefail
18+
19+ prepare_docs_log () {
20+ echo " [prepare docs release] -- $@ "
21+ }
22+
23+ # Only run for tagged commits
24+ if [ -z " $( git tag -l --points-at HEAD) " ]; then
25+ prepare_docs_log " Skipping non-tagged commit."
26+ exit 0
27+ fi
28+
29+ DOCS_DIR=" ./docs"
30+ DIST_DIR=" ./dist/docs"
31+ BRANCHES_DIR=" ${DIST_DIR} /branches"
32+ API_SPEC_FILE=" ${DOCS_DIR} /openapi.yaml"
33+
34+ rm -rf " ${DIST_DIR} "
35+
36+ redoc_bundle_file=$( mktemp)
37+ git_ref_version=$( git describe --always)
38+ branch_docs_dir=" ${BRANCHES_DIR} /${git_ref_version} "
39+
40+ # Build Redoc bundle (a single HTML file)
41+ redoc-cli bundle ${API_SPEC_FILE} -o ${redoc_bundle_file} --title ' Milight Hub API Documentation'
42+
43+ # Check out current stuff from gh-pages (we'll append to it)
44+ git fetch origin ' refs/heads/gh-pages:refs/heads/gh-pages'
45+ git checkout gh-pages -- branches || prepare_docs_log " Failed to checkout branches from gh-pages, skipping..."
46+
47+ if [ -e " ./branches" ]; then
48+ mkdir -p " ${DIST_DIR} "
49+ mv " ./branches" " ${BRANCHES_DIR} "
50+ else
51+ mkdir -p " ${BRANCHES_DIR} "
52+ fi
53+
54+ if [ -e " ${DOCS_DIR} /gh-pages" ]; then
55+ cp -r ${DOCS_DIR} /gh-pages/* " ${DIST_DIR} "
56+ else
57+ prepare_docs_log " Skipping copy of gh-pages dir, doesn't exist"
58+ fi
59+
60+ # Create the docs bundle for our ref. This will be the redoc bundle + a
61+ # snapshot of the OpenAPI spec
62+ mkdir -p " ${branch_docs_dir} "
63+ cp " ${API_SPEC_FILE} " " ${branch_docs_dir} "
64+ cp " ${redoc_bundle_file} " " ${branch_docs_dir} /index.html"
65+
66+ # Update `latest` symlink to this branch
67+ rm -rf " ${BRANCHES_DIR} /latest"
68+ ln -s " ${git_ref_version} " " ${BRANCHES_DIR} /latest"
69+
70+ # Create a JSON file containing a list of all branches with docs (we'll
71+ # have an index page that renders the list).
72+ ls " ${BRANCHES_DIR} " | jq -Rc ' .' | jq -sc ' .' > " ${DIST_DIR} /branches.json"
0 commit comments