Skip to content

Commit 054b28f

Browse files
committed
feature: documentation previews for PRs with github
1 parent a0295f8 commit 054b28f

1 file changed

Lines changed: 162 additions & 19 deletions

File tree

.github/workflows/static.yml

Lines changed: 162 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# Simple workflow for deploying static content to GitHub Pages
2-
name: Deploy static content to Pages
3-
1+
# Workflow for deploying static content to GitHub Pages with PR previews
2+
name: Deploy Documentation
43
on:
54
# Runs on pushes targeting the default branch
65
push:
76
branches: ["main"]
8-
7+
# Runs on pull requests for preview builds
8+
pull_request:
9+
branches: ["main"]
910
# Allows you to run this workflow manually from the Actions tab
1011
workflow_dispatch:
1112

@@ -14,50 +15,192 @@ permissions:
1415
contents: write
1516
pages: write
1617
id-token: write
18+
pull-requests: write
1719

18-
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
19-
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
20+
# Allow only one concurrent deployment per branch/PR
2021
concurrency:
21-
group: "pages"
22+
group: "pages-${{ github.ref }}"
2223
cancel-in-progress: false
2324

2425
jobs:
25-
# Single deploy job since we're just deploying
26-
deploy:
27-
environment:
28-
name: github-pages
29-
url: ${{ steps.deployment.outputs.page_url }}
26+
# Build job - runs for both main branch and PRs
27+
build:
3028
runs-on: ubuntu-latest
3129
steps:
3230
- name: Checkout
3331
uses: actions/checkout@v4
32+
3433
- name: Set up python
3534
uses: actions/setup-python@v4
3635
with:
3736
python-version: '3.12'
37+
3838
- name: Install dependencies
3939
run: |
4040
sudo apt install pandoc
4141
pip install -r requirements.txt
4242
pip install ipykernel rdkit cairosvg pymzml
4343
pip install .
44+
4445
- name: Build Documentation
4546
run: sphinx-build -b html ./docs ./_build
47+
48+
- name: Copy binder files
49+
run: cp -r ./.binder ./_build
50+
51+
- name: Upload build artifacts
52+
uses: actions/upload-artifact@v4
53+
with:
54+
name: documentation-${{ github.sha }}
55+
path: _build/
56+
retention-days: 30
57+
58+
# Deploy to main GitHub Pages (only for main branch)
59+
deploy-main:
60+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
61+
needs: build
62+
environment:
63+
name: github-pages
64+
url: ${{ steps.deployment.outputs.page_url }}
65+
runs-on: ubuntu-latest
66+
steps:
67+
- name: Download build artifacts
68+
uses: actions/download-artifact@v4
69+
with:
70+
name: documentation-${{ github.sha }}
71+
path: _build/
72+
4673
- name: Setup Pages
4774
uses: actions/configure-pages@v5
48-
- name: Push _build to gh_pages branch
75+
76+
- name: Upload to GitHub Pages
77+
uses: actions/upload-pages-artifact@v3
78+
with:
79+
path: _build/
80+
81+
- name: Deploy to GitHub Pages
82+
id: deployment
83+
uses: actions/deploy-pages@v4
84+
85+
# Deploy PR preview to separate branch
86+
deploy-preview:
87+
if: github.event_name == 'pull_request'
88+
needs: build
89+
runs-on: ubuntu-latest
90+
steps:
91+
- name: Checkout
92+
uses: actions/checkout@v4
93+
with:
94+
token: ${{ secrets.GITHUB_TOKEN }}
95+
96+
- name: Download build artifacts
97+
uses: actions/download-artifact@v4
98+
with:
99+
name: documentation-${{ github.sha }}
100+
path: _build/
101+
102+
- name: Deploy PR preview
49103
run: |
50-
cp -r ./.binder ./_build
104+
# Create a unique branch name for this PR
105+
PR_BRANCH="pr-preview-${{ github.event.number }}"
106+
51107
cd _build
52108
# Initialize a new Git repository in the _build directory
53109
git init
54110
git config user.name "github-actions[bot]"
55111
git config user.email "github-actions[bot]@users.noreply.github.com"
56-
112+
57113
# Add all files and commit changes
58114
git add .
59-
git branch -M gh_pages
60-
git commit -m "Deploy documentation [skip ci]"
115+
git checkout -b $PR_BRANCH
116+
git commit -m "Deploy PR #${{ github.event.number }} preview [skip ci]"
117+
118+
# Force push to the PR preview branch
119+
git push --force "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" $PR_BRANCH
120+
121+
- name: Comment on PR with preview link
122+
uses: actions/github-script@v7
123+
with:
124+
script: |
125+
const prNumber = context.payload.number;
126+
const repoOwner = context.repo.owner;
127+
const repoName = context.repo.repo;
128+
const previewUrl = `https://${repoOwner}.github.io/${repoName}/pr-preview-${prNumber}/`;
129+
130+
// Check if we already commented
131+
const comments = await github.rest.issues.listComments({
132+
owner: repoOwner,
133+
repo: repoName,
134+
issue_number: prNumber,
135+
});
136+
137+
const botComment = comments.data.find(comment =>
138+
comment.user.login === 'github-actions[bot]' &&
139+
comment.body.includes('📖 Documentation Preview')
140+
);
141+
142+
const commentBody = `📖 **Documentation Preview**
143+
144+
The documentation for this PR has been built and is available at:
145+
🔗 **[View Preview](${previewUrl})**
146+
147+
This preview will be updated automatically when you push new commits to this PR.
148+
149+
---
150+
*Preview built from commit: \`${context.sha.substring(0, 7)}\`*`;
151+
152+
if (botComment) {
153+
// Update existing comment
154+
await github.rest.issues.updateComment({
155+
owner: repoOwner,
156+
repo: repoName,
157+
comment_id: botComment.id,
158+
body: commentBody
159+
});
160+
} else {
161+
// Create new comment
162+
await github.rest.issues.createComment({
163+
owner: repoOwner,
164+
repo: repoName,
165+
issue_number: prNumber,
166+
body: commentBody
167+
});
168+
}
169+
170+
# Cleanup old PR preview branches
171+
cleanup-previews:
172+
if: github.event_name == 'pull_request' && github.event.action == 'closed'
173+
runs-on: ubuntu-latest
174+
steps:
175+
- name: Checkout
176+
uses: actions/checkout@v4
177+
with:
178+
token: ${{ secrets.GITHUB_TOKEN }}
179+
fetch-depth: 0
180+
181+
- name: Delete PR preview branch
182+
run: |
183+
PR_BRANCH="pr-preview-${{ github.event.number }}"
61184
62-
# Force push to the gh-pages branch
63-
git push --force "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" gh_pages
185+
# Check if the branch exists and delete it
186+
if git ls-remote --heads origin $PR_BRANCH | grep $PR_BRANCH; then
187+
git push origin --delete $PR_BRANCH
188+
echo "Deleted preview branch: $PR_BRANCH"
189+
else
190+
echo "Preview branch $PR_BRANCH not found"
191+
fi
192+
193+
- name: Comment cleanup notification
194+
uses: actions/github-script@v7
195+
with:
196+
script: |
197+
const prNumber = context.payload.number;
198+
199+
await github.rest.issues.createComment({
200+
owner: context.repo.owner,
201+
repo: context.repo.repo,
202+
issue_number: prNumber,
203+
body: `🧹 **Preview Cleanup**
204+
205+
The documentation preview for this PR has been cleaned up and is no longer available.`
206+
});

0 commit comments

Comments
 (0)