Skip to content

Commit 5268427

Browse files
committed
Set up Read the Docs publishing for master and tags
1 parent f7c5564 commit 5268427

2 files changed

Lines changed: 139 additions & 9 deletions

File tree

.github/workflows/python-publish.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,3 @@ jobs:
2020
run: python -m build
2121
- name: Publish to PyPI
2222
uses: pypa/gh-action-pypi-publish@release/v1
23-
24-
docs:
25-
runs-on: ubuntu-latest
26-
steps:
27-
- name: Trigger ReadTheDocs build
28-
run: |
29-
curl -s -X POST \
30-
-H "Authorization: Token ${{ secrets.RTD_API_TOKEN }}" \
31-
"https://readthedocs.org/api/v3/projects/python-emails/versions/latest/builds/"

.github/workflows/readthedocs.yml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
name: Publish Read the Docs
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
tags:
8+
- '**'
9+
10+
jobs:
11+
publish:
12+
runs-on: ubuntu-latest
13+
env:
14+
RTD_API_TOKEN: ${{ secrets.RTD_API_TOKEN }}
15+
RTD_PROJECT_SLUG: python-emails
16+
steps:
17+
- name: Trigger Read the Docs build
18+
run: |
19+
set -euo pipefail
20+
21+
: "${RTD_API_TOKEN:?RTD_API_TOKEN secret is required}"
22+
23+
api_base="https://app.readthedocs.org/api/v3/projects/${RTD_PROJECT_SLUG}"
24+
auth_header="Authorization: Token ${RTD_API_TOKEN}"
25+
26+
get_version_details() {
27+
local version_slug="$1"
28+
local response_file="${2:-version.json}"
29+
30+
curl \
31+
--silent \
32+
--show-error \
33+
--output "${response_file}" \
34+
--write-out '%{http_code}' \
35+
--header "${auth_header}" \
36+
"${api_base}/versions/${version_slug}/"
37+
}
38+
39+
wait_for_version_slug() {
40+
local version_name="$1"
41+
42+
for attempt in {1..12}; do
43+
local status_code
44+
local version_slug
45+
status_code="$(
46+
curl \
47+
--silent \
48+
--show-error \
49+
--output versions.json \
50+
--write-out '%{http_code}' \
51+
--get \
52+
--header "${auth_header}" \
53+
--data-urlencode "type=tag" \
54+
--data-urlencode "verbose_name=${version_name}" \
55+
"${api_base}/versions/"
56+
)"
57+
58+
if [[ "${status_code}" == "200" ]]; then
59+
version_slug="$(
60+
jq \
61+
--raw-output \
62+
--arg version_name "${version_name}" \
63+
'.results[] | select(.verbose_name == $version_name) | .slug' \
64+
versions.json | head -n 1
65+
)"
66+
67+
if [[ -n "${version_slug}" && "${version_slug}" != "null" ]]; then
68+
printf '%s\n' "${version_slug}"
69+
return 0
70+
fi
71+
fi
72+
73+
sleep 5
74+
done
75+
76+
echo "Read the Docs version '${version_name}' was not found after sync."
77+
if [[ -f versions.json ]]; then
78+
cat versions.json
79+
fi
80+
return 1
81+
}
82+
83+
trigger_build() {
84+
local version_slug="$1"
85+
86+
curl \
87+
--fail-with-body \
88+
--silent \
89+
--show-error \
90+
--request POST \
91+
--header "${auth_header}" \
92+
"${api_base}/versions/${version_slug}/builds/"
93+
}
94+
95+
if [[ "${GITHUB_REF_TYPE}" == "branch" ]]; then
96+
trigger_build latest
97+
exit 0
98+
fi
99+
100+
version_name="${GITHUB_REF_NAME}"
101+
102+
curl \
103+
--fail-with-body \
104+
--silent \
105+
--show-error \
106+
--request POST \
107+
--header "${auth_header}" \
108+
"${api_base}/sync-versions/"
109+
110+
version_slug="$(wait_for_version_slug "${version_name}")"
111+
status_code="$(get_version_details "${version_slug}")"
112+
113+
if [[ "${status_code}" != "200" ]]; then
114+
echo "Failed to fetch Read the Docs version details for '${version_slug}'."
115+
cat version.json
116+
exit 1
117+
fi
118+
119+
active="$(jq -r '.active' version.json)"
120+
hidden="$(jq -r '.hidden' version.json)"
121+
122+
if [[ "${active}" == "true" && "${hidden}" == "false" ]]; then
123+
trigger_build "${version_slug}"
124+
exit 0
125+
fi
126+
127+
curl \
128+
--fail-with-body \
129+
--silent \
130+
--show-error \
131+
--request PATCH \
132+
--header "${auth_header}" \
133+
--header "Content-Type: application/json" \
134+
--data '{"active": true, "hidden": false}' \
135+
"${api_base}/versions/${version_slug}/"
136+
137+
if [[ "${active}" == "true" ]]; then
138+
trigger_build "${version_slug}"
139+
fi

0 commit comments

Comments
 (0)