Skip to content

Commit bedbec3

Browse files
Add PR documentation preview deployments via GitHub Pages
Co-authored-by: MLopez-Ibanez <2620021+MLopez-Ibanez@users.noreply.github.com>
1 parent b6d6b6a commit bedbec3

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

.github/workflows/R.yml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ jobs:
190190
needs: R-CMD-check
191191
permissions:
192192
contents: write # github-pages-deploy-action
193+
pull-requests: write # post PR preview comment
193194
concurrency: # Recommended if you intend to make multiple deployments in quick succession.
194-
group: web-${{ github.workflow }}-${{ github.ref }}
195+
group: web-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
195196
runs-on: ${{ matrix.os }}
196197
strategy:
197198
fail-fast: false
@@ -233,6 +234,46 @@ jobs:
233234
single-commit: true
234235
clean: true
235236

237+
- name: Deploy PR preview 🚀
238+
if: success() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
239+
uses: JamesIves/github-pages-deploy-action@v4
240+
with:
241+
folder: r/docs
242+
target-folder: previews/pr-${{ github.event.pull_request.number }}/r
243+
single-commit: true
244+
clean: true
245+
246+
- name: Comment PR preview link
247+
if: success() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
248+
uses: actions/github-script@v9
249+
with:
250+
script: |
251+
const marker = '<!-- docs-preview-r-marker -->';
252+
const prNumber = context.issue.number;
253+
const url = `https://multi-objective.github.io/moocore/previews/pr-${prNumber}/r/`;
254+
const body = `${marker}\n📚 [**R docs preview** for this PR](${url})\n\n`;
255+
const comments = await github.rest.issues.listComments({
256+
owner: context.repo.owner,
257+
repo: context.repo.repo,
258+
issue_number: prNumber,
259+
});
260+
const existing = comments.data.find(c => c.body.includes(marker));
261+
if (existing) {
262+
await github.rest.issues.updateComment({
263+
owner: context.repo.owner,
264+
repo: context.repo.repo,
265+
comment_id: existing.id,
266+
body,
267+
});
268+
} else {
269+
await github.rest.issues.createComment({
270+
owner: context.repo.owner,
271+
repo: context.repo.repo,
272+
issue_number: prNumber,
273+
body,
274+
});
275+
}
276+
236277
rchk:
237278
needs: pkgdown
238279
uses: multi-objective/moocore/.github/workflows/rchk.yml@main
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Cleanup docs preview
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
cleanup:
12+
name: Remove docs preview for closed PR
13+
runs-on: ubuntu-latest
14+
# Only run for same-repo PRs; fork PRs never get a preview deployed
15+
if: github.event.pull_request.head.repo.full_name == github.repository
16+
steps:
17+
- uses: actions/checkout@v6
18+
with:
19+
ref: gh-pages
20+
21+
- name: Remove preview folder
22+
run: |
23+
PR_NUMBER="${{ github.event.pull_request.number }}"
24+
PREVIEW_DIR="previews/pr-${PR_NUMBER}"
25+
if [ -d "${PREVIEW_DIR}" ]; then
26+
git rm -rf "${PREVIEW_DIR}"
27+
git config user.email "actions@github.com"
28+
git config user.name "GitHub Actions"
29+
git commit -m "Remove docs preview for PR #${PR_NUMBER}"
30+
git push
31+
echo "Removed ${PREVIEW_DIR}"
32+
else
33+
echo "No preview directory found for PR #${PR_NUMBER}, nothing to clean up"
34+
fi

.github/workflows/python.yml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ jobs:
117117
if: "! contains(github.event.head_commit.message, '[skip ci]')"
118118
permissions:
119119
contents: write
120+
pull-requests: write # post PR preview comment
120121
concurrency: # Recommended if you intend to make multiple deployments in quick succession.
121-
group: web-${{ github.workflow }}-${{ github.ref }}
122+
group: web-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
122123
timeout-minutes: 10
123124
runs-on: ubuntu-latest
124125
steps:
@@ -165,6 +166,46 @@ jobs:
165166
single-commit: true
166167
clean: true
167168

169+
- name: Deploy PR preview 🚀
170+
if: success() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
171+
uses: JamesIves/github-pages-deploy-action@v4
172+
with:
173+
folder: python/doc/_build/html/
174+
target-folder: previews/pr-${{ github.event.pull_request.number }}/python
175+
single-commit: true
176+
clean: true
177+
178+
- name: Comment PR preview link
179+
if: success() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
180+
uses: actions/github-script@v9
181+
with:
182+
script: |
183+
const marker = '<!-- docs-preview-python-marker -->';
184+
const prNumber = context.issue.number;
185+
const url = `https://multi-objective.github.io/moocore/previews/pr-${prNumber}/python/`;
186+
const body = `${marker}\n📚 [**Python docs preview** for this PR](${url})\n\n`;
187+
const comments = await github.rest.issues.listComments({
188+
owner: context.repo.owner,
189+
repo: context.repo.repo,
190+
issue_number: prNumber,
191+
});
192+
const existing = comments.data.find(c => c.body.includes(marker));
193+
if (existing) {
194+
await github.rest.issues.updateComment({
195+
owner: context.repo.owner,
196+
repo: context.repo.repo,
197+
comment_id: existing.id,
198+
body,
199+
});
200+
} else {
201+
await github.rest.issues.createComment({
202+
owner: context.repo.owner,
203+
repo: context.repo.repo,
204+
issue_number: prNumber,
205+
body,
206+
});
207+
}
208+
168209
coverage:
169210
timeout-minutes: 15
170211
needs: build-doc

0 commit comments

Comments
 (0)