|
| 1 | +name: docs-build |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [develop] |
| 6 | + tags: |
| 7 | + - docs-v* |
| 8 | + - v* |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +env: |
| 12 | + REGISTRY: ghcr.io |
| 13 | + IMAGE_NAME: ${{ github.repository }}-docs |
| 14 | + TAG: 0.1.0 |
| 15 | + GH_TOKEN: ${{ github.token }} |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + dockerfile-changed: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + permissions: |
| 25 | + contents: read |
| 26 | + packages: read |
| 27 | + outputs: |
| 28 | + changed: ${{ steps.change.outputs.changed }} |
| 29 | + image: ${{ steps.change.outputs.image }} |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v4 |
| 33 | + - name: Log in to Container Registry |
| 34 | + uses: docker/login-action@v3 |
| 35 | + with: |
| 36 | + registry: ${{ env.REGISTRY }} |
| 37 | + username: ${{ github.actor }} |
| 38 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + - name: Detect change |
| 40 | + id: change |
| 41 | + shell: bash |
| 42 | + run: | |
| 43 | + export COMMIT_SHORT_SHA="${GITHUB_SHA:0:8}" |
| 44 | + if ! docker manifest inspect "${REGISTRY}/${IMAGE_NAME,,}:${TAG}" 2>&1 > /dev/null ; then |
| 45 | + echo "image not found...${REGISTRY}/${IMAGE_NAME,,}:${TAG}" |
| 46 | + export NEEDS_IMAGE=true |
| 47 | + fi |
| 48 | + def_branch=$(gh api "repos/${GITHUB_REPOSITORY}" -q '.default_branch') |
| 49 | + git fetch origin "${def_branch}" |
| 50 | + files=$(git diff --name-only "${GITHUB_SHA}" FETCH_HEAD | tr '\n' ' ') |
| 51 | + echo "${files}" |
| 52 | + if echo "${files}" | grep -q "docs/poetry.lock/\|docs/Dockerfile"; then export NEEDS_IMAGE=true ; fi |
| 53 | + if [[ "${NEEDS_IMAGE}" ]]; then |
| 54 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 55 | + if [[ "${{ github.event_name }}" == 'pull_request' ]]; then |
| 56 | + echo "image=${REGISTRY}/${IMAGE_NAME,,}:${COMMIT_SHORT_SHA}" >> "$GITHUB_OUTPUT" |
| 57 | + else |
| 58 | + echo "image=${REGISTRY}/${IMAGE_NAME,,}:${TAG}" >> "$GITHUB_OUTPUT" |
| 59 | + fi |
| 60 | + else |
| 61 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 62 | + echo "image=${REGISTRY}/${IMAGE_NAME,,}:${TAG}" >> "$GITHUB_OUTPUT" |
| 63 | + fi |
| 64 | +
|
| 65 | + build-and-push-image: |
| 66 | + needs: dockerfile-changed |
| 67 | + if: needs.dockerfile-changed.outputs.changed == 'true' |
| 68 | + runs-on: ubuntu-latest |
| 69 | + permissions: |
| 70 | + contents: read |
| 71 | + packages: write |
| 72 | + steps: |
| 73 | + - name: Checkout |
| 74 | + uses: actions/checkout@v4 |
| 75 | + - name: Set up Docker Buildx |
| 76 | + uses: docker/setup-buildx-action@v3 |
| 77 | + - name: Log in to Container Registry |
| 78 | + uses: docker/login-action@v3 |
| 79 | + with: |
| 80 | + registry: ${{ env.REGISTRY }} |
| 81 | + username: ${{ github.actor }} |
| 82 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + - name: Build and push |
| 84 | + uses: docker/build-push-action@v6 |
| 85 | + with: |
| 86 | + context: docs |
| 87 | + file: docker/Dockerfile |
| 88 | + push: true |
| 89 | + tags: ${{ needs.dockerfile-changed.outputs.image }} |
| 90 | + |
| 91 | + build-docs: |
| 92 | + needs: [dockerfile-changed, build-and-push-image] |
| 93 | + container: |
| 94 | + image: ${{ needs.dockerfile-changed.outputs.image }} |
| 95 | + if: ${{ always() }} |
| 96 | + runs-on: ubuntu-latest |
| 97 | + steps: |
| 98 | + - name: Checkout |
| 99 | + uses: actions/checkout@v4 |
| 100 | + - name: Build docs |
| 101 | + run: | |
| 102 | + sphinx-build -b html docs _build/docs |
| 103 | + - name: Delete unnecessary files |
| 104 | + run: | |
| 105 | + find _build -name .doctrees -prune -exec rm -rf {} \; |
| 106 | + find _build -name .buildinfo -exec rm {} \; |
| 107 | + - name: Upload HTML |
| 108 | + uses: actions/upload-artifact@v4 |
| 109 | + with: |
| 110 | + name: html-build-artifact |
| 111 | + path: _build/docs |
| 112 | + if-no-files-found: error |
| 113 | + retention-days: 1 |
| 114 | + - name: Store PR information |
| 115 | + if: github.event_name == 'pull_request' |
| 116 | + run: | |
| 117 | + mkdir ./pr |
| 118 | + echo ${{ github.event.number }} > ./pr/pr.txt |
| 119 | + echo ${{ github.event.pull_request.merged }} > ./pr/merged.txt |
| 120 | + echo ${{ github.event.action }} > ./pr/action.txt |
| 121 | + - name: Upload PR information |
| 122 | + if: ${{ github.event_name == 'pull_request' }} |
| 123 | + uses: actions/upload-artifact@v4 |
| 124 | + with: |
| 125 | + name: pr |
| 126 | + path: pr/ |
| 127 | + |
| 128 | + store-html: |
| 129 | + needs: [build-docs] |
| 130 | + runs-on: ubuntu-latest |
| 131 | + steps: |
| 132 | + - uses: actions/checkout@v4 |
| 133 | + with: |
| 134 | + ref: "gh-pages" |
| 135 | + - name: Initialize Git configuration |
| 136 | + run: | |
| 137 | + git config user.name docs-build |
| 138 | + git config user.email [email protected] |
| 139 | + - name: Download artifacts |
| 140 | + uses: actions/download-artifact@v4 |
| 141 | + with: |
| 142 | + name: html-build-artifact |
| 143 | + - name: Copy HTML directories |
| 144 | + run: | |
| 145 | + ls -asl |
| 146 | + for i in `ls -d *` |
| 147 | + do |
| 148 | + echo "Git adding ${i}" |
| 149 | + git add "${i}" |
| 150 | + done |
| 151 | + - name: Check or create dot-no-jekyll file |
| 152 | + run: | |
| 153 | + if [ -f ".nojekyll" ]; then |
| 154 | + echo "The dot-no-jekyll file already exists." |
| 155 | + exit 0 |
| 156 | + fi |
| 157 | + touch .nojekyll |
| 158 | + git add .nojekyll |
| 159 | + - name: Check or create redirect page |
| 160 | + env: |
| 161 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 162 | + run: | |
| 163 | + resp=$(grep 'http-equiv="refresh"' index.html 2>/dev/null) || true |
| 164 | + if [ -n "${resp}" ]; then |
| 165 | + echo "The redirect file already exists." |
| 166 | + exit 0 |
| 167 | + fi |
| 168 | + # If any of these commands fail, fail the build. |
| 169 | + def_branch=$(gh api "repos/${GITHUB_REPOSITORY}" --jq ".default_branch") |
| 170 | + html_url=$(gh api "repos/${GITHUB_REPOSITORY}/pages" --jq ".html_url") |
| 171 | + # Beware ugly quotation mark avoidance in the foll lines. |
| 172 | + echo '<!DOCTYPE html>' > index.html |
| 173 | + echo '<html>' >> index.html |
| 174 | + echo ' <head>' >> index.html |
| 175 | + echo ' <title>Redirect to documentation</title>' >> index.html |
| 176 | + echo ' <meta charset="utf-8">' >> index.html |
| 177 | + echo ' <meta http=equiv="refresh" content="3; URL='${html_url}${def_branch}'/index.html">' >> index.html |
| 178 | + echo ' <link rel="canonical" href="'${html_url}${def_branch}'/index.html">' >> index.html |
| 179 | + echo ' <script language="javascript">' >> index.html |
| 180 | + echo ' function redirect() {' >> index.html |
| 181 | + echo ' window.location.assign("'${html_url}${def_branch}'/index.html")' >> index.html |
| 182 | + echo ' }' >> index.html |
| 183 | + echo ' </script>' >> index.html |
| 184 | + echo ' </head>' >> index.html |
| 185 | + echo ' <body onload="redirect()">' >> index.html |
| 186 | + echo ' <p>Please follow the link to the <a href="'${html_url}${def_branch}'/index.html">' >> index.html |
| 187 | + echo ${def_branch}'</a> branch documentation.</p>' >> index.html |
| 188 | + echo ' </body>' >> index.html |
| 189 | + echo '</html>' >> index.html |
| 190 | + git add index.html |
| 191 | + - name: Commit changes to the GitHub Pages branch |
| 192 | + run: | |
| 193 | + git status |
| 194 | + if git commit -m 'Pushing changes to GitHub Pages.'; then |
| 195 | + git push -f |
| 196 | + else |
| 197 | + echo "Nothing changed." |
| 198 | + fi |
0 commit comments