Skip to content

Commit 2b1b80a

Browse files
committed
Add GitHub Actions workflows for PR preview builds and cleanup
Add two workflows to automatically build and deploy HTML previews for pull requests: - pr-preview.yml: Builds AsciiDoc to HTML and deploys previews to GitHub Pages subdirectories (pr-{number}/) without interfering with the main production site. Supports fork PRs using pull_request_target and comments on PRs with preview links. - pr-preview-cleanup.yml: Automatically removes PR preview directories from gh-pages when PRs are closed or merged. Each PR gets a unique preview URL at {owner}.github.io/{repo}/pr-{number}/main.html that updates automatically on new commits.
1 parent 23029e9 commit 2b1b80a

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: PR Preview Cleanup
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
cleanup:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
with:
17+
ref: gh-pages
18+
fetch-depth: 0
19+
20+
- name: Remove PR preview directory
21+
run: |
22+
PR_NUM=${{ github.event.pull_request.number }}
23+
PREVIEW_DIR="pr-${PR_NUM}"
24+
25+
if [ -d "$PREVIEW_DIR" ]; then
26+
echo "Removing preview directory: $PREVIEW_DIR"
27+
rm -rf "$PREVIEW_DIR"
28+
29+
# Check if there are any changes to commit
30+
if [ -n "$(git status --porcelain)" ]; then
31+
git config user.name "github-actions[bot]"
32+
git config user.email "github-actions[bot]@users.noreply.github.com"
33+
git add -A
34+
git commit -m "Remove preview for PR #${PR_NUM}"
35+
git push
36+
echo "Removed preview directory: $PREVIEW_DIR"
37+
else
38+
echo "No changes to commit"
39+
fi
40+
else
41+
echo "Preview directory $PREVIEW_DIR does not exist, skipping cleanup"
42+
fi
43+

.github/workflows/pr-preview.yml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: PR Preview Build and Deploy
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
contents: write
9+
pull-requests: write
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: pr-preview-${{ github.event.pull_request.number }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
build-and-deploy:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout base repository
22+
uses: actions/checkout@v4
23+
with:
24+
ref: ${{ github.event.pull_request.base.ref }}
25+
fetch-depth: 0
26+
27+
- name: Checkout PR branch
28+
uses: actions/checkout@v4
29+
with:
30+
ref: ${{ github.event.pull_request.head.sha }}
31+
path: pr-source
32+
33+
- name: Set up Ruby
34+
uses: ruby/setup-ruby@v1
35+
with:
36+
ruby-version: '3.1'
37+
bundler-cache: true
38+
39+
- name: Install Asciidoctor
40+
run: gem install asciidoctor
41+
42+
- name: Build HTML from AsciiDoc
43+
working-directory: pr-source/supplementary_style_guide
44+
run: asciidoctor main.adoc
45+
46+
- name: Prepare preview directory
47+
run: |
48+
PR_NUM=${{ github.event.pull_request.number }}
49+
PREVIEW_DIR="deploy/pr-${PR_NUM}"
50+
mkdir -p "${PREVIEW_DIR}"
51+
52+
# Copy the generated HTML file from PR source
53+
cp pr-source/supplementary_style_guide/main.html "${PREVIEW_DIR}/"
54+
55+
# Copy images directory preserving structure
56+
if [ -d "pr-source/supplementary_style_guide/images" ]; then
57+
cp -r pr-source/supplementary_style_guide/images "${PREVIEW_DIR}/"
58+
fi
59+
60+
# Copy any other image files preserving directory structure
61+
find pr-source/supplementary_style_guide -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" -o -name "*.gif" -o -name "*.svg" \) | while read -r file; do
62+
rel_path="${file#pr-source/supplementary_style_guide/}"
63+
target_dir="${PREVIEW_DIR}/$(dirname "$rel_path")"
64+
mkdir -p "$target_dir"
65+
cp "$file" "$target_dir/"
66+
done
67+
68+
- name: Deploy to GitHub Pages
69+
uses: peaceiris/actions-gh-pages@v3
70+
with:
71+
github_token: ${{ secrets.GITHUB_TOKEN }}
72+
publish_dir: ./deploy/pr-${{ github.event.pull_request.number }}
73+
destination_dir: pr-${{ github.event.pull_request.number }}
74+
keep_files: true
75+
76+
- name: Generate preview URL
77+
id: preview-url
78+
run: |
79+
REPO_NAME="${{ github.repository }}"
80+
PREVIEW_URL="https://${{ github.repository_owner }}.github.io/${REPO_NAME#*/}/pr-${{ github.event.pull_request.number }}/main.html"
81+
echo "url=${PREVIEW_URL}" >> $GITHUB_OUTPUT
82+
echo "Preview URL: ${PREVIEW_URL}"
83+
84+
- name: Find existing comment
85+
uses: peter-evans/find-comment@v3
86+
id: find-comment
87+
with:
88+
issue-number: ${{ github.event.pull_request.number }}
89+
comment-author: 'github-actions[bot]'
90+
body-includes: 'Preview Build'
91+
92+
- name: Create or update PR comment
93+
uses: peter-evans/create-or-update-comment@v4
94+
with:
95+
comment-id: ${{ steps.find-comment.outputs.comment-id }}
96+
issue-number: ${{ github.event.pull_request.number }}
97+
body: |
98+
## Preview Build
99+
100+
A preview of this PR has been built and deployed.
101+
102+
**Preview URL:** [${{ steps.preview-url.outputs.url }}](${{ steps.preview-url.outputs.url }})
103+
104+
This preview will be updated automatically when new commits are pushed to this PR.
105+
106+
---
107+
*This comment is automatically generated by the PR Preview workflow.*
108+

0 commit comments

Comments
 (0)