Skip to content

Commit af10082

Browse files
snomiaoclaude
andauthored
[ci] Improve Storybook deployment and comment workflow (#5426)
## Summary - Improves Storybook deployment and PR comment workflow similar to the Playwright improvements in #5425 - Creates unified deployment and commenting system for better maintainability - Adds Cloudflare Pages deployment for Storybook previews ## Deployment Cases Matrix | Case | PR Type | Branch | Deployment | Features | |------|---------|--------|------------|----------| | **1** | Non-forked PR | `version-bump-*` | ✅ Chromatic | • Visual diff testing<br>• Chromatic build URL<br>• Chromatic Storybook URL<br>• Shows visual changes | | **2** | Non-forked PR | All branches | ✅ Cloudflare Pages | • Live Storybook preview<br>• pages.dev URL<br>• No visual diff | | **3** | Forked PR | Any branch | ✅ Cloudflare Pages | • Live Storybook preview<br>• pages.dev URL<br>• No visual diff<br>• Runs via separate workflow to avoid permission problems | ### Key Points: - **Chromatic** (paid service): Only for `version-bump-*` branches to track visual changes between releases - **Cloudflare Pages** (free): For all other PRs to provide Storybook preview without visual diff - **Security**: Forked PRs use a separate workflow with limited permissions ## Changes ### New Features - 🚀 **Cloudflare Pages Deployment**: Storybook builds are now deployed to Cloudflare Pages for easy preview - 🔄 **Unified Script**: Single reusable shell script handles both deployment and PR comments - 🔒 **Better Security**: Separate workflows for fork vs non-fork PRs ### Improvements - ♻️ **Retry Logic**: Automatic retry (3 attempts) for failed deployments - 📝 **Better Comments**: Clearer PR comments with deployment links and status - 🎯 **Simplified Logic**: Workflow logic moved to reusable script for easier maintenance - ⚡ **Better Error Handling**: Proper handling of different workflow conclusions - 🐛 **Fixed Comment Output**: Deployment logs now properly redirected to stderr ### Files Changed - `scripts/cicd/pr-storybook-deploy-and-comment.sh` - New unified deployment script - `.github/workflows/chromatic.yaml` - Updated to use new script and add deployment - `.github/workflows/pr-storybook-deploy.yaml` - New workflow for forked PRs - `.github/workflows/pr-storybook-comment.yaml` - Removed (replaced by new system) ## ⚠️ Required Setup The Cloudflare Pages project `comfyui-storybook` needs to be created under the organization's Cloudflare account: ```bash # Using the account ID from GitHub secrets export CLOUDFLARE_ACCOUNT_ID=5ae914d9b87bcf6bbe1ada5798f92a5f export CLOUDFLARE_API_TOKEN=<org-token> wrangler pages project create comfyui-storybook --production-branch main ``` **Note**: The project must be created under the same Cloudflare account that's configured in the GitHub secrets. ## Test Plan - [x] Create Cloudflare Pages project `comfyui-storybook` - [x] Workflow runs successfully on all PRs - [x] PR comments are posted correctly at start and completion - [x] Storybook deploys to Cloudflare Pages with correct URL - [ ] Fork PRs are handled by separate workflow - [ ] Non-fork PRs get inline deployment - [ ] version-bump-* branches show Chromatic info ## References - Similar improvements for Playwright: #5459 - Based on pattern from sno-fix-playwright-comment-2 branch 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 28a779d commit af10082

File tree

4 files changed

+515
-130
lines changed

4 files changed

+515
-130
lines changed

.github/workflows/chromatic.yaml

Lines changed: 178 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: 'Chromatic'
1+
name: Storybook and Chromatic CI
22

33
# - [Automate Chromatic with GitHub Actions • Chromatic docs]( https://www.chromatic.com/docs/github-actions/ )
44

@@ -8,10 +8,97 @@ on:
88
branches: [main]
99

1010
jobs:
11+
# Post starting comment for non-forked PRs
12+
comment-on-pr-start:
13+
runs-on: ubuntu-latest
14+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false
15+
permissions:
16+
pull-requests: write
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Post starting comment
22+
env:
23+
GITHUB_TOKEN: ${{ github.token }}
24+
run: |
25+
chmod +x scripts/cicd/pr-storybook-deploy-and-comment.sh
26+
./scripts/cicd/pr-storybook-deploy-and-comment.sh \
27+
"${{ github.event.pull_request.number }}" \
28+
"${{ github.head_ref }}" \
29+
"starting" \
30+
"$(date -u '+%m/%d/%Y, %I:%M:%S %p')"
31+
32+
# Build Storybook for all PRs (free Cloudflare deployment)
33+
storybook-build:
34+
runs-on: ubuntu-latest
35+
if: github.event_name == 'pull_request'
36+
outputs:
37+
conclusion: ${{ steps.job-status.outputs.conclusion }}
38+
workflow-url: ${{ steps.workflow-url.outputs.url }}
39+
steps:
40+
- name: Checkout code
41+
uses: actions/checkout@v4
42+
43+
- name: Install pnpm
44+
uses: pnpm/action-setup@v4
45+
with:
46+
version: 10
47+
48+
- name: Setup Node.js
49+
uses: actions/setup-node@v4
50+
with:
51+
node-version: '20'
52+
cache: 'pnpm'
53+
54+
- name: Cache tool outputs
55+
uses: actions/cache@v4
56+
with:
57+
path: |
58+
.cache
59+
storybook-static
60+
tsconfig.tsbuildinfo
61+
key: storybook-cache-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles('src/**/*.{ts,vue,js}', '*.config.*', '.storybook/**/*') }}
62+
restore-keys: |
63+
storybook-cache-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}-
64+
storybook-cache-${{ runner.os }}-
65+
storybook-tools-cache-${{ runner.os }}-
66+
67+
- name: Install dependencies
68+
run: pnpm install --frozen-lockfile
69+
70+
- name: Build Storybook
71+
run: pnpm build-storybook
72+
73+
- name: Set job status
74+
id: job-status
75+
if: always()
76+
run: |
77+
echo "conclusion=${{ job.status }}" >> $GITHUB_OUTPUT
78+
79+
- name: Get workflow URL
80+
id: workflow-url
81+
if: always()
82+
run: |
83+
echo "url=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> $GITHUB_OUTPUT
84+
85+
- name: Upload Storybook build
86+
if: success() && github.event.pull_request.head.repo.fork == false
87+
uses: actions/upload-artifact@v4
88+
with:
89+
name: storybook-static
90+
path: storybook-static/
91+
retention-days: 7
92+
93+
# Chromatic deployment only for version-bump-* branches or manual triggers
1194
chromatic-deployment:
1295
runs-on: ubuntu-latest
13-
# Only run for PRs from version-bump-* branches or manual triggers
14-
if: github.event_name == 'workflow_dispatch' || startsWith(github.head_ref, 'version-bump-')
96+
if: github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && startsWith(github.head_ref, 'version-bump-'))
97+
outputs:
98+
conclusion: ${{ steps.job-status.outputs.conclusion }}
99+
workflow-url: ${{ steps.workflow-url.outputs.url }}
100+
chromatic-build-url: ${{ steps.chromatic.outputs.buildUrl }}
101+
chromatic-storybook-url: ${{ steps.chromatic.outputs.storybookUrl }}
15102
steps:
16103
- name: Checkout code
17104
uses: actions/checkout@v4
@@ -29,7 +116,6 @@ jobs:
29116
node-version: '20'
30117
cache: 'pnpm'
31118

