Skip to content

Commit 671a7e7

Browse files
committed
ci: add site deploy workflow
1 parent 85abbe3 commit 671a7e7

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

.github/workflows/publish_docs.yml

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# TorchMeter, AGPL-3.0 license
2+
# Author: Ahzyuan
3+
# Repo: https://github.com/TorchMeter/torchmeter
4+
5+
name: 📤 Deploy TorchMeter Docs 📜
6+
7+
on:
8+
workflow_dispatch:
9+
inputs:
10+
repo_name:
11+
description: 'The name of the repo inside TorchMeter that contains the docs files'
12+
default: 'torchmeter'
13+
required: true
14+
type: string
15+
# schedule:
16+
# # Runs every day at 00:00 UTC
17+
# - cron: "0 0 * * *"
18+
19+
permissions:
20+
contents: write
21+
pages: write
22+
id-token: write
23+
24+
jobs:
25+
Get-Latest-Version:
26+
runs-on: ubuntu-latest
27+
28+
outputs:
29+
tag: ${{ steps.latest-tag.outputs.tag }}
30+
31+
steps:
32+
- name: Fetch Code
33+
uses: actions/checkout@v4
34+
with:
35+
repository: TorchMeter/${{ github.event.inputs.repo_name || 'torchmeter' }}
36+
fetch-depth: 0
37+
38+
- name: Check Docs files
39+
run: |
40+
if [ ! -d docs ]; then
41+
echo -e "\n❌ Docs directory does not exist" >&2
42+
exit 1
43+
elif [ ! -f mkdocs.yml ]; then
44+
echo -e "\n❌ mkdocs.yml does not exist" >&2
45+
exit 1
46+
else
47+
echo -e "\n✅ All necessary files for building documentation are ready"
48+
fi
49+
50+
- name: Get Latest Tag
51+
id: latest-tag
52+
run: |
53+
tags=$(git for-each-ref --sort=-creatordate --format '%(refname:short)' refs/tags)
54+
55+
LATEST_TAG=""
56+
found=0
57+
58+
for tag in $tags; do
59+
if [[ $tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
60+
LATEST_TAG=$tag
61+
found=1
62+
break
63+
fi
64+
done
65+
66+
if [ $found -ne 1 ]; then
67+
echo -e "\n❌ No valid tag found matching format vA.B.C" >&2
68+
exit 1
69+
fi
70+
71+
echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
72+
echo -e "\n✅ Docs will be built for version $LATEST_TAG"
73+
74+
Deploy:
75+
runs-on: ubuntu-latest
76+
77+
needs: Get-Latest-Version
78+
if: github.repository == 'TorchMeter/docs'
79+
80+
steps:
81+
- name: Fetch Code
82+
uses: actions/checkout@v4
83+
84+
- name: Configure Git Credentials
85+
run: |
86+
git config user.name github-actions[bot]
87+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
88+
89+
- uses: actions/setup-python@v4
90+
with:
91+
python-version: 3.x
92+
93+
- name: Cache Mkdocs
94+
uses: actions/cache@v4
95+
with:
96+
path: ~/.cache/pip
97+
key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.cfg') }}
98+
restore-keys: |
99+
${{ runner.os }}-pip-
100+
101+
- name: Setup Drawio
102+
run: |
103+
set -eo pipefail
104+
105+
drawio_arch=amd64
106+
drawio_version=18.1.3
107+
drawio_sha256sum=39a50f25ad52d6909c5c18d89a7cfc193e8e31fb98458a390c0a0709d22e9e10
108+
109+
drawio_deb="drawio-${drawio_arch}-${drawio_version}.deb"
110+
drawio_url="https://github.com/jgraph/drawio-desktop/releases/download/v${drawio_version}/${drawio_deb}"
111+
112+
curl -L -o "$drawio_deb" "$drawio_url" &
113+
sudo apt-get install -y libasound2t64 xvfb &
114+
wait
115+
116+
sha256sum --check <<<"${drawio_sha256sum} $drawio_deb"
117+
sudo apt-get install -y ./"$drawio_deb"
118+
119+
- name: Prepare for Building Docs
120+
run: |
121+
WORKING_PATH=$(pwd)
122+
set -eo pipefail
123+
124+
# clone torchmeter's assets and source code in parallel
125+
git clone https://github.com/TorchMeter/assets.git /tmp/torchmeter-assets &
126+
git clone https://github.com/TorchMeter/${{ github.event.inputs.repo_name || 'torchmeter' }}.git /tmp/torchmeter-src &
127+
wait
128+
129+
# checkout the correct version
130+
(cd /tmp/torchmeter-assets && git checkout ${{ needs.Get-Latest-Version.outputs.tag }} || git checkout master) &
131+
(cd /tmp/torchmeter-src && git checkout ${{ needs.Get-Latest-Version.outputs.tag }}) &
132+
wait
133+
134+
# copy docs-related files to working directory in parallel
135+
cp -rfL /tmp/torchmeter-src/docs $WORKING_PATH/docs &
136+
cp -rfL /tmp/torchmeter-src/torchmeter $WORKING_PATH/torchmeter &
137+
cp -f /tmp/torchmeter-src/mkdocs.yml $WORKING_PATH/mkdocs.yml &
138+
cp -f /tmp/torchmeter-src/default_cfg.yaml $WORKING_PATH/default_cfg.yaml &
139+
wait
140+
141+
# link to assets for all locales directories
142+
cd $WORKING_PATH/docs/src/
143+
for dir in ./*/; do
144+
if [ -d "$dir" ]; then
145+
ln -sf /tmp/torchmeter-assets "$dir/assets" &
146+
fi
147+
done
148+
wait
149+
150+
# install dependencies
151+
python3 -m pip install --upgrade pip
152+
DEPS=$(python -c "import configparser; c=configparser.ConfigParser(); c.read('/tmp/torchmeter-src/setup.cfg'); print(' '.join(c['options.extras_require']['docs'].split('\n')))")
153+
python3 -m pip install $DEPS
154+
155+
cd $WORKING_PATH
156+
157+
- name: Deploy Stable
158+
run: |
159+
# mkdocs gh-deploy --force
160+
161+
git checkout gh-pages || git checkout --orphan gh-pages
162+
git pull origin gh-pages --depth=1 || echo "No remote changes to pull"
163+
git checkout master
164+
165+
xvfb-run -a mike deploy -pu ${{ needs.Get-Latest-Version.outputs.tag }} latest
166+
mike set-default -p latest
167+
168+
- name: Deploy Dev
169+
run: |
170+
WORKING_PATH=$(pwd)
171+
rm -rf ./*
172+
set -eo pipefail
173+
174+
# switch to master branch
175+
(cd /tmp/torchmeter-assets && git checkout master) &
176+
(cd /tmp/torchmeter-src && git checkout master) &
177+
wait
178+
179+
# copy docs-related files to working directory in parallel
180+
cp -rfL /tmp/torchmeter-src/docs $WORKING_PATH/docs &
181+
cp -rfL /tmp/torchmeter-src/torchmeter $WORKING_PATH/torchmeter &
182+
cp -f /tmp/torchmeter-src/mkdocs.yml $WORKING_PATH/mkdocs.yml &
183+
cp -f /tmp/torchmeter-src/default_cfg.yaml $WORKING_PATH/default_cfg.yaml &
184+
wait
185+
186+
# link to assets for all locales directories
187+
cd $WORKING_PATH/docs/src/
188+
for dir in ./*/; do
189+
if [ -d "$dir" ]; then
190+
ln -sf /tmp/torchmeter-assets "$dir/assets" &
191+
fi
192+
done
193+
wait
194+
195+
git checkout gh-pages || git checkout --orphan gh-pages
196+
git pull origin gh-pages --depth=1 || echo "No remote changes to pull"
197+
git checkout master
198+
199+
cd $WORKING_PATH
200+
xvfb-run -a mike deploy -pu dev

0 commit comments

Comments
 (0)