Skip to content

Commit 52f17e7

Browse files
committed
Move Bun baseline runtime setup into CI
1 parent ad6c0e5 commit 52f17e7

4 files changed

Lines changed: 64 additions & 80 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: 'Setup Bun Compile Runtime'
2+
description: 'Download and cache a Bun runtime used by bun build --compile-executable-path'
3+
4+
inputs:
5+
target:
6+
description: 'Bun compile target, for example bun-windows-x64-baseline'
7+
required: true
8+
9+
runs:
10+
using: 'composite'
11+
steps:
12+
- name: Get Bun version
13+
id: bun-version
14+
shell: bash
15+
run: echo "version=$(bun --version)" >> "$GITHUB_OUTPUT"
16+
17+
- name: Cache Bun compile runtime
18+
uses: actions/cache@v5
19+
with:
20+
path: ${{ runner.temp }}/bun-compile-runtimes/${{ inputs.target }}-v${{ steps.bun-version.outputs.version }}
21+
key: ${{ runner.os }}-bun-compile-runtime-${{ inputs.target }}-v${{ steps.bun-version.outputs.version }}
22+
23+
- name: Prepare Bun compile runtime
24+
shell: pwsh
25+
env:
26+
BUN_COMPILE_TARGET: ${{ inputs.target }}
27+
BUN_VERSION: ${{ steps.bun-version.outputs.version }}
28+
RUNTIME_DIR: ${{ runner.temp }}/bun-compile-runtimes/${{ inputs.target }}-v${{ steps.bun-version.outputs.version }}
29+
run: |
30+
$ErrorActionPreference = 'Stop'
31+
32+
$runtimePath = Join-Path $env:RUNTIME_DIR 'bun.exe'
33+
if (!(Test-Path -LiteralPath $runtimePath)) {
34+
New-Item -ItemType Directory -Force -Path $env:RUNTIME_DIR | Out-Null
35+
36+
$zipPath = Join-Path $env:RUNTIME_DIR "$($env:BUN_COMPILE_TARGET).zip"
37+
$downloadUrl = "https://github.com/oven-sh/bun/releases/download/bun-v$($env:BUN_VERSION)/$($env:BUN_COMPILE_TARGET).zip"
38+
39+
Write-Host "Downloading $($env:BUN_COMPILE_TARGET): $downloadUrl"
40+
Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath
41+
Expand-Archive -LiteralPath $zipPath -DestinationPath $env:RUNTIME_DIR -Force
42+
43+
$extractedRuntimePath = Join-Path $env:RUNTIME_DIR "$($env:BUN_COMPILE_TARGET)/bun.exe"
44+
if (!(Test-Path -LiteralPath $extractedRuntimePath)) {
45+
throw "Downloaded $($env:BUN_COMPILE_TARGET), but bun.exe was not found at $extractedRuntimePath"
46+
}
47+
48+
Copy-Item -LiteralPath $extractedRuntimePath -Destination $runtimePath -Force
49+
}
50+
51+
"BUN_COMPILE_EXECUTABLE_PATH=$runtimePath" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8

.github/workflows/cli-release-build.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ jobs:
315315
echo "$ENV_OVERRIDES" | jq -r 'to_entries | .[] | .key + "=" + .value' >> $GITHUB_ENV
316316
fi
317317
318+
- name: Prepare Windows baseline Bun compile runtime
319+
uses: ./.github/actions/setup-bun-compile-runtime
320+
with:
321+
target: bun-windows-x64-baseline
322+
318323
- name: Build binary
319324
run: bun run scripts/build-binary.ts ${{ inputs.binary-name }} ${{ inputs.new-version }}
320325
working-directory: cli

.github/workflows/freebuff-e2e.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ jobs:
162162
echo "NEXT_PUBLIC_CB_ENVIRONMENT=prod" >> $GITHUB_ENV
163163
echo "CODEBUFF_GITHUB_ACTIONS=true" >> $GITHUB_ENV
164164
165+
- name: Prepare Windows baseline Bun compile runtime
166+
uses: ./.github/actions/setup-bun-compile-runtime
167+
with:
168+
target: bun-windows-x64-baseline
169+
165170
- name: Build Freebuff binary
166171
run: bun freebuff/cli/build.ts 0.0.0-e2e
167172
shell: bash

cli/scripts/build-binary.ts

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
rmSync,
1313
writeFileSync,
1414
} from 'fs'
15-
import { homedir, tmpdir } from 'os'
15+
import { tmpdir } from 'os'
1616
import { dirname, join } from 'path'
1717
import { fileURLToPath } from 'url'
1818

