.prepare_docs 2.2 KB

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