Binary Size Comment workflow (SurgeDM/Surge)
The Binary Size Comment workflow from SurgeDM/Surge, explained and optimized by Latchkey.
A
CI health: A - excellent
Point runs-on at Latchkey and get job timeouts, self-healing for flaky steps, and up to 58% lower cost, applied automatically.
What it does
This is the Binary Size Comment workflow from the SurgeDM/Surge repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
workflow (.yml)
name: Binary Size Comment
on:
workflow_run:
workflows: ["Binary Size Compare"]
types:
- completed
permissions:
pull-requests: write
jobs:
comment:
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Download artifact
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh run download ${{ github.event.workflow_run.id }} -n binary-size-data -R ${{ github.repository }} --dir .
- name: Comment on PR (Create or Update)
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const prNumber = Number(fs.readFileSync('./pr_number.txt', 'utf8').trim());
const pr = Number(fs.readFileSync('./size_pr.txt', 'utf8').trim());
const main = Number(fs.readFileSync('./size_main.txt', 'utf8').trim());
const diff = Number(fs.readFileSync('./diff.txt', 'utf8').trim());
const exceeds = fs.readFileSync('./exceeds.txt', 'utf8').trim() === 'true';
// Better formatter handling negative numbers correctly
const fmt = (n) => {
const units = ['B','KB','MB','GB'];
let i = 0; let v = Math.abs(n);
while (v >= 1024 && i < 3) { v /= 1024; i++; }
return `${(n < 0 ? '-' : '')}${v.toFixed(2)} ${units[i]}`;
};
const statusLabel = exceeds ? "⚠️ **Size Increased**" : "✅ **Size Decreased or Stable**";
// Hidden signature so the bot knows which comment is its own
const signature = "<!-- binary-size-signature -->";
const body = `${signature}
### Binary Size Analysis
${statusLabel}
| Version | Human Readable | Raw Bytes |
| :--- | :--- | :--- |
| **Main** | ${fmt(main)} | \`${main}\` |
| **PR** | ${fmt(pr)} | \`${pr}\` |
| **Difference** | **${fmt(diff)}** | \`${diff}\` |
`;
// Look for existing comment
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber
});
const existingComment = comments.find(c => c.body.includes(signature));
if (existingComment) {
// Update the existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body
});
} else if (exceeds) {
// Only create a NEW comment if the size increased
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body
});
}
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
name: Binary Size Comment on: workflow_run: workflows: ["Binary Size Compare"] types: - completed permissions: pull-requests: write jobs: comment: timeout-minutes: 30 if: > github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' runs-on: latchkey-small steps: - name: Download artifact env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh run download ${{ github.event.workflow_run.id }} -n binary-size-data -R ${{ github.repository }} --dir . - name: Comment on PR (Create or Update) uses: actions/github-script@v7 with: script: | const fs = require('fs'); const prNumber = Number(fs.readFileSync('./pr_number.txt', 'utf8').trim()); const pr = Number(fs.readFileSync('./size_pr.txt', 'utf8').trim()); const main = Number(fs.readFileSync('./size_main.txt', 'utf8').trim()); const diff = Number(fs.readFileSync('./diff.txt', 'utf8').trim()); const exceeds = fs.readFileSync('./exceeds.txt', 'utf8').trim() === 'true'; // Better formatter handling negative numbers correctly const fmt = (n) => { const units = ['B','KB','MB','GB']; let i = 0; let v = Math.abs(n); while (v >= 1024 && i < 3) { v /= 1024; i++; } return `${(n < 0 ? '-' : '')}${v.toFixed(2)} ${units[i]}`; }; const statusLabel = exceeds ? "⚠️ **Size Increased**" : "✅ **Size Decreased or Stable**"; // Hidden signature so the bot knows which comment is its own const signature = "<!-- binary-size-signature -->"; const body = `${signature} ### Binary Size Analysis ${statusLabel} | Version | Human Readable | Raw Bytes | | :--- | :--- | :--- | | **Main** | ${fmt(main)} | \`${main}\` | | **PR** | ${fmt(pr)} | \`${pr}\` | | **Difference** | **${fmt(diff)}** | \`${diff}\` | `; // Look for existing comment const comments = await github.paginate(github.rest.issues.listComments, { owner: context.repo.owner, repo: context.repo.repo, issue_number: prNumber }); const existingComment = comments.find(c => c.body.includes(signature)); if (existingComment) { // Update the existing comment await github.rest.issues.updateComment({ owner: context.repo.owner, repo: context.repo.repo, comment_id: existingComment.id, body: body }); } else if (exceeds) { // Only create a NEW comment if the size increased await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: prNumber, body: body }); }
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Add a job timeout so a hung step cannot burn hours of runner time.
This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.