Skip to content

Commit cc4197e

Browse files
committed
add a two step process
Signed-off-by: Aritra Dey <[email protected]>
1 parent 492ce4e commit cc4197e

File tree

2 files changed

+77
-38
lines changed

2 files changed

+77
-38
lines changed

.github/workflows/publish-docs.yml

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,106 @@ on:
44
push:
55
branches: [master]
66
pull_request:
7-
types:
8-
- opened
9-
- reopened
10-
- synchronize
11-
- closed
7+
types: [opened, reopened, synchronize, closed]
8+
workflow_dispatch:
9+
inputs:
10+
pr_number:
11+
description: 'PR Number'
12+
required: true
13+
run_id:
14+
description: 'Run ID of the build artifact'
15+
required: true
1216

1317
permissions:
1418
contents: write
19+
pull-requests: write
20+
actions: read
1521

16-
concurrency: preview-${{ github.ref }}
17-
18-
env:
19-
PREVIEW_BRANCH: gh-pages
22+
concurrency: ci-${{ github.ref }}
2023

2124
jobs:
22-
deploy-preview:
23-
runs-on: ubuntu-latest
25+
# 1. Build Preview Artifact (Runs on PR)
26+
build-preview:
2427
if: github.event_name == 'pull_request'
28+
runs-on: ubuntu-latest
2529
steps:
26-
- uses: actions/checkout@v4
27-
- uses: actions/setup-node@v4
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
33+
- name: Setup Node.js
34+
uses: actions/setup-node@v4
2835
with:
2936
node-version: 20
3037

3138
- name: Calculate BASE_URL
32-
run: echo "BASE_URL=/pr-preview/pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV
39+
run: |
40+
echo "BASE_URL=/pr-preview/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_ENV
3341
34-
- run: npm ci && npm run build
42+
- name: Install and Build
43+
run: npm ci && npm run build
3544
env:
3645
BASE_URL: ${{ env.BASE_URL }}
3746

38-
- uses: rossjrw/pr-preview-action@v1
39-
id: preview-step
47+
- name: Upload Preview Artifact
48+
uses: actions/upload-artifact@v4
4049
with:
41-
source-dir: ./build/
42-
preview-branch: ${{ env.PREVIEW_BRANCH }}
43-
comment: false
50+
name: preview-build
51+
path: build/
4452

45-
- uses: marocchino/sticky-pull-request-comment@v2
46-
if: steps.preview-step.outputs.deployment-action == 'deploy' && env.deployment_status == 'success'
53+
- name: Comment Build Info
54+
uses: marocchino/sticky-pull-request-comment@v2
4755
with:
48-
header: pr-preview
56+
header: pr-build-info
4957
message: |
50-
[PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
51-
:---:
52-
| <p></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }} <br><br>
53-
| <h6>Built to branch [`${{ env.PREVIEW_BRANCH }}`](${{ github.server_url }}/${{ github.repository }}/tree/${{ env.PREVIEW_BRANCH }}) at ${{ steps.preview-step.outputs.action-start-time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ github.repository }}/deployments) is complete. <br><br> </h6>
58+
### ✅ Build Successful
59+
To deploy this preview, a maintainer must run the **Deploy Preview** workflow.
60+
61+
**Run ID**: `${{ github.run_id }}`
62+
**PR Number**: `${{ github.event.pull_request.number }}`
63+
64+
# 2. Deploy Preview (Manual Trigger)
65+
deploy-preview:
66+
if: github.event_name == 'workflow_dispatch'
67+
runs-on: ubuntu-latest
68+
steps:
69+
- name: Checkout
70+
uses: actions/checkout@v4
5471

55-
- uses: marocchino/sticky-pull-request-comment@v2
56-
if: steps.preview-step.outputs.deployment-action == 'remove' && env.deployment_status == 'success'
72+
- name: Download Artifact
73+
uses: actions/download-artifact@v4
5774
with:
58-
header: pr-preview
59-
message: |
60-
[PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
61-
:---:
62-
Preview removed because the pull request was closed.
63-
${{ steps.preview-step.outputs.action-start-time }}
75+
name: preview-build
76+
run-id: ${{ inputs.run_id }}
77+
github-token: ${{ secrets.GITHUB_TOKEN }}
78+
path: site
79+
80+
- name: Deploy to GitHub Pages
81+
uses: JamesIves/github-pages-deploy-action@v4
82+
with:
83+
branch: gh-pages
84+
folder: site
85+
target-folder: pr-preview/pr-${{ inputs.pr_number }}
6486

87+
- name: Comment Preview Link
88+
uses: actions/github-script@v6
89+
with:
90+
script: |
91+
const prNumber = ${{ inputs.pr_number }};
92+
const owner = context.repo.owner;
93+
const repo = context.repo.repo;
94+
const url = `https://${owner}.github.io/${repo}/pr-preview/pr-${prNumber}/`;
95+
96+
await github.rest.issues.createComment({
97+
owner,
98+
repo,
99+
issue_number: prNumber,
100+
body: `🚀 **Preview Deployed!**\n\n[View Preview](${url})`
101+
});
102+
103+
# 3. Production Build & Deploy (Runs on Master Push)
65104
build-production:
66-
runs-on: ubuntu-latest
67105
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
106+
runs-on: ubuntu-latest
68107
steps:
69108
- uses: actions/checkout@v4
70109
- uses: actions/setup-node@v4
@@ -75,9 +114,9 @@ jobs:
75114
with: { name: site, path: build/ }
76115

77116
deploy-production:
78-
runs-on: ubuntu-latest
79117
needs: build-production
80118
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
119+
runs-on: ubuntu-latest
81120
steps:
82121
- uses: actions/download-artifact@v4
83122
with: { name: site, path: site }

docusaurus.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
tagline:
55
"Ecosystem science, policy, and management informed by the best available data and models",
66
url: "https://pecanproject.github.io",
7-
baseUrl: (process.env.GITHUB_ACTIONS && process.env.BASE_URL) ? `${process.env.BASE_URL}/` : "/",
7+
baseUrl: process.env.BASE_URL || "/",
88
onBrokenLinks: "ignore",
99
onBrokenMarkdownLinks: "warn",
1010
favicon: "img/favicon.ico",

0 commit comments

Comments
 (0)