1+ name : Package Preview Publish
2+
3+ on :
4+ pull_request :
5+ types : [opened, synchronize, reopened]
6+
7+ jobs :
8+ publish-preview :
9+ runs-on : ubuntu-latest
10+
11+ steps :
12+ - name : Checkout code
13+ uses : actions/checkout@v4
14+
15+ - name : Enable corepack
16+ run : corepack enable
17+
18+ - name : Setup Node.js
19+ uses : actions/setup-node@v4
20+ with :
21+ node-version : 20
22+ cache : ' npm'
23+
24+ - name : Install dependencies
25+ run : npm install
26+
27+ - name : Build package
28+ run : npm run build
29+
30+ - name : Publish preview with pkg.pr.new
31+ run : npx pkg-pr-new publish --json output.json --comment=off
32+
33+ - name : Comment PR with install instructions
34+ uses : actions/github-script@v6
35+ with :
36+ github-token : ${{ secrets.GITHUB_TOKEN }}
37+ script : |
38+ const fs = require('fs');
39+ const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
40+ if (!output.packages || output.packages.length === 0) {
41+ core.setFailed('No packages published by pkg.pr.new');
42+ return;
43+ }
44+ const pkg = output.packages[0];
45+ const installCmd = `npm i ${pkg.url}`;
46+ const badge = `[](https://pkg.pr.new/~/${context.repo.owner}/${context.repo.repo})`;
47+ const body = `### 🚀 Package Preview Available!
48+
49+ ${badge}
50+
51+ ---
52+
53+ **Install this PR's preview build with npm:**
54+
55+ \`\`\`sh
56+ ${installCmd}
57+ \`\`\`
58+
59+ - 📦 [Preview Package on pkg.pr.new](${pkg.url})
60+ - 🔗 [View this commit on GitHub](https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${pkg.commit})
61+
62+ ---
63+ <sub>Preview powered by [pkg.pr.new](https://pkg.pr.new) — try new features instantly, no NPM release needed!</sub>
64+ ` ;
65+
66+ const botCommentIdentifier = '### 🚀 Package Preview Available!';
67+
68+ async function findBotComment(issueNumber) {
69+ if (!issueNumber) return null;
70+ const comments = await github.rest.issues.listComments({
71+ owner: context.repo.owner,
72+ repo: context.repo.repo,
73+ issue_number: issueNumber,
74+ });
75+ return comments.data.find((comment) =>
76+ comment.body.includes(botCommentIdentifier)
77+ );
78+ }
79+
80+ async function createOrUpdateComment(issueNumber) {
81+ if (!issueNumber) {
82+ console.log('No issue number provided. Cannot post or update comment.');
83+ return;
84+ }
85+
86+ const existingComment = await findBotComment(issueNumber);
87+ if (existingComment) {
88+ await github.rest.issues.updateComment({
89+ owner: context.repo.owner,
90+ repo: context.repo.repo,
91+ comment_id: existingComment.id,
92+ body: body,
93+ });
94+ } else {
95+ await github.rest.issues.createComment({
96+ issue_number: issueNumber,
97+ owner: context.repo.owner,
98+ repo: context.repo.repo,
99+ body: body,
100+ });
101+ }
102+ }
103+
104+ if (context.eventName === 'pull_request') {
105+ if (context.issue.number) {
106+ await createOrUpdateComment(context.issue.number);
107+ }
108+ }
0 commit comments