32-
33119
- name: Cache tool outputs
34120
uses: actions/cache@v4
35121
with:
@@ -54,4 +140,92 @@ jobs:
54140
buildScriptName: build-storybook
55141
autoAcceptChanges: 'main' # Auto-accept changes on main branch
56142
exitOnceUploaded: true # Don't wait for UI tests to complete
143+
onlyChanged: true # Only capture changed stories
144+
145+
- name: Set job status
146+
id: job-status
147+
if: always()
148+
run: |
149+
echo "conclusion=${{ job.status }}" >> $GITHUB_OUTPUT
57150
151+
- name: Get workflow URL
152+
id: workflow-url
153+
if: always()
154+
run: |
155+
echo "url=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> $GITHUB_OUTPUT
156+
157+
# Deploy and comment for non-forked PRs only
158+
deploy-and-comment:
159+
needs: [storybook-build]
160+
runs-on: ubuntu-latest
161+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false && always()
162+
permissions:
163+
pull-requests: write
164+
contents: read
165+
steps:
166+
- name: Checkout repository
167+
uses: actions/checkout@v4
168+
169+
- name: Download Storybook build
170+
if: needs.storybook-build.outputs.conclusion == 'success'
171+
uses: actions/download-artifact@v4
172+
with:
173+
name: storybook-static
174+
path: storybook-static
175+
176+
- name: Make deployment script executable
177+
run: chmod +x scripts/cicd/pr-storybook-deploy-and-comment.sh
178+
179+
- name: Deploy Storybook and comment on PR
180+
env:
181+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
182+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
183+
GITHUB_TOKEN: ${{ github.token }}
184+
WORKFLOW_CONCLUSION: ${{ needs.storybook-build.outputs.conclusion }}
185+
WORKFLOW_URL: ${{ needs.storybook-build.outputs.workflow-url }}
186+
run: |
187+
./scripts/cicd/pr-storybook-deploy-and-comment.sh \
188+
"${{ github.event.pull_request.number }}" \
189+
"${{ github.head_ref }}" \
190+
"completed"
191+
192+
# Update comment with Chromatic URLs for version-bump branches
193+
update-comment-with-chromatic:
194+
needs: [chromatic-deployment, deploy-and-comment]
195+
runs-on: ubuntu-latest
196+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false && startsWith(github.head_ref, 'version-bump-') && needs.chromatic-deployment.outputs.chromatic-build-url != ''
197+
permissions:
198+
pull-requests: write
199+
steps:
200+
- name: Update comment with Chromatic URLs
201+
uses: actions/github-script@v7
202+
with:
203+
script: |
204+
const buildUrl = '${{ needs.chromatic-deployment.outputs.chromatic-build-url }}';
205+
const storybookUrl = '${{ needs.chromatic-deployment.outputs.chromatic-storybook-url }}';
206+
207+
// Find the existing Storybook comment
208+
const { data: comments } = await github.rest.issues.listComments({
209+
owner: context.repo.owner,
210+
repo: context.repo.repo,
211+
issue_number: ${{ github.event.pull_request.number }}
212+
});
213+
214+
const storybookComment = comments.find(comment =>
215+
comment.body.includes('<!-- STORYBOOK_BUILD_STATUS -->')
216+
);
217+
218+
if (storybookComment && buildUrl && storybookUrl) {
219+
// Append Chromatic info to existing comment
220+
const updatedBody = storybookComment.body.replace(
221+
/---\n(.*)$/s,
222+
`---\n### 🎨 Chromatic Visual Tests\n- 📊 [View Chromatic Build](${buildUrl})\n- 📚 [View Chromatic Storybook](${storybookUrl})\n\n$1`
223+
);
224+
225+
await github.rest.issues.updateComment({
226+
owner: context.repo.owner,
227+
repo: context.repo.repo,
228+
comment_id: storybookComment.id,
229+
body: updatedBody
230+
});
231+
}

.github/workflows/pr-storybook-comment.yaml

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

0 commit comments

Comments
 (0)