|
| 1 | +# Render the ISO build table (kind × arch: exact size + download link) shared by |
| 2 | +# the test and release workflows. |
| 3 | +# |
| 4 | +# The build-iso action writes one `iso-sizes-<target>-<system>.tsv` per built |
| 5 | +# ISO; each workflow uploads those as `iso-meta-*` artifacts and downloads them |
| 6 | +# back into a single dir (merge-multiple) that this action reads. The table |
| 7 | +# links each kind/arch to its ISO artifact's browser download URL, resolved from |
| 8 | +# the current run's artifacts — the very artifacts the workflow just uploaded. |
| 9 | +# (When box moves ISOs to S3 these links become S3 URLs; only this action and |
| 10 | +# the upload steps change.) |
| 11 | +# |
| 12 | +# The rendered markdown is exposed as the `table` output so callers place it |
| 13 | +# wherever they need: the test workflow sets `sticky-comment: true` to upsert a |
| 14 | +# single PR comment; the release workflow feeds `table` into the release body. |
| 15 | +name: Render ISO table |
| 16 | +description: >- |
| 17 | + Render the coder/box ISO build table (size + download link per kind × arch) |
| 18 | + from per-arch size metadata, and optionally upsert it as a sticky PR comment. |
| 19 | +
|
| 20 | +inputs: |
| 21 | + meta-dir: |
| 22 | + description: >- |
| 23 | + Directory holding the per-arch `iso-sizes-<target>-<system>.tsv` rows |
| 24 | + (downloaded from the `iso-meta-*` artifacts, merged into one dir). |
| 25 | + required: false |
| 26 | + default: meta |
| 27 | + expiry-days: |
| 28 | + description: >- |
| 29 | + Artifact retention in days, shown in the table footer so readers know how |
| 30 | + long the download links stay live. |
| 31 | + required: false |
| 32 | + default: "1" |
| 33 | + sticky-comment: |
| 34 | + description: >- |
| 35 | + "true" upserts the table as a sticky pull-request comment (matched by a |
| 36 | + hidden marker so every rebuild updates the same comment). "false" only |
| 37 | + renders the `table` output. |
| 38 | + required: false |
| 39 | + default: "false" |
| 40 | + github-token: |
| 41 | + description: Token used to list run artifacts and (when sticky) upsert the PR comment. |
| 42 | + required: false |
| 43 | + default: ${{ github.token }} |
| 44 | + |
| 45 | +outputs: |
| 46 | + table: |
| 47 | + description: The rendered markdown table (without the sticky-comment marker). |
| 48 | + value: ${{ steps.render.outputs.table }} |
| 49 | + |
| 50 | +runs: |
| 51 | + using: composite |
| 52 | + steps: |
| 53 | + - name: Render ISO table |
| 54 | + id: render |
| 55 | + uses: actions/github-script@v7 |
| 56 | + env: |
| 57 | + META_DIR: ${{ inputs.meta-dir }} |
| 58 | + EXPIRY_DAYS: ${{ inputs.expiry-days }} |
| 59 | + STICKY_COMMENT: ${{ inputs.sticky-comment }} |
| 60 | + with: |
| 61 | + github-token: ${{ inputs.github-token }} |
| 62 | + script: | |
| 63 | + const fs = require('fs'); |
| 64 | + const path = require('path'); |
| 65 | +
|
| 66 | + const metaDir = process.env.META_DIR || 'meta'; |
| 67 | + const expiryDays = process.env.EXPIRY_DAYS || '1'; |
| 68 | + const sticky = process.env.STICKY_COMMENT === 'true'; |
| 69 | +
|
| 70 | + // 1. Collect exact ISO sizes from the per-arch TSV rows. |
| 71 | + const rows = []; |
| 72 | + const files = fs.existsSync(metaDir) ? fs.readdirSync(metaDir) : []; |
| 73 | + for (const f of files) { |
| 74 | + if (!f.startsWith('iso-sizes-') || !f.endsWith('.tsv')) continue; |
| 75 | + const text = fs.readFileSync(path.join(metaDir, f), 'utf8'); |
| 76 | + for (const line of text.split('\n')) { |
| 77 | + if (!line.trim()) continue; |
| 78 | + const [system, file, bytes] = line.split('\t'); |
| 79 | + rows.push({ system, file, bytes: Number(bytes) }); |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | + // 2. Map each ISO artifact name -> its browser download URL. |
| 84 | + const { owner, repo } = context.repo; |
| 85 | + const runId = context.runId; |
| 86 | + const arts = await github.paginate( |
| 87 | + github.rest.actions.listWorkflowRunArtifacts, |
| 88 | + { owner, repo, run_id: runId, per_page: 100 }, |
| 89 | + ); |
| 90 | + const urlFor = (name) => { |
| 91 | + const a = arts.find((x) => x.name === name); |
| 92 | + return a |
| 93 | + ? `https://github.com/${owner}/${repo}/actions/runs/${runId}/artifacts/${a.id}` |
| 94 | + : null; |
| 95 | + }; |
| 96 | +
|
| 97 | + // Small presentation helpers. |
| 98 | + const fmtSize = (b) => { |
| 99 | + if (!Number.isFinite(b) || b <= 0) return '—'; |
| 100 | + const u = ['B', 'KB', 'MB', 'GB', 'TB']; |
| 101 | + let i = 0, n = b; |
| 102 | + while (n >= 1024 && i < u.length - 1) { n /= 1024; i += 1; } |
| 103 | + return `${n.toFixed(i >= 2 ? 2 : 0)} ${u[i]}`; |
| 104 | + }; |
| 105 | + const prettyArch = (s) => s.replace(/-linux$/, ''); |
| 106 | + const kindOf = (file) => file.includes('-installer-') |
| 107 | + ? { slug: 'installer', label: 'Installer' } |
| 108 | + : { slug: 'appliance', label: 'Appliance' }; |
| 109 | +
|
| 110 | + // 3. Build the table, sorted by kind (installer before appliance) then |
| 111 | + // arch for a stable layout. |
| 112 | + const kindRank = (file) => (file.includes('-installer-') ? 0 : 1); |
| 113 | + rows.sort((a, b) => |
| 114 | + kindRank(a.file) - kindRank(b.file) || a.system.localeCompare(b.system)); |
| 115 | + let body = '## 📀 ISO build artifacts\n\n'; |
| 116 | + if (rows.length === 0) { |
| 117 | + body += '_No ISO artifacts were produced in this run._\n'; |
| 118 | + } else { |
| 119 | + body += '| Kind | Arch | Size | Download |\n|:--|:--|--:|:--:|\n'; |
| 120 | + for (const r of rows) { |
| 121 | + const k = kindOf(r.file); |
| 122 | + const url = urlFor(`coder-box-${k.slug}-${r.system}`); |
| 123 | + const dl = url ? `[⬇️ \`${r.file}\`](${url})` : '—'; |
| 124 | + body += `| ${k.label} | ${prettyArch(r.system)} | ${fmtSize(r.bytes)} | ${dl} |\n`; |
| 125 | + } |
| 126 | + } |
| 127 | + const sha = (context.payload.pull_request?.head?.sha || context.sha).slice(0, 7); |
| 128 | + const expiry = `artifacts expire in ~${expiryDays} day${expiryDays === '1' ? '' : 's'}`; |
| 129 | + body += `\n<sub>↻ Updated for \`${sha}\` · ` |
| 130 | + + `[run #${context.runNumber}](https://github.com/${owner}/${repo}/actions/runs/${runId}) · ` |
| 131 | + + `${expiry} · sign in to GitHub to download.</sub>\n`; |
| 132 | +
|
| 133 | + core.setOutput('table', body); |
| 134 | +
|
| 135 | + // 4. When requested (and on a PR), upsert the sticky comment matched by |
| 136 | + // this hidden marker instead of posting a new one each rebuild. |
| 137 | + if (sticky && context.payload.pull_request) { |
| 138 | + const MARKER = '<!-- iso-build-table -->'; |
| 139 | + const commentBody = `${body}\n${MARKER}`; |
| 140 | + const issue_number = context.payload.pull_request.number; |
| 141 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 142 | + owner, repo, issue_number, per_page: 100, |
| 143 | + }); |
| 144 | + const existing = comments.find((c) => c.body && c.body.includes(MARKER)); |
| 145 | + if (existing) { |
| 146 | + await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body: commentBody }); |
| 147 | + } else { |
| 148 | + await github.rest.issues.createComment({ owner, repo, issue_number, body: commentBody }); |
| 149 | + } |
| 150 | + } |
0 commit comments