-
Notifications
You must be signed in to change notification settings - Fork 13
329 lines (290 loc) · 10.8 KB
/
python-docs.yml
File metadata and controls
329 lines (290 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
name: Pip Build
on:
workflow_call:
inputs:
package-name:
description: "The Python-importable package name for which docs are being built"
required: true
type: string
python-version:
description: "The Python version to build and test with"
default: "3.9"
required: false
type: string
install-package:
description: "Install the Python package prior to building documentation"
required: false
default: true
type: boolean
docs-template-repo:
description: "Optional external repository holding the documentation template"
required: false
default: ""
type: string
docs-template-ref:
description: "The ref to checkout for the template repo"
default: "master"
required: false
type: string
docs-directory:
default: "docs/"
description: "The documentation directory"
required: false
type: string
docs-build-directory:
default: "build/html"
description: "The directory where built documentation will be, relative to docs-directory"
required: false
type: string
experimental:
description: "Mark this version as experimental and not required to pass"
required: false
default: false
type: boolean
deploy:
description: "Deploy the documentation to the gh-pages branch if successfully built"
required: false
default: false
type: boolean
docs-extras:
default: ""
description: "Extra packages to be installed for documentation"
required: false
type: string
system-packages:
default: ""
description: "CI-specific system packages required for docs building"
required: false
type: string
outputs: {}
env:
MPLBACKEND: "agg"
QT_QPA_PLATFORM: "offscreen"
jobs:
build:
name: "Python ${{ inputs.python-version }}: documentation building"
runs-on: ubuntu-20.04
continue-on-error: ${{ inputs.experimental }}
defaults:
run:
# The following allows for each run step to utilize ~/.bash_profile
# for setting up the per-step initial state.
# --login: a login shell. Source ~/.bash_profile
# -e: exit on first error
# -o pipefail: piped processes are important; fail if they fail
shell: bash --login -eo pipefail {0}
outputs:
deploy_version: ${{ steps.version.outputs.built_docs_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- uses: actions/checkout@v4
if: inputs.docs-template-repo != ''
with:
repository: ${{ inputs.docs-template-repo }}
ref: ${{ inputs.docs-template-ref }}
path: ${{ inputs.docs-directory }}
submodules: 'recursive'
- name: Check that the documentation directory exists
run: |
if [ ! -d "${{ inputs.docs-directory}}" ]; then
echo "Documentation directory '${{ inputs.docs-directory}}' does not exist!" \
| tee "$GITHUB_STEP_SUMMARY"
exit 1
fi
- name: Check version tag for deployment
id: version
shell: bash -l {0}
run: |
if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
# It may be a PR against a non-master branch, but that doesn't matter here.
version="master"
elif [[ "$GITHUB_EVENT_NAME" == "push" && "${{ github.ref }}" = refs/heads/* ]]; then
# If this is a push to a branch, then use that branch name.
# `basename refs/heads/a` -> "a"
version="$(basename "${{ github.ref }}")"
else
# For refs/tags and anything else, use the version from git.
version="$(git describe --tags)"
fi
(
echo "Package version: $(git describe --tags)"
echo "Documentation version name: ${version}"
) | tee "$GITHUB_STEP_SUMMARY"
echo "built_docs_version=${version}" >> $GITHUB_OUTPUT
- name: Check environment variables for issues
run: |
echo "* Package to be built: ${{ inputs.package-name }}"
echo "* Pip 'extras' for documentation: ${{ inputs.docs-extras }}"
echo "* General pip packages required for documentation: ${{ inputs.docs-extras }}"
if [ -z "${{ steps.version.outputs.built_docs_version }}" ]; then
echo "Built docs version unset? See previous step"
exit 1
fi
- name: Install required system packages
if: inputs.system-packages != ''
run: |
sudo apt-get update
sudo apt-get -y install ${{ inputs.system-packages }}
- name: Prepare for log files
run: |
mkdir $HOME/logs
- uses: actions/setup-python@v5
with:
python-version: '${{ inputs.python-version }}'
- name: Upgrade pip
run: |
pip install --upgrade pip
- name: Installing package
if: ${{ inputs.install-package }}
run: |
pip install .[test,doc]
- name: Installing documentation extras
run: |
# 1. escape '<' so the user doesn't have to
# 2. escape '>' so the user doesn't have to
# 3. allow conda/pip to use the same requirements spec;
# conda expects pkg=ver but pip expects pkg==ver; using a basic
# (not =<>)=(not =) to avoid incompatibility with macOS sed not supporting
# '=\+'
#
if [ -n "${{ inputs.docs-extras }}" ]; then
input_requirements=$(
echo "${{ inputs.docs-extras }}" |
sed -e "s/</\</g" |
sed -e "s/>/\>/g" |
sed -e 's/\([^=<>]\)=\([^=]\)/\1==\2/g'
)
declare -a docs_requirements=()
for req in $input_requirements; do
docs_requirements+=( "$req" )
done
set -x
pip install "${docs_requirements[@]}"
else
echo "No extras to install."
fi
- name: Check the pip packages in the test env
run: |
pip list
- name: Build documentation
run: |
cd "${{ inputs.docs-directory }}"
make html 2>&1 | tee $HOME/logs/docs-build.txt
- name: Upload documentation as artifact
uses: actions/upload-artifact@v4
with:
name: Python ${{ inputs.python-version }} - documentation
path: "${{ inputs.docs-directory }}/${{ inputs.docs-build-directory }}"
- name: Upload log file artifacts
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: Python ${{ inputs.python-version }} - documentation - logs
path: "~/logs"
deploy:
name: "Documentation deployment"
runs-on: ubuntu-20.04
needs: build
if: ${{ inputs.deploy }}
permissions:
# to deploy to Pages
pages: write
# push to repo gh-pages branch (*gasp*; is there a better way?)
contents: write
# deployments: write
# to verify the deployment originates from an appropriate source
id-token: write
defaults:
run:
# The following allows for each run step to utilize ~/.bash_profile
# for setting up the per-step initial state.
# --login: a login shell. Source ~/.bash_profile
# -e: exit on first error
# -o pipefail: piped processes are important; fail if they fail
shell: bash --login -eo pipefail {0}
environment:
name: gh-pages
# url: ${{ steps.build-and-deploy.outputs.page_url }}
steps:
- uses: actions/setup-python@v5
with:
python-version: '${{ inputs.python-version }}'
- name: Upgrade pip
run: |
pip install --upgrade pip
- name: Installing documentation upload requirements
run: |
pip install docs-versions-menu
- name: Download documentation artifact
uses: actions/download-artifact@v4
with:
name: Python ${{ inputs.python-version }} - documentation
- name: Configure git for docs deployment
run: |
git config --global user.name github-actions
git config --global user.email github-actions@github.com
git config --global init.defaultBranch gh-pages
- name: List cached documentation
run: |
ls -lR
- name: Update documentation with docs-versions-menu
run: |
# Adapted from docs-versions-menu workflow and my old caproto attempt:
# - https://github.com/goerz/docs_versions_menu/blob/master/.github/workflows/docs.yml
# - https://github.com/caproto/caproto/blob/master/.github/workflows/docs.yml
set -x
git clone --branch gh-pages https://github.com/${{ github.repository }} "$HOME/gh-pages" || (
mkdir "$HOME/gh-pages"
cd "$HOME/gh-pages"
git init
)
rsync -av --delete ./ "$HOME/gh-pages/${{ needs.build.outputs.deploy_version }}/"
# Run docs-versions-menu
cd "$HOME/gh-pages"
docs-versions-menu
- name: Commit updated documentation to gh-pages
run: |
cd "$HOME/gh-pages"
git add --all --verbose
git status
if ! git rev-parse HEAD &>/dev/null ; then
git commit --verbose \
-m "Initial commit of documentation" \
-m "Deployed from commit ${GITHUB_SHA} (${GITHUB_REF})"
else
commit_message_file="$HOME/documentation_commit_message.txt"
echo "The commit message will be:"
echo "---------------------------"
git log --format=%B -n 1 | tee "${commit_message_file}"
echo "---------------------------"
last_log_line=$(cat "${commit_message_file}" | grep -v '^$' | tail -n1)
last_author=$(git log --format=%an -n 1)
echo "Last log line: ${last_log_line}"
echo "Last author: ${last_author}"
echo "Current ref: ${{ github.ref }}"
if [[ "$last_author" == "github-actions"* && "$last_log_line" == *"${{ github.ref }}"* ]]; then
# Amend if the previous commit was done by Actions and was based on the same branch/tag name
echo "Amending previous commit"
echo "Deployed from commit ${GITHUB_SHA} (${GITHUB_REF})" >> "${commit_message_file}"
git commit --verbose --amend --file="${commit_message_file}"
else
echo "Making new commit"
git commit --verbose \
-m "Auto-update from Github Actions Workflow" \
-m "Deployed from commit ${GITHUB_SHA} (${GITHUB_REF})" ||
echo "Documentation unchanged"
fi
fi
git log -n 2 --stat
- name: Pushing documentation
if: ${{ inputs.deploy }}
run: |
cd "$HOME/gh-pages"
# --force-with-lease=gh-pages
git push --verbose \
--force \
"https://${GITHUB_ACTOR}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}" \
gh-pages