Skip to content

Commit 9fdbfe2

Browse files
fix
Signed-off-by: Rajesh-Nagarajan-11 <[email protected]>
1 parent 095acae commit 9fdbfe2

File tree

2 files changed

+153
-49
lines changed

2 files changed

+153
-49
lines changed
Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
name: Build and Preview Site
1+
name: Build Preview Site
22
on:
33
pull_request:
44
branches: [master]
55
types: [opened, synchronize, reopened, closed]
66

77
concurrency:
8-
group: ${{ github.workflow }}-${{ github.ref }}
8+
group: pr-preview-build-${{ github.event.pull_request.number }}
99
cancel-in-progress: true
10-
10+
11+
# Only read permissions for building (safe for forks)
1112
permissions:
12-
contents: write
13-
pull-requests: write
13+
contents: read
1414

1515
jobs:
1616
build:
@@ -37,52 +37,23 @@ jobs:
3737
bash .github/workflows/script.sh
3838
3939
- name: Upload files
40-
uses: actions/upload-artifact@master
40+
uses: actions/upload-artifact@v4
4141
with:
42-
name: public-dir
42+
name: public-dir-pr-${{ github.event.pull_request.number }}
4343
path: public-dir.zip
4444
retention-days: 1
45-
46-
preview:
47-
needs: build
48-
runs-on: ubuntu-latest
49-
if: github.event_name == 'pull_request'
50-
permissions:
51-
contents: write
52-
pull-requests: write
53-
steps:
54-
- name: Checkout 🛎️
55-
uses: actions/checkout@v6
56-
57-
- name: Download pre-built site
58-
if: github.event_name == 'pull_request'
59-
uses: actions/download-artifact@v4
60-
with:
61-
name: public-dir
62-
path: .
63-
64-
- name: Extract site
65-
if: github.event_name == 'pull_request'
45+
46+
# Save PR metadata for the deploy workflow
47+
- name: Save PR metadata
6648
run: |
67-
unzip -q public-dir.zip
68-
# Ensure the extracted folder has the static files in a 'public' dir for the action
69-
if [ -d "public-dir" ] && [ ! -d "public" ]; then
70-
mv public-dir public
71-
elif [ ! -d "public" ]; then
72-
echo "Warning: Neither public nor public-dir found after extraction"
73-
fi
74-
75-
- name: Deploy Preview
76-
if: github.event_name == 'pull_request' && github.event.action != 'closed'
77-
uses: rossjrw/[email protected]
78-
with:
79-
source-dir: public
80-
token: ${{ secrets.GH_ACCESS_TOKEN }}
81-
82-
- name: Remove Preview on Close
83-
if: github.event_name == 'pull_request' && github.event.action == 'closed'
84-
uses: rossjrw/[email protected]
49+
mkdir -p ./pr-metadata
50+
echo "${{ github.event.pull_request.number }}" > ./pr-metadata/pr_number
51+
echo "${{ github.event.action }}" > ./pr-metadata/pr_action
52+
echo "${{ github.event.pull_request.head.sha }}" > ./pr-metadata/pr_sha
53+
54+
- name: Upload PR metadata
55+
uses: actions/upload-artifact@v4
8556
with:
86-
source-dir: public
87-
remove: true
88-
token: ${{ secrets.GH_ACCESS_TOKEN }}
57+
name: pr-metadata-${{ github.event.pull_request.number }}
58+
path: pr-metadata/
59+
retention-days: 1
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
name: Deploy Preview Site
2+
on:
3+
workflow_run:
4+
workflows: ["Build Preview Site"]
5+
types:
6+
- completed
7+
8+
# This workflow runs with write permissions from the base repository
9+
permissions:
10+
contents: write
11+
pull-requests: write
12+
13+
jobs:
14+
deploy:
15+
runs-on: ubuntu-latest
16+
# Only run if the build was successful
17+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
18+
steps:
19+
- name: Checkout 🛎️
20+
uses: actions/checkout@v6
21+
22+
- name: Download artifacts
23+
uses: actions/download-artifact@v4
24+
with:
25+
run-id: ${{ github.event.workflow_run.id }}
26+
github-token: ${{ secrets.GITHUB_TOKEN }}
27+
pattern: pr-*
28+
merge-multiple: false
29+
30+
- name: Read PR metadata
31+
id: pr-metadata
32+
run: |
33+
# Find the metadata directory
34+
metadata_dir=$(find . -type d -name "pr-metadata-*" | head -n 1)
35+
36+
if [ -z "$metadata_dir" ]; then
37+
echo "Error: Could not find PR metadata"
38+
exit 1
39+
fi
40+
41+
pr_number=$(cat "$metadata_dir/pr_number")
42+
pr_action=$(cat "$metadata_dir/pr_action")
43+
pr_sha=$(cat "$metadata_dir/pr_sha")
44+
45+
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
46+
echo "pr_action=$pr_action" >> $GITHUB_OUTPUT
47+
echo "pr_sha=$pr_sha" >> $GITHUB_OUTPUT
48+
49+
echo "PR Number: $pr_number"
50+
echo "PR Action: $pr_action"
51+
echo "PR SHA: $pr_sha"
52+
53+
- name: Extract site
54+
run: |
55+
# Find and unzip the public directory
56+
public_zip=$(find . -type f -name "public-dir-pr-*.zip" | head -n 1)
57+
58+
if [ -z "$public_zip" ]; then
59+
echo "Error: Could not find public directory zip"
60+
exit 1
61+
fi
62+
63+
unzip -q "$public_zip"
64+
65+
# Ensure the extracted folder has the static files in a 'public' dir for the action
66+
if [ -d "public-dir" ] && [ ! -d "public" ]; then
67+
mv public-dir public
68+
elif [ ! -d "public" ]; then
69+
echo "Error: Neither public nor public-dir found after extraction"
70+
exit 1
71+
fi
72+
73+
- name: Deploy Preview
74+
if: steps.pr-metadata.outputs.pr_action != 'closed'
75+
uses: rossjrw/[email protected]
76+
with:
77+
source-dir: public
78+
preview-branch: gh-pages
79+
umbrella-dir: pr-preview
80+
custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }}
81+
token: ${{ secrets.GH_ACCESS_TOKEN }}
82+
83+
- name: Remove Preview on Close
84+
if: steps.pr-metadata.outputs.pr_action == 'closed'
85+
uses: rossjrw/[email protected]
86+
with:
87+
source-dir: public
88+
preview-branch: gh-pages
89+
umbrella-dir: pr-preview
90+
custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }}
91+
action: remove
92+
token: ${{ secrets.GH_ACCESS_TOKEN }}
93+
94+
- name: Comment on PR
95+
if: steps.pr-metadata.outputs.pr_action != 'closed'
96+
uses: actions/github-script@v7
97+
with:
98+
github-token: ${{ secrets.GITHUB_TOKEN }}
99+
script: |
100+
const prNumber = ${{ steps.pr-metadata.outputs.pr_number }};
101+
const previewUrl = `https://${context.repo.owner}.github.io/${context.repo.repo}/pr-preview/pr-${prNumber}/`;
102+
103+
// Find existing preview comment
104+
const comments = await github.rest.issues.listComments({
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
issue_number: prNumber,
108+
});
109+
110+
const botComment = comments.data.find(comment =>
111+
comment.user.type === 'Bot' &&
112+
comment.body.includes('Preview deployed')
113+
);
114+
115+
const commentBody = `🚀 **Preview deployed!**\n\nYou can view the preview at: ${previewUrl}\n\n_This preview will be updated on each push and removed when the PR is closed._`;
116+
117+
if (botComment) {
118+
// Update existing comment
119+
await github.rest.issues.updateComment({
120+
owner: context.repo.owner,
121+
repo: context.repo.repo,
122+
comment_id: botComment.id,
123+
body: commentBody
124+
});
125+
} else {
126+
// Create new comment
127+
await github.rest.issues.createComment({
128+
owner: context.repo.owner,
129+
repo: context.repo.repo,
130+
issue_number: prNumber,
131+
body: commentBody
132+
});
133+
}

0 commit comments

Comments
 (0)