Skip to content

Commit a5535aa

Browse files
committed
Update Build and deploy docs CI to be able to publish docs for PR, tags and main. Also clean up docs related to PRs when the PR is closed.
1 parent 5d407e7 commit a5535aa

File tree

3 files changed

+104
-55
lines changed

3 files changed

+104
-55
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Build and Deploy Documentation
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
tags:
7+
- 'v[0-9]*.[0-9]*.[0-9]*'
8+
pull_request:
9+
types: [opened, synchronize, reopened]
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: write
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
20+
- name: Setup PDM
21+
uses: pdm-project/setup-pdm@v4
22+
with:
23+
python-version: '3.12'
24+
25+
- name: Install dependencies (default & doc)
26+
run: pdm install --group doc --frozen-lockfile
27+
28+
- name: Build Documentation
29+
working-directory: docs
30+
run: pdm run make dirhtml
31+
32+
- name: Determine deployment folder
33+
id: deploy_folder
34+
run: |
35+
echo "Determining deployment folder..."
36+
if [ "${{ github.event_name }}" = "pull_request" ]; then
37+
echo "Deploying to target pr/${{ github.event.number }}"
38+
echo "DEPLOY_DIR=pr/${{ github.event.number }}" >> $GITHUB_OUTPUT
39+
elif [[ "${{ github.ref }}" == refs/tags/* ]]; then
40+
echo "Deploying to target ${{ github.ref_name }}"
41+
echo "DEPLOY_DIR=${{ github.ref_name }}" >> $GITHUB_OUTPUT
42+
else
43+
echo "Deploying to target main"
44+
echo "DEPLOY_DIR=main" >> $GITHUB_OUTPUT
45+
fi
46+
47+
- name: Deploy Documentation to GitHub Pages
48+
uses: peaceiris/actions-gh-pages@v4
49+
with:
50+
github_token: ${{ secrets.GITHUB_TOKEN }}
51+
publish_dir: docs/build/dirhtml
52+
destination_dir: ${{ steps.deploy_folder.outputs.DEPLOY_DIR }}
53+
publish_branch: gh-pages
54+
55+
- name: Update default site redirect (tag event only)
56+
if: startsWith(github.ref, 'refs/tags/')
57+
run: |
58+
# Extract the tag name from the GITHUB_REF variable.
59+
TAG_NAME=${GITHUB_REF##*/}
60+
# Create an index.html file that redirects to /<tag>/index.html.
61+
echo "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"0; url=./${TAG_NAME}/index.html\" /></head><body></body></html>" > docs/build/index.html
62+
# Clone the gh-pages branch using token authentication
63+
git clone --branch gh-pages https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} gh-pages
64+
cp docs/build/index.html gh-pages/index.html
65+
cd gh-pages
66+
git config user.name "github-actions"
67+
git config user.email "github-actions@github.com"
68+
git add index.html
69+
git commit -m "Update default documentation redirect to tag ${GITHUB_REF##*/}" || echo "No changes to commit"
70+
git push origin gh-pages

.github/workflows/build_deploy_documentation.yml

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Cleanup PR Documentation
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
cleanup:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
steps:
13+
- name: Checkout gh-pages branch
14+
uses: actions/checkout@v4
15+
with:
16+
repository: ${{ github.repository }}
17+
ref: gh-pages
18+
token: ${{ secrets.GITHUB_TOKEN }}
19+
path: gh-pages
20+
21+
- name: Remove PR documentation for closed PR
22+
run: |
23+
PR_NUMBER="${{ github.event.number }}"
24+
echo "Removing documentation for PR #${PR_NUMBER}"
25+
rm -rf gh-pages/pr/${PR_NUMBER}
26+
27+
- name: Commit and push cleanup
28+
working-directory: gh-pages
29+
run: |
30+
git config user.name "github-actions"
31+
git config user.email "github-actions@github.com"
32+
git add .
33+
git commit -m "Cleanup documentation for closed PR #${{ github.event.number }}" || echo "No changes to commit"
34+
git push origin HEAD:gh-pages

0 commit comments

Comments
 (0)