Skip to content

Commit 2402e53

Browse files
ci(audience): add CDN bundle size gate (SDK-115) (#2855)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent aa051dd commit 2402e53

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
name: Audience Bundle Size
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "**"
7+
# Do not add as a required check — PRs that don't touch these
8+
# paths would be blocked forever (GitHub skips the check entirely
9+
# instead of reporting it as passed).
10+
paths:
11+
- "packages/audience/sdk/**"
12+
- "packages/audience/core/**"
13+
- "packages/internal/metrics/**"
14+
- "pnpm-lock.yaml"
15+
- ".github/workflows/audience-bundle-size.yaml"
16+
17+
permissions:
18+
pull-requests: write
19+
contents: read
20+
21+
concurrency:
22+
group: audience-bundle-size-${{ github.event.pull_request.number || github.ref }}
23+
cancel-in-progress: true
24+
25+
env:
26+
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.TS_IMMUTABLE_SDK_NX_TOKEN }}
27+
28+
jobs:
29+
bundle-size:
30+
name: Audience Bundle Size Check
31+
runs-on: ubuntu-latest-4-cores
32+
steps:
33+
- name: Checkout code
34+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # pin@v6.0.1
35+
with:
36+
ref: ${{ github.event.pull_request.head.sha }}
37+
# Full history needed — we check out the base commit later to measure its size.
38+
fetch-depth: 0
39+
40+
- name: Setup
41+
uses: ./.github/actions/setup
42+
43+
- name: Read budget config
44+
id: budget
45+
run: |
46+
BUDGET_DIR="packages/audience/sdk"
47+
BUDGET_FILE="${BUDGET_DIR}/bundlebudget.json"
48+
MAX_GZIP=$(jq -e '.budgets[0].maxSizeGzip | numbers' "$BUDGET_FILE") \
49+
|| { echo "::error file=${BUDGET_FILE}::.budgets[0].maxSizeGzip must be a number"; exit 1; }
50+
WARN_GZIP=$(jq -e '.budgets[0].warnSizeGzip | numbers' "$BUDGET_FILE") \
51+
|| { echo "::error file=${BUDGET_FILE}::.budgets[0].warnSizeGzip must be a number"; exit 1; }
52+
BUNDLE_REL=$(jq -er '.budgets[0].file | strings' "$BUDGET_FILE") \
53+
|| { echo "::error file=${BUDGET_FILE}::.budgets[0].file must be a string"; exit 1; }
54+
{
55+
echo "max_gzip=$MAX_GZIP"
56+
echo "warn_gzip=$WARN_GZIP"
57+
echo "bundle_path=${BUDGET_DIR}/${BUNDLE_REL}"
58+
} >> "$GITHUB_OUTPUT"
59+
60+
- name: Build audience SDK (PR)
61+
# The `...` suffix also builds audience-core and metrics, which get bundled in.
62+
run: pnpm --filter @imtbl/audience... build
63+
64+
- name: Measure PR bundle size
65+
id: pr_size
66+
env:
67+
BUNDLE: ${{ steps.budget.outputs.bundle_path }}
68+
run: |
69+
RAW_SIZE=$(stat --format=%s "$BUNDLE")
70+
GZIP_SIZE=$(gzip -c "$BUNDLE" | wc -c)
71+
echo "raw=$RAW_SIZE" >> "$GITHUB_OUTPUT"
72+
echo "gzip=$GZIP_SIZE" >> "$GITHUB_OUTPUT"
73+
echo "PR bundle: raw=${RAW_SIZE} bytes, gzip=${GZIP_SIZE} bytes"
74+
75+
- name: Build audience SDK (base) and measure
76+
id: base_size
77+
env:
78+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
79+
BASE_REF: ${{ github.event.pull_request.base.ref }}
80+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
81+
BUNDLE: ${{ steps.budget.outputs.bundle_path }}
82+
run: |
83+
if git checkout "$BASE_SHA" \
84+
&& pnpm install --frozen-lockfile \
85+
&& pnpm --filter @imtbl/audience... build \
86+
&& [ -f "$BUNDLE" ]; then
87+
RAW_SIZE=$(stat --format=%s "$BUNDLE")
88+
GZIP_SIZE=$(gzip -c "$BUNDLE" | wc -c)
89+
{
90+
echo "ok=true"
91+
echo "raw=$RAW_SIZE"
92+
echo "gzip=$GZIP_SIZE"
93+
} >> "$GITHUB_OUTPUT"
94+
echo "Base bundle: ok=true, raw=${RAW_SIZE} bytes, gzip=${GZIP_SIZE} bytes"
95+
else
96+
echo "ok=false" >> "$GITHUB_OUTPUT"
97+
echo "::warning::Base build at ${BASE_SHA} unavailable — delta vs ${BASE_REF} will be reported as n/a"
98+
fi
99+
100+
# Switch back to the PR code so later steps run against the right version.
101+
git checkout "$HEAD_SHA"
102+
pnpm install --frozen-lockfile
103+
104+
- name: Evaluate bundle size
105+
id: evaluate
106+
env:
107+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
108+
BASE_REF: ${{ github.event.pull_request.base.ref }}
109+
PR_GZIP: ${{ steps.pr_size.outputs.gzip }}
110+
PR_RAW: ${{ steps.pr_size.outputs.raw }}
111+
BASE_GZIP: ${{ steps.base_size.outputs.gzip }}
112+
BASE_RAW: ${{ steps.base_size.outputs.raw }}
113+
BASE_OK: ${{ steps.base_size.outputs.ok }}
114+
MAX_GZIP: ${{ steps.budget.outputs.max_gzip }}
115+
WARN_GZIP: ${{ steps.budget.outputs.warn_gzip }}
116+
run: |
117+
BASE_SHA_SHORT="${BASE_SHA:0:7}"
118+
119+
if [ "$BASE_OK" = "true" ]; then
120+
DELTA_GZIP=$((PR_GZIP - BASE_GZIP))
121+
DELTA_RAW=$((PR_RAW - BASE_RAW))
122+
if [ $DELTA_GZIP -gt 0 ]; then DELTA_GZIP_FMT="+${DELTA_GZIP} bytes"; else DELTA_GZIP_FMT="${DELTA_GZIP} bytes"; fi
123+
if [ $DELTA_RAW -gt 0 ]; then DELTA_RAW_FMT="+${DELTA_RAW} bytes"; else DELTA_RAW_FMT="${DELTA_RAW} bytes"; fi
124+
else
125+
DELTA_GZIP_FMT="n/a (base build unavailable)"
126+
DELTA_RAW_FMT="n/a"
127+
fi
128+
129+
STATUS="pass"
130+
STATUS_ICON="white_check_mark"
131+
if [ "$PR_GZIP" -gt "$MAX_GZIP" ]; then
132+
STATUS="fail"
133+
STATUS_ICON="x"
134+
elif [ "$PR_GZIP" -gt "$WARN_GZIP" ]; then
135+
STATUS="warn"
136+
STATUS_ICON="warning"
137+
fi
138+
139+
PR_GZIP_KB=$(echo "scale=2; $PR_GZIP / 1024" | bc)
140+
MAX_GZIP_KB=$(echo "scale=2; $MAX_GZIP / 1024" | bc)
141+
WARN_GZIP_KB=$(echo "scale=2; $WARN_GZIP / 1024" | bc)
142+
143+
{
144+
echo "## :${STATUS_ICON}: Audience Bundle Size — @imtbl/audience"
145+
echo ""
146+
echo "| Metric | Size | Delta vs \`${BASE_REF}\` (${BASE_SHA_SHORT}) |"
147+
echo "|--------|------|---------------|"
148+
echo "| **Gzipped** | ${PR_GZIP} bytes (${PR_GZIP_KB} KB) | ${DELTA_GZIP_FMT} |"
149+
echo "| Raw (minified) | ${PR_RAW} bytes | ${DELTA_RAW_FMT} |"
150+
echo ""
151+
echo "**Budget:** ${MAX_GZIP_KB} KB gzipped (warn at ${WARN_GZIP_KB} KB)"
152+
} > /tmp/comment-body.md
153+
154+
if [ "$BASE_OK" != "true" ]; then
155+
echo "" >> /tmp/comment-body.md
156+
echo "> :information_source: Base build at \`${BASE_SHA_SHORT}\` (\`${BASE_REF}\`) was unavailable; delta could not be computed. Gate still enforces the absolute budget." >> /tmp/comment-body.md
157+
fi
158+
159+
if [ "$STATUS" = "warn" ]; then
160+
echo "" >> /tmp/comment-body.md
161+
echo "> :warning: **Approaching budget** — gzipped size exceeds ${WARN_GZIP_KB} KB warning threshold." >> /tmp/comment-body.md
162+
fi
163+
164+
if [ "$STATUS" = "fail" ]; then
165+
echo "" >> /tmp/comment-body.md
166+
echo "> :x: **Over budget** — gzipped size exceeds ${MAX_GZIP_KB} KB limit. Reduce bundle size before merging." >> /tmp/comment-body.md
167+
fi
168+
169+
echo "status=$STATUS" >> "$GITHUB_OUTPUT"
170+
171+
EOF_MARKER=$(head -c 20 /dev/urandom | base64 | tr -d '/+=' | head -c 20)
172+
{
173+
echo "comment<<${EOF_MARKER}"
174+
cat /tmp/comment-body.md
175+
echo "${EOF_MARKER}"
176+
} >> "$GITHUB_OUTPUT"
177+
178+
cat /tmp/comment-body.md >> "$GITHUB_STEP_SUMMARY"
179+
180+
- name: Post PR comment
181+
# Without this guard, fork PRs fail the whole job on a permission error.
182+
if: github.event.pull_request.head.repo.full_name == github.repository
183+
uses: marocchino/sticky-pull-request-comment@67d0dec7b07ed060a405f9b2a64b8ab319fdd7db # pin@v2.9.2
184+
with:
185+
header: audience-bundle-size
186+
message: ${{ steps.evaluate.outputs.comment }}
187+
188+
- name: Fail if over budget
189+
if: steps.evaluate.outputs.status == 'fail'
190+
env:
191+
PR_GZIP: ${{ steps.pr_size.outputs.gzip }}
192+
MAX_GZIP: ${{ steps.budget.outputs.max_gzip }}
193+
run: |
194+
echo "::error::Audience bundle gzipped size (${PR_GZIP} bytes) exceeds budget (${MAX_GZIP} bytes)"
195+
exit 1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"budgets": [
3+
{
4+
"file": "dist/cdn/imtbl-audience.global.js",
5+
"maxSizeGzip": 24576,
6+
"warnSizeGzip": 20480
7+
}
8+
]
9+
}

0 commit comments

Comments
 (0)