|
1 |
| -# From: https://github.com/rkdarst/sphinx-actions-test/blob/master/.github/workflows/sphinx-build.yml |
| 1 | +# Deploy Sphinx. This could be shorter, but we also do some extra |
| 2 | +# stuff. |
| 3 | +# |
| 4 | +# License: CC-0. This is the canonical location of this file, which |
| 5 | +# you may want to link to anyway: |
| 6 | +# https://github.com/coderefinery/sphinx-lesson-template/blob/main/.github/workflows/sphinx.yml |
| 7 | +# https://raw.githubusercontent.com/coderefinery/sphinx-lesson-template/main/.github/workflows/sphinx.yml |
| 8 | + |
2 | 9 |
|
3 | 10 | name: sphinx
|
4 |
| -on: [push, pull_request, workflow_dispatch] |
| 11 | +on: [push, pull_request] |
5 | 12 |
|
6 |
| -# If these SPHINXOPTS are enabled, then be strict about the builds and |
7 |
| -# fail on any warnings |
8 |
| -#env: |
9 |
| -# SPHINXOPTS: "-W --keep-going -T" |
| 13 | +env: |
| 14 | + DEFAULT_BRANCH: "main" |
| 15 | + # If these SPHINXOPTS are enabled, then be strict about the |
| 16 | + # builds and fail on any warnings. |
| 17 | + #SPHINXOPTS: "-W --keep-going -T" |
| 18 | + GENERATE_PDF: true # to enable, must be 'true' lowercase |
| 19 | + GENERATE_SINGLEHTML: true # to enable, must be 'true' lowercase |
| 20 | + PDF_FILENAME: lesson.pdf |
| 21 | + MULTIBRANCH: true # to enable, must be 'true' lowercase |
10 | 22 |
|
11 | 23 |
|
12 | 24 | jobs:
|
13 |
| - build-and-deploy: |
14 |
| - name: Build and gh-pages |
| 25 | + build: |
| 26 | + name: Build |
15 | 27 | runs-on: ubuntu-latest
|
| 28 | + permissions: |
| 29 | + contents: read |
| 30 | + |
16 | 31 | steps:
|
17 | 32 | # https://github.com/marketplace/actions/checkout
|
18 |
| - - uses: actions/checkout@v2 |
| 33 | + - uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + fetch-depth: 0 |
| 36 | + lfs: true |
| 37 | + |
19 | 38 | # https://github.com/marketplace/actions/setup-python
|
20 | 39 | # ^-- This gives info on matrix testing.
|
21 | 40 | - name: Install Python
|
22 |
| - uses: actions/setup-python@v1 |
| 41 | + uses: actions/setup-python@v4 |
23 | 42 | with:
|
24 |
| - python-version: 3.8 |
25 |
| - - name: Fetch all refs |
26 |
| - run: | |
27 |
| - git fetch |
28 |
| - # I don't know where the "run" thing is documented. |
| 43 | + python-version: '3.11' |
| 44 | + cache: 'pip' |
| 45 | + |
| 46 | + # https://docs.github.com/en/actions/guides/building-and-testing-python#installing-dependencies |
| 47 | + # ^-- This gives info on installing dependencies with pip |
29 | 48 | - name: Install dependencies
|
30 | 49 | run: |
|
| 50 | + python -m pip install --upgrade pip |
31 | 51 | pip install -r requirements.txt
|
| 52 | +
|
| 53 | + # Debug |
32 | 54 | - name: Debugging information
|
| 55 | + env: |
| 56 | + ref: ${{github.ref}} |
| 57 | + event_name: ${{github.event_name}} |
| 58 | + head_ref: ${{github.head_ref}} |
| 59 | + base_ref: ${{github.base_ref}} |
33 | 60 | run: |
|
34 |
| - echo "github.ref:" ${{github.ref}} |
35 |
| - echo "github.event_name:" ${{github.event_name}} |
36 |
| - echo "github.head_ref:" ${{github.head_ref}} |
37 |
| - echo "github.base_ref:" ${{github.base_ref}} |
| 61 | + echo "github.ref: ${ref}" |
| 62 | + echo "github.event_name: ${event_name}" |
| 63 | + echo "github.head_ref: ${head_ref}" |
| 64 | + echo "github.base_ref: ${base_ref}" |
| 65 | + echo "GENERATE_PDF: ${GENERATE_PDF}" |
| 66 | + echo "GENERATE_SINGLEHTML: ${GENERATE_SINGLEHTML}" |
| 67 | + set -x |
38 | 68 | git rev-parse --abbrev-ref HEAD
|
39 | 69 | git branch
|
40 | 70 | git branch -a
|
41 | 71 | git remote -v
|
| 72 | + python -V |
| 73 | + pip list --not-required |
| 74 | + pip list |
| 75 | +
|
42 | 76 |
|
43 | 77 | # Build
|
44 | 78 | - uses: ammaraskar/sphinx-problem-matcher@master
|
45 |
| - - name: Build Sphinx docs |
| 79 | + - name: Build Sphinx docs (dirhtml) |
| 80 | + # SPHINXOPTS used via environment variables |
46 | 81 | run: |
|
47 | 82 | make dirhtml
|
| 83 | + # This fixes broken copy button icons, as explained in |
| 84 | + # https://github.com/coderefinery/sphinx-lesson/issues/50 |
| 85 | + # https://github.com/executablebooks/sphinx-copybutton/issues/110 |
| 86 | + # This can be removed once these PRs are accepted (but the |
| 87 | + # fixes also need to propagate to other themes): |
| 88 | + # https://github.com/sphinx-doc/sphinx/pull/8524 |
| 89 | + # https://github.com/readthedocs/sphinx_rtd_theme/pull/1025 |
| 90 | + sed -i 's/url_root="#"/url_root=""/' _build/dirhtml/index.html || true |
48 | 91 |
|
| 92 | + # singlehtml |
| 93 | + - name: Generate singlehtml |
| 94 | + if: ${{ env.GENERATE_SINGLEHTML == 'true' }} |
| 95 | + run: | |
| 96 | + make singlehtml |
| 97 | + mv _build/singlehtml/ _build/dirhtml/singlehtml/ |
49 | 98 |
|
50 |
| - # The following supports building all branches and combining on |
51 |
| - # gh-pages |
| 99 | + # PDF if requested |
| 100 | + - name: Generate PDF |
| 101 | + if: ${{ env.GENERATE_PDF == 'true' }} |
| 102 | + run: | |
| 103 | + pip install https://github.com/rkdarst/sphinx_pyppeteer_builder/archive/refs/heads/main.zip |
| 104 | + make pyppeteer |
| 105 | + mv _build/pyppeteer/*.pdf _build/dirhtml/${PDF_FILENAME} |
52 | 106 |
|
53 |
| - # Clone and set up the old gh-pages branch |
54 |
| - - name: Clone old gh-pages |
| 107 | + # Stage all deployed assets in _gh-pages/ for simplicity, and to |
| 108 | + # prepare to do a multi-branch deployment. |
| 109 | + - name: Copy deployment data to _gh-pages/ |
55 | 110 | if: ${{ github.event_name == 'push' }}
|
56 |
| - run: | |
57 |
| - set -x |
58 |
| - git fetch |
59 |
| - ( git branch gh-pages remotes/origin/gh-pages && git clone . --branch=gh-pages _gh-pages/ ) || mkdir _gh-pages |
60 |
| - rm -rf _gh-pages/.git/ |
61 |
| - mkdir -p _gh-pages/branch/ |
62 |
| - # If a push and main, copy build to _gh-pages/ as the "main" |
63 |
| - # deployment. |
64 |
| - - name: Copy new build (main) |
65 |
| - if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
66 |
| - run: | |
67 |
| - set -x |
68 |
| - # Delete everything under _gh-pages/ that is from the |
69 |
| - # primary branch deployment. Eicludes the other branches |
70 |
| - # _gh-pages/branch-* paths, and not including |
71 |
| - # _gh-pages itself. |
72 |
| - find _gh-pages/ -mindepth 1 ! -path '_gh-pages/branch*' -delete |
| 111 | + run: |
73 | 112 | rsync -a _build/dirhtml/ _gh-pages/
|
74 |
| - # If a push and not on main, then copy the build to |
75 |
| - # _gh-pages/branch/$brname (transforming '/' into '--') |
76 |
| - - name: Copy new build (branch) |
77 |
| - if: ${{ github.event_name == 'push' && github.ref != 'refs/heads/main' }} |
78 |
| - run: | |
79 |
| - set -x |
80 |
| - #brname=$(git rev-parse --abbrev-ref HEAD) |
81 |
| - brname="${{github.ref}}" |
82 |
| - brname="${brname##refs/heads/}" |
83 |
| - brdir=${brname//\//--} # replace '/' with '--' |
84 |
| - rm -rf _gh-pages/branch/${brdir} |
85 |
| - rsync -a _build/dirhtml/ _gh-pages/branch/${brdir} |
86 |
| - # Go through each branch in _gh-pages/branch/, if it's not a |
87 |
| - # ref, then delete it. |
88 |
| - - name: Delete old feature branches |
| 113 | + |
| 114 | + # Use gh-pages-multibranch to multiplex different branches into |
| 115 | + # one deployment. See |
| 116 | + # https://github.com/coderefinery/gh-pages-multibranch |
| 117 | + - name: gh-pages multibranch |
| 118 | + uses: coderefinery/gh-pages-multibranch@main |
| 119 | + if: ${{ github.event_name == 'push' && env.MULTIBRANCH == 'true' }} |
| 120 | + with: |
| 121 | + directory: _gh-pages/ |
| 122 | + default_branch: ${{ env.DEFAULT_BRANCH }} |
| 123 | + publish_branch: gh-pages |
| 124 | + |
| 125 | + # Add the .nojekyll file |
| 126 | + - name: nojekyll |
89 | 127 | if: ${{ github.event_name == 'push' }}
|
90 | 128 | run: |
|
91 |
| - set -x |
92 |
| - for brdir in `ls _gh-pages/branch/` ; do |
93 |
| - brname=${brdir//--/\/} # replace '--' with '/' |
94 |
| - if ! git show-ref remotes/origin/$brname ; then |
95 |
| - echo "Removing $brdir" |
96 |
| - rm -r _gh-pages/branch/$brdir/ |
97 |
| - fi |
98 |
| - done |
| 129 | + touch _gh-pages/.nojekyll |
| 130 | +
|
| 131 | + # Save artifact for the next step. |
| 132 | + - uses: actions/upload-artifact@v4 |
| 133 | + if: ${{ github.event_name == 'push' }} |
| 134 | + with: |
| 135 | + name: gh-pages-build |
| 136 | + path: _gh-pages/ |
| 137 | + |
| 138 | + # Deploy in a separate job so that write permissions are restricted |
| 139 | + # to the minimum steps. |
| 140 | + deploy: |
| 141 | + name: Deploy |
| 142 | + runs-on: ubuntu-latest |
| 143 | + needs: build |
| 144 | + # This if can't use the env context - find better way later. |
| 145 | + if: ${{ github.event_name == 'push' }} |
| 146 | + permissions: |
| 147 | + contents: write |
| 148 | + |
| 149 | + steps: |
| 150 | + - uses: actions/download-artifact@v4 |
| 151 | + if: ${{ github.event_name == 'push' && ( env.MULTIBRANCH == 'true' || github.ref == format('refs/heads/{0}', env.DEFAULT_BRANCH )) }} |
| 152 | + with: |
| 153 | + name: gh-pages-build |
| 154 | + path: _gh-pages/ |
| 155 | + |
| 156 | + # As of 2023, we could publish to pages via a Deployment. This |
| 157 | + # isn't done yet to give it time to stabilize (out of beta), and |
| 158 | + # also having a gh-pages branch to check out is rather |
| 159 | + # convenient. |
99 | 160 |
|
100 | 161 | # Deploy
|
101 | 162 | # https://github.com/peaceiris/actions-gh-pages
|
102 | 163 | - name: Deploy
|
103 | 164 | uses: peaceiris/actions-gh-pages@v3
|
104 |
| - if: ${{ github.event_name == 'push' }} |
105 |
| - #if: ${{ success() && github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 165 | + if: ${{ github.event_name == 'push' && ( env.MULTIBRANCH == 'true' || github.ref == format('refs/heads/{0}', env.DEFAULT_BRANCH )) }} |
106 | 166 | with:
|
107 | 167 | publish_branch: gh-pages
|
108 | 168 | github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
0 commit comments