|
| 1 | +name: docs-build |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ main, release-* ] |
| 6 | + types: [ opened, synchronize ] |
| 7 | + |
| 8 | + push: |
| 9 | + branches: [ main ] |
| 10 | + tags: |
| 11 | + - v* |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +concurrency: |
| 15 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 16 | + cancel-in-progress: true |
| 17 | + |
| 18 | +defaults: |
| 19 | + run: |
| 20 | + shell: bash |
| 21 | + |
| 22 | +jobs: |
| 23 | + build-docs: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
| 28 | + - name: Build image |
| 29 | + run: | |
| 30 | + docker build --pull --tag docs-builder:latest --file docs/Dockerfile . |
| 31 | + - name: Build docs |
| 32 | + run: | |
| 33 | + docker run -v $(pwd):/work -w /work docs-builder:latest sphinx-build -b html -d /tmp docs docs/_build/output |
| 34 | + - name: Delete unnecessary files |
| 35 | + run: | |
| 36 | + sudo rm -rf docs/_build/jupyter_execute |
| 37 | + sudo rm -rf docs/_build/.buildinfo |
| 38 | + - name: Upload HTML |
| 39 | + uses: actions/upload-artifact@v4 |
| 40 | + with: |
| 41 | + name: html-build-artifact |
| 42 | + path: docs/_build/ |
| 43 | + if-no-files-found: error |
| 44 | + retention-days: 1 |
| 45 | + - name: Store PR information |
| 46 | + if: ${{ github.event_name == 'pull_request' }} |
| 47 | + run: | |
| 48 | + mkdir ./pr |
| 49 | + echo ${{ github.event.number }} > ./pr/pr.txt |
| 50 | + echo ${{ github.event.pull_request.merged }} > ./pr/merged.txt |
| 51 | + echo ${{ github.event.action }} > ./pr/action.txt |
| 52 | + - name: Upload PR information |
| 53 | + if: ${{ github.event_name == 'pull_request' }} |
| 54 | + uses: actions/upload-artifact@v4 |
| 55 | + with: |
| 56 | + name: pr |
| 57 | + path: pr/ |
| 58 | + |
| 59 | + store-html: |
| 60 | + needs: [ build-docs ] |
| 61 | + if: ${{ github.event_name == 'push' }} |
| 62 | + runs-on: ubuntu-latest |
| 63 | + steps: |
| 64 | + - uses: actions/checkout@v4 |
| 65 | + with: |
| 66 | + ref: "gh-pages" |
| 67 | + - name: Initialize Git configuration |
| 68 | + run: | |
| 69 | + git config user.name docs-build |
| 70 | + git config user.email [email protected] |
| 71 | + - name: Download artifacts |
| 72 | + uses: actions/download-artifact@v4 |
| 73 | + with: |
| 74 | + name: html-build-artifact |
| 75 | + - name: Copy HTML directories |
| 76 | + run: | |
| 77 | + ls -asl |
| 78 | + - name: Store bleeding edge docs from main |
| 79 | + if: ${{ github.ref == 'refs/heads/main' }} |
| 80 | + run: | |
| 81 | + mkdir main || true |
| 82 | + rsync -av --progress --delete output/ main/ |
| 83 | + git add main |
| 84 | + - name: Store docs for a release tag |
| 85 | + if: ${{ startsWith(github.ref, 'refs/tags/v') }} |
| 86 | + env: |
| 87 | + LATEST: ${{ contains(github.event.head_commit.message, '/not-latest') && 'not-true' || 'true' }} |
| 88 | + run: | |
| 89 | + printenv LATEST |
| 90 | + if [[ "${GITHUB_REF}" =~ "-rc" ]]; then |
| 91 | + echo "Not saving documents for release candidates." |
| 92 | + exit 0 |
| 93 | + fi |
| 94 | + if [[ "${GITHUB_REF}" =~ v([0-9]+\.[0-9]+\.[0-9]+) ]]; then |
| 95 | + TAG="${BASH_REMATCH[1]}" |
| 96 | + mkdir "${TAG}" || true |
| 97 | + rsync -av --progress --delete output/ "${TAG}/" |
| 98 | + git add "${TAG}/" |
| 99 | + if [[ "${LATEST}" == 'true' ]]; then |
| 100 | + mkdir latest || true |
| 101 | + rsync -av --progress --delete output/ latest/ |
| 102 | + cp output/versions.json . |
| 103 | + git add latest |
| 104 | + git add versions.json |
| 105 | + fi |
| 106 | + fi |
| 107 | + - name: Check or create dot-no-jekyll file |
| 108 | + run: | |
| 109 | + if [ -f ".nojekyll" ]; then |
| 110 | + echo "The dot-no-jekyll file already exists." |
| 111 | + exit 0 |
| 112 | + fi |
| 113 | + touch .nojekyll |
| 114 | + git add .nojekyll |
| 115 | + - name: Check or create redirect page |
| 116 | + env: |
| 117 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 118 | + run: | |
| 119 | + resp=$(grep 'http-equiv="refresh"' index.html 2>/dev/null) || true |
| 120 | + if [ -n "${resp}" ]; then |
| 121 | + echo "The redirect file already exists." |
| 122 | + exit 0 |
| 123 | + fi |
| 124 | + # If any of these commands fail, fail the build. |
| 125 | + html_url=$(gh api "repos/${GITHUB_REPOSITORY}/pages" --jq ".html_url") |
| 126 | + # Beware ugly quotation mark avoidance in the foll lines. |
| 127 | + echo '<!DOCTYPE html>' > index.html |
| 128 | + echo '<html>' >> index.html |
| 129 | + echo ' <head>' >> index.html |
| 130 | + echo ' <title>Redirect to documentation</title>' >> index.html |
| 131 | + echo ' <meta charset="utf-8">' >> index.html |
| 132 | + echo ' <meta http=equiv="refresh" content="3; URL='${html_url}'/latest/index.html">' >> index.html |
| 133 | + echo ' <link rel="canonical" href="'${html_url}'/latest/index.html">' >> index.html |
| 134 | + echo ' <script language="javascript">' >> index.html |
| 135 | + echo ' function redirect() {' >> index.html |
| 136 | + echo ' window.location.assign("'${html_url}'/latest/index.html")' >> index.html |
| 137 | + echo ' }' >> index.html |
| 138 | + echo ' </script>' >> index.html |
| 139 | + echo ' </head>' >> index.html |
| 140 | + echo ' <body onload="redirect()">' >> index.html |
| 141 | + echo ' <p>Please follow the link to the <a href="'${html_url}'/latest/index.html">' >> index.html |
| 142 | + echo 'latest</a> documentation.</p>' >> index.html |
| 143 | + echo ' </body>' >> index.html |
| 144 | + echo '</html>' >> index.html |
| 145 | + git add index.html |
| 146 | + - name: Commit changes to the GitHub Pages branch |
| 147 | + run: | |
| 148 | + git status |
| 149 | + if git commit -m 'Pushing changes to GitHub Pages.'; then |
| 150 | + git push -f |
| 151 | + else |
| 152 | + echo "Nothing changed." |
| 153 | + fi |
0 commit comments