Skip to content

Commit 6af06a3

Browse files
committed
Update GH actions
1 parent f498fd5 commit 6af06a3

File tree

1 file changed

+101
-79
lines changed

1 file changed

+101
-79
lines changed

.github/workflows/sphinx.yml

+101-79
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,69 @@
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+
29

310
name: sphinx
411
on: [push, pull_request]
512

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"
1013
env:
11-
DEFAULT_BRANCH: main
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
1222

1323

1424
jobs:
15-
build-and-deploy:
16-
name: Build and gh-pages
25+
build:
26+
name: Build
1727
runs-on: ubuntu-latest
28+
permissions:
29+
contents: read
30+
1831
steps:
1932
# https://github.com/marketplace/actions/checkout
20-
- uses: actions/checkout@v2
33+
- uses: actions/checkout@v4
2134
with:
2235
fetch-depth: 0
2336
lfs: true
37+
2438
# https://github.com/marketplace/actions/setup-python
2539
# ^-- This gives info on matrix testing.
2640
- name: Install Python
27-
uses: actions/setup-python@v2
28-
with:
29-
python-version: 3.8
30-
# https://docs.github.com/en/actions/guides/building-and-testing-python#caching-dependencies
31-
# ^-- How to set up caching for pip on Ubuntu
32-
- name: Cache pip
33-
uses: actions/cache@v2
41+
uses: actions/setup-python@v4
3442
with:
35-
path: ~/.cache/pip
36-
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
37-
restore-keys: |
38-
${{ runner.os }}-pip-
39-
${{ runner.os }}-
43+
python-version: '3.11'
44+
cache: 'pip'
45+
4046
# https://docs.github.com/en/actions/guides/building-and-testing-python#installing-dependencies
4147
# ^-- This gives info on installing dependencies with pip
4248
- name: Install dependencies
4349
run: |
4450
python -m pip install --upgrade pip
4551
pip install -r requirements.txt
52+
53+
# Debug
4654
- 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}}
4760
run: |
48-
echo "github.ref:" ${{github.ref}}
49-
echo "github.event_name:" ${{github.event_name}}
50-
echo "github.head_ref:" ${{github.head_ref}}
51-
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}"
5267
set -x
5368
git rev-parse --abbrev-ref HEAD
5469
git branch
@@ -61,7 +76,8 @@ jobs:
6176
6277
# Build
6378
- uses: ammaraskar/sphinx-problem-matcher@master
64-
- name: Build Sphinx docs
79+
- name: Build Sphinx docs (dirhtml)
80+
# SPHINXOPTS used via environment variables
6581
run: |
6682
make dirhtml
6783
# This fixes broken copy button icons, as explained in
@@ -73,76 +89,82 @@ jobs:
7389
# https://github.com/readthedocs/sphinx_rtd_theme/pull/1025
7490
sed -i 's/url_root="#"/url_root=""/' _build/dirhtml/index.html || true
7591
92+
# singlehtml
93+
- name: Generate singlehtml
94+
if: ${{ env.GENERATE_SINGLEHTML == 'true' }}
95+
run: |
96+
make singlehtml
97+
mv _build/singlehtml/ _build/dirhtml/singlehtml/
7698
77-
# The following supports building all branches and combining on
78-
# 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}
79106
80-
# Clone and set up the old gh-pages branch
81-
- 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/
82110
if: ${{ github.event_name == 'push' }}
83-
run: |
84-
set -x
85-
git fetch
86-
( git branch gh-pages remotes/origin/gh-pages && git clone . --branch=gh-pages _gh-pages/ ) || mkdir _gh-pages
87-
rm -rf _gh-pages/.git/
88-
mkdir -p _gh-pages/branch/
89-
# If a push and default branch, copy build to _gh-pages/ as the "main"
90-
# deployment.
91-
- name: Copy new build (default branch)
92-
if: |
93-
contains(github.event_name, 'push') &&
94-
contains(github.ref, env.DEFAULT_BRANCH)
95-
run: |
96-
set -x
97-
# Delete everything under _gh-pages/ that is from the
98-
# primary branch deployment. Eicludes the other branches
99-
# _gh-pages/branch-* paths, and not including
100-
# _gh-pages itself.
101-
find _gh-pages/ -mindepth 1 ! -path '_gh-pages/branch*' -delete
111+
run:
102112
rsync -a _build/dirhtml/ _gh-pages/
103-
# If a push and not on default branch, then copy the build to
104-
# _gh-pages/branch/$brname (transforming '/' into '--')
105-
- name: Copy new build (branch)
106-
if: |
107-
contains(github.event_name, 'push') &&
108-
!contains(github.ref, env.DEFAULT_BRANCH)
109-
run: |
110-
set -x
111-
#brname=$(git rev-parse --abbrev-ref HEAD)
112-
brname="${{github.ref}}"
113-
brname="${brname##refs/heads/}"
114-
brdir=${brname//\//--} # replace '/' with '--'
115-
rm -rf _gh-pages/branch/${brdir}
116-
rsync -a _build/dirhtml/ _gh-pages/branch/${brdir}
117-
# Go through each branch in _gh-pages/branch/, if it's not a
118-
# ref, then delete it.
119-
- name: Delete old feature branches
120-
if: ${{ github.event_name == 'push' }}
121-
run: |
122-
set -x
123-
for brdir in `ls _gh-pages/branch/` ; do
124-
brname=${brdir//--/\/} # replace '--' with '/'
125-
if ! git show-ref remotes/origin/$brname ; then
126-
echo "Removing $brdir"
127-
rm -r _gh-pages/branch/$brdir/
128-
fi
129-
done
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
130124

131125
# Add the .nojekyll file
132126
- name: nojekyll
133127
if: ${{ github.event_name == 'push' }}
134128
run: |
135129
touch _gh-pages/.nojekyll
136130
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.
160+
137161
# Deploy
138162
# https://github.com/peaceiris/actions-gh-pages
139163
- name: Deploy
140164
uses: peaceiris/actions-gh-pages@v3
141-
if: ${{ github.event_name == 'push' }}
142-
#if: ${{ success() && github.event_name == 'push' && github.ref == 'refs/heads/${{ env.DEFAULT_BRANCH }}' }}
165+
if: ${{ github.event_name == 'push' && ( env.MULTIBRANCH == 'true' || github.ref == format('refs/heads/{0}', env.DEFAULT_BRANCH )) }}
143166
with:
144167
publish_branch: gh-pages
145168
github_token: ${{ secrets.GITHUB_TOKEN }}
146169
publish_dir: _gh-pages/
147170
force_orphan: true
148-

0 commit comments

Comments
 (0)