@@ -113,82 +113,6 @@ function getTargetInfo(): TargetInfo {
113113
return target
114114
}
115115

116-
async function getCompileExecutablePath(
117-
targetInfo: TargetInfo,
118-
): Promise<string | undefined> {
119-
if (OVERRIDE_COMPILE_EXECUTABLE_PATH) {
120-
return OVERRIDE_COMPILE_EXECUTABLE_PATH
121-
}
122-
123-
// Bun 1.3.11 can fail to extract this target from inside `bun build` on
124-
// Windows runners. Pre-fetch the official release zip and point Bun at the
125-
// extracted runtime so release and Freebuff CI builds still produce the
126-
// baseline binary.
127-
if (
128-
process.platform !== 'win32' ||
129-
targetInfo.bunTarget !== 'bun-windows-x64-baseline'
130-
) {
131-
return undefined
132-
}
133-
134-
const cacheRoot = join(
135-
process.env.BUN_INSTALL ?? join(homedir(), '.bun'),
136-
'install',
137-
'cache',
138-
)
139-
const runtimeDir = join(
140-
cacheRoot,
141-
`${targetInfo.bunTarget}-v${Bun.version}-runtime`,
142-
)
143-
const runtimePath = join(runtimeDir, 'bun.exe')
144-
145-
if (existsSync(runtimePath)) {
146-
return runtimePath
147-
}
148-
149-
rmSync(runtimeDir, { recursive: true, force: true })
150-
mkdirSync(runtimeDir, { recursive: true })
151-
152-
const zipPath = join(runtimeDir, `${targetInfo.bunTarget}.zip`)
153-
const downloadUrl = `https://github.com/oven-sh/bun/releases/download/bun-v${Bun.version}/${targetInfo.bunTarget}.zip`
154-
155-
logAlways(`Downloading ${targetInfo.bunTarget}: ${downloadUrl}`)
156-
const response = await fetch(downloadUrl)
157-
if (!response.ok) {
158-
throw new Error(
159-
`Failed to download ${targetInfo.bunTarget}: ${response.status} ${response.statusText}`,
160-
)
161-
}
162-
writeFileSync(zipPath, new Uint8Array(await response.arrayBuffer()))
163-
164-
runCommand(
165-
'powershell.exe',
166-
[
167-
'-NoProfile',
168-
'-NonInteractive',
169-
'-ExecutionPolicy',
170-
'Bypass',
171-
'-Command',
172-
`Expand-Archive -LiteralPath '${zipPath.replaceAll("'", "''")}' -DestinationPath '${runtimeDir.replaceAll("'", "''")}' -Force`,
173-
],
174-
{ env: process.env },
175-
)
176-
177-
const extractedRuntimePath = join(
178-
runtimeDir,
179-
'bun-windows-x64-baseline',
180-
'bun.exe',
181-
)
182-
if (!existsSync(extractedRuntimePath)) {
183-
throw new Error(
184-
`Downloaded ${targetInfo.bunTarget}, but bun.exe was not found at ${extractedRuntimePath}`,
185-
)
186-
}
187-
188-
writeFileSync(runtimePath, readFileSync(extractedRuntimePath))
189-
return runtimePath
190-
}
191-
192116
async function main() {
193117
const [, , binaryNameArg, version] = process.argv
194118
const binaryName = binaryNameArg ?? 'codecane'
@@ -200,7 +124,6 @@ async function main() {
200124
log(`Building ${binaryName} @ ${version}`)
201125

202126
const targetInfo = getTargetInfo()
203-
const compileExecutablePath = await getCompileExecutablePath(targetInfo)
204127
const binDir = join(cliRoot, 'bin')
205128

206129
if (!existsSync(binDir)) {
@@ -251,8 +174,8 @@ async function main() {
251174
'--compile',
252175
'--production', // Required so compiled binaries use the production JSX runtime (avoids jsxDEV crashes).
253176
`--target=${targetInfo.bunTarget}`,
254-
...(compileExecutablePath
255-
? [`--compile-executable-path=${compileExecutablePath}`]
177+
...(OVERRIDE_COMPILE_EXECUTABLE_PATH
178+
? [`--compile-executable-path=${OVERRIDE_COMPILE_EXECUTABLE_PATH}`]
256179
: []),
257180
`--outfile=${outputFile}`,
258181
'--sourcemap=none',

0 commit comments

Comments
 (0)