.prepare_docs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. redoc_bundle_file=$(mktemp)
  29. git_ref_version=$(git describe --always)
  30. branch_docs_dir="${BRANCHES_DIR}/${git_ref_version}"
  31. # Build Redoc bundle (a single HTML file)
  32. redoc-cli bundle ${API_SPEC_FILE} -o ${redoc_bundle_file}
  33. # Check out current stuff from gh-pages (we'll append to it)
  34. git fetch origin 'refs/heads/gh-pages:refs/heads/gh-pages'
  35. git checkout gh-pages -- branches || prepare_docs_log "Failed to checkout branches from gh-pages, skipping..."
  36. if [ -e "./branches" ]; then
  37. mv "./branches" "${BRANCHES_DIR}"
  38. else
  39. mkdir -p "${BRANCHES_DIR}"
  40. fi
  41. if [ -e "${DOCS_DIR}/gh-pages" ]; then
  42. cp -r ${DOCS_DIR}/gh-pages/* "${DIST_DIR}"
  43. else
  44. prepare_docs_log "Skipping copy of gh-pages dir, doesn't exist"
  45. fi
  46. # Create the docs bundle for our ref. This will be the redoc bundle + a
  47. # snapshot of the OpenAPI spec
  48. mkdir -p "${branch_docs_dir}"
  49. cp "${API_SPEC_FILE}" "${branch_docs_dir}"
  50. cp "${redoc_bundle_file}" "${branch_docs_dir}/index.html"
  51. # Create a JSON file containing a list of all branches with docs (we'll
  52. # have an index page that renders the list).
  53. ls "${BRANCHES_DIR}" | jq -Rc '.' | jq -sc '.' > "${DIST_DIR}/branches.json"