Skip to content

Commit 9643606

Browse files
Merge pull request #49 from Rajesh-Nagarajan-11/build-preview-fix
clean up CI actions
2 parents 425e16c + 7b30c91 commit 9643606

File tree

5 files changed

+201
-139
lines changed

5 files changed

+201
-139
lines changed

.github/actions/comment-preview-on-pr/Dockerfile

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/actions/comment-preview-on-pr/action.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.github/actions/comment-preview-on-pr/app/main.py

Lines changed: 0 additions & 70 deletions
This file was deleted.
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.GITHUB_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.GITHUB_TOKEN }}
57+
name: pr-metadata-${{ github.event.pull_request.number }}
58+
path: pr-metadata/
59+
retention-days: 1
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
name: Deploy Preview Site
2+
on:
3+
workflow_run:
4+
workflows: ["Build Preview Site"]
5+
types:
6+
- completed
7+
branches:
8+
- master
9+
10+
# This workflow runs with write permissions from the base repository
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
actions: read
15+
16+
jobs:
17+
deploy:
18+
runs-on: ubuntu-latest
19+
# Only run if the build was successful and originated from a PR
20+
if: |
21+
github.event.workflow_run.conclusion == 'success' &&
22+
github.event.workflow_run.event == 'pull_request'
23+
steps:
24+
- name: Checkout 🛎️
25+
uses: actions/checkout@v6
26+
27+
- name: Download artifacts
28+
uses: actions/github-script@v7
29+
with:
30+
script: |
31+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
32+
owner: context.repo.owner,
33+
repo: context.repo.repo,
34+
run_id: context.payload.workflow_run.id,
35+
});
36+
37+
let matchArtifacts = allArtifacts.data.artifacts.filter((artifact) => {
38+
return artifact.name.match(/^pr-(metadata|public-dir)-/);
39+
});
40+
41+
if (matchArtifacts.length === 0) {
42+
core.setFailed('No artifacts found');
43+
return;
44+
}
45+
46+
let fs = require('fs');
47+
for (const artifact of matchArtifacts) {
48+
let download = await github.rest.actions.downloadArtifact({
49+
owner: context.repo.owner,
50+
repo: context.repo.repo,
51+
artifact_id: artifact.id,
52+
archive_format: 'zip',
53+
});
54+
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${artifact.name}.zip`, Buffer.from(download.data));
55+
}
56+
57+
- name: Extract artifacts
58+
run: |
59+
for zip in *.zip; do
60+
if [ -f "$zip" ]; then
61+
mkdir -p "${zip%.zip}"
62+
unzip -q "$zip" -d "${zip%.zip}"
63+
fi
64+
done
65+
66+
- name: Read PR metadata
67+
id: pr-metadata
68+
run: |
69+
# Find the metadata directory
70+
metadata_dir=$(find . -type d -name "pr-metadata-*" | head -n 1)
71+
72+
if [ -z "$metadata_dir" ]; then
73+
echo "Error: Could not find PR metadata"
74+
ls -la
75+
exit 1
76+
fi
77+
78+
pr_number=$(cat "$metadata_dir/pr_number")
79+
pr_action=$(cat "$metadata_dir/pr_action")
80+
pr_sha=$(cat "$metadata_dir/pr_sha")
81+
82+
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
83+
echo "pr_action=$pr_action" >> $GITHUB_OUTPUT
84+
echo "pr_sha=$pr_sha" >> $GITHUB_OUTPUT
85+
86+
echo "PR Number: $pr_number"
87+
echo "PR Action: $pr_action"
88+
echo "PR SHA: $pr_sha"
89+
90+
- name: Extract site
91+
run: |
92+
# Find and extract the public directory zip
93+
public_artifact=$(find . -type d -name "public-dir-pr-*" | head -n 1)
94+
95+
if [ -z "$public_artifact" ]; then
96+
echo "Error: Could not find public directory artifact"
97+
ls -la
98+
exit 1
99+
fi
100+
101+
# Find the zip file in the artifact directory
102+
public_zip=$(find "$public_artifact" -type f -name "*.zip" | head -n 1)
103+
104+
if [ -z "$public_zip" ]; then
105+
echo "Error: Could not find public directory zip in artifact"
106+
ls -la "$public_artifact"
107+
exit 1
108+
fi
109+
110+
unzip -q "$public_zip"
111+
112+
# Ensure the extracted folder has the static files in a 'public' dir for the action
113+
if [ -d "public-dir" ] && [ ! -d "public" ]; then
114+
mv public-dir public
115+
elif [ ! -d "public" ]; then
116+
echo "Error: Neither public nor public-dir found after extraction"
117+
ls -la
118+
exit 1
119+
fi
120+
121+
- name: Deploy Preview
122+
if: steps.pr-metadata.outputs.pr_action != 'closed'
123+
uses: rossjrw/[email protected]
124+
with:
125+
source-dir: public
126+
preview-branch: gh-pages
127+
umbrella-dir: pr-preview
128+
custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }}
129+
token: ${{ secrets.GH_ACCESS_TOKEN }}
130+
131+
- name: Remove Preview on Close
132+
if: steps.pr-metadata.outputs.pr_action == 'closed'
133+
uses: rossjrw/[email protected]
134+
with:
135+
source-dir: public
136+
preview-branch: gh-pages
137+
umbrella-dir: pr-preview
138+
custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }}
139+
action: remove
140+
token: ${{ secrets.GH_ACCESS_TOKEN }}
141+
142+
- name: Comment on PR
143+
if: steps.pr-metadata.outputs.pr_action != 'closed'
144+
uses: actions/github-script@v7
145+
with:
146+
github-token: ${{ secrets.GITHUB_TOKEN }}
147+
script: |
148+
const prNumber = ${{ steps.pr-metadata.outputs.pr_number }};
149+
const previewUrl = `https://${context.repo.owner}.github.io/${context.repo.repo}/pr-preview/pr-${prNumber}/`;
150+
151+
// Find existing preview comment
152+
const comments = await github.rest.issues.listComments({
153+
owner: context.repo.owner,
154+
repo: context.repo.repo,
155+
issue_number: prNumber,
156+
});
157+
158+
const botComment = comments.data.find(comment =>
159+
comment.user.type === 'Bot' &&
160+
comment.body.includes('Preview deployed')
161+
);
162+
163+
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._`;
164+
165+
if (botComment) {
166+
// Update existing comment
167+
await github.rest.issues.updateComment({
168+
owner: context.repo.owner,
169+
repo: context.repo.repo,
170+
comment_id: botComment.id,
171+
body: commentBody
172+
});
173+
} else {
174+
// Create new comment
175+
await github.rest.issues.createComment({
176+
owner: context.repo.owner,
177+
repo: context.repo.repo,
178+
issue_number: prNumber,
179+
body: commentBody
180+
});
181+
}

0 commit comments

Comments
 (0)