-
-
Notifications
You must be signed in to change notification settings - Fork 294
236 lines (223 loc) · 8.95 KB
/
Copy pathdocumentation.yml
File metadata and controls
236 lines (223 loc) · 8.95 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
name: documentation
on:
pull_request:
types: [opened, synchronize, reopened]
branches: [main]
paths:
[docs/**, .github/workflows/documentation.yml, birdnet_analyzer/cli.py]
push:
branches:
- main
paths:
[docs/**, .github/workflows/documentation.yml, birdnet_analyzer/cli.py]
release:
types: [published]
workflow_dispatch:
inputs:
tag:
description: "Release tag to build and publish (e.g. v2.4.0)"
required: true
type: string
set_stable:
description: "Also publish this version as stable"
required: false
type: boolean
default: false
python_version:
description: "Python to build with (3.11 for tags pinning tensorflow 2.15)"
required: false
type: string
default: "3.13"
permissions:
contents: write
# One group per deploy target: a shared group lets a newer run evict a queued
# deploy. Overlap is safe, the deploy step retries onto a newer gh-pages tip.
concurrency:
group: >-
${{ github.event_name == 'pull_request'
&& format('docs-pr-{0}', github.ref)
|| github.event_name == 'push' && 'docs-deploy-dev'
|| format('docs-deploy-{0}', github.event.release.tag_name || inputs.tag) }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
docs:
runs-on: ubuntu-latest
steps:
- name: Determine build source and target directory
id: target
env:
EVENT: ${{ github.event_name }}
RELEASE_TAG: ${{ github.event.release.tag_name }}
RELEASE_PRERELEASE: ${{ github.event.release.prerelease }}
INPUT_TAG: ${{ inputs.tag }}
INPUT_STABLE: ${{ inputs.set_stable }}
run: |
# [[ =~ ]] anchors on the whole string, so a value containing a
# newline cannot pass and inject extra lines into $GITHUB_OUTPUT.
TAG_RE='^v[0-9]+(\.[0-9]+)*(-[A-Za-z0-9.]+)?$'
case "$EVENT" in
release)
if ! [[ "$RELEASE_TAG" =~ $TAG_RE ]]; then
echo "::error::release tag '$RELEASE_TAG' does not look like vX.Y.Z, not deploying docs"
exit 1
fi
echo "ref=$RELEASE_TAG" >> "$GITHUB_OUTPUT"
echo "dest=$RELEASE_TAG" >> "$GITHUB_OUTPUT"
# Prereleases get their /vX.Y.Z-rc/ dir but must not become stable.
if [ "$RELEASE_PRERELEASE" = "true" ]; then
echo "stable=false" >> "$GITHUB_OUTPUT"
else
echo "stable=true" >> "$GITHUB_OUTPUT"
fi
;;
workflow_dispatch)
if ! [[ "$INPUT_TAG" =~ $TAG_RE ]]; then
echo "::error::tag must look like vX.Y.Z, got '$INPUT_TAG'"
exit 1
fi
echo "ref=$INPUT_TAG" >> "$GITHUB_OUTPUT"
echo "dest=$INPUT_TAG" >> "$GITHUB_OUTPUT"
echo "stable=$INPUT_STABLE" >> "$GITHUB_OUTPUT"
;;
push)
echo "ref=" >> "$GITHUB_OUTPUT"
echo "dest=dev" >> "$GITHUB_OUTPUT"
echo "stable=false" >> "$GITHUB_OUTPUT"
;;
*) # pull_request: build as a check, no deploy
echo "ref=" >> "$GITHUB_OUTPUT"
echo "dest=" >> "$GITHUB_OUTPUT"
echo "stable=false" >> "$GITHUB_OUTPUT"
;;
esac
- uses: actions/checkout@v7
with:
ref: ${{ steps.target.outputs.ref }}
- uses: actions/setup-python@v7
with:
# Old tags pin dependencies that have no wheels for current Python.
python-version: ${{ inputs.python_version || '3.13' }}
- name: Set up uv
uses: astral-sh/setup-uv@v10.0.1
with:
# Re-enabling needs a cache-suffix, or this key collides with the
# ubuntu/3.13 test job's.
enable-cache: false
- name: Install dependencies
run: uv pip install --system .[docs]
- name: Sphinx build
env:
DEST: ${{ steps.target.outputs.dest }}
run: |
EXTRA=""
if [ -n "$DEST" ] && [ "$DEST" != "dev" ]; then
# Old tags hardcode a version and don't load the switcher, so both
# are forced from here; on current conf.py the -D flags are no-ops.
EXTRA="-D version=${DEST#v} -D release=${DEST#v}"
EXTRA="$EXTRA -D html_js_files=https://birdnet-team.github.io/BirdNET-Analyzer/switcher.js"
elif [ "$DEST" = "dev" ]; then
EXTRA="-D version=dev -D release=dev"
fi
# Doctrees in a temp dir so they don't end up in the deployed site.
sphinx-build -E -d "$RUNNER_TEMP/doctrees" $EXTRA docs _build
- name: Check out site chrome from main
if: ${{ steps.target.outputs.dest != '' }}
uses: actions/checkout@v7
with:
ref: main
path: chrome
- name: Check out gh-pages
if: ${{ steps.target.outputs.dest != '' }}
uses: actions/checkout@v7
with:
ref: gh-pages
path: site
- name: Deploy to GitHub Pages
if: ${{ steps.target.outputs.dest != '' }}
env:
DEST: ${{ steps.target.outputs.dest }}
STABLE: ${{ steps.target.outputs.stable }}
run: |
# Guard: $DEST is interpolated into rm -rf below.
[ -n "$DEST" ] || { echo "::error::empty deploy target"; exit 1; }
# Re-runnable: a rejected push re-applies this onto the newer tip.
apply() {
# The root holds only chrome (regenerated below) and version dirs;
# this also clears the old unversioned layout.
find site -mindepth 1 -maxdepth 1 \
! -name .git ! -name 'v[0-9]*' ! -name stable ! -name dev \
-exec rm -rf {} +
rm -rf "site/$DEST"
cp -a _build "site/$DEST"
if [ "$STABLE" = "true" ]; then
rm -rf site/stable
cp -a _build site/stable
# Records which release /stable/ currently holds, so later deploys
# (which regenerate versions.json from disk) can still label it.
echo "$DEST" > site/stable/.version
fi
cp -a chrome/docs/_site/. site/
touch site/.nojekyll
python - <<'EOF'
import json
import re
from pathlib import Path
site = Path("site")
def sort_key(name):
# v3.0.0 sorts above v3.0.0-rc, which sorts above v2.4.0.
core, _, suffix = name[1:].partition("-")
return [int(x) for x in core.split(".")], suffix == "", suffix
versions = sorted(
(d.name for d in site.iterdir()
if d.is_dir()
and re.fullmatch(r"v\d+(\.\d+)*(-[A-Za-z0-9.]+)?", d.name)),
key=sort_key,
reverse=True,
)
marker = site / "stable" / ".version"
stable = marker.read_text().strip() if marker.is_file() else None
# dev first, then releases newest-first; the stable release is listed
# once, as "vX.Y.Z (stable)".
entries = []
if (site / "dev").is_dir():
entries.append({"name": "dev (unreleased)", "path": "dev"})
for v in versions:
if v == stable:
entries.append(
{"name": f"{v} (stable)", "path": "stable", "aliases": [v]}
)
else:
entries.append({"name": v, "path": v})
# /stable/ exists but no version dir claims it (only reachable by
# hand-editing gh-pages): list it plainly instead of hiding it.
if (site / "stable").is_dir() and not any(
e["path"] == "stable" for e in entries
):
entries.insert(
1 if entries and entries[0]["path"] == "dev" else 0,
{"name": f"{stable} (stable)" if stable else "stable",
"path": "stable"},
)
(site / "versions.json").write_text(json.dumps(entries, indent=2) + "\n")
EOF
}
git -C site config user.name "github-actions[bot]"
git -C site config user.email "41898270+github-actions[bot]@users.noreply.github.com"
for attempt in 1 2 3; do
apply
git -C site add -A
if git -C site diff --cached --quiet; then
echo "Nothing to deploy for $DEST"
exit 0
fi
git -C site commit -q -m "Deploy docs: $DEST"
if git -C site push; then
exit 0
fi
# A concurrent deploy landed first: rebuild on top of it and retry.
echo "Push rejected, re-applying onto the latest gh-pages (attempt $attempt)"
git -C site fetch -q origin gh-pages
git -C site reset -q --hard FETCH_HEAD
done
echo "::error::could not push docs for $DEST after 3 attempts"
exit 1