refact(workflow) Improve Windows-specific directory handling in build… #24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
permissions: | |
contents: write | |
packages: write | |
actions: read | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- "package.json" | |
- "src/**" | |
- ".github/**" | |
jobs: | |
build: | |
strategy: | |
matrix: | |
include: | |
- version: "linux-x64" | |
os: ubuntu-latest | |
platform: linux-x64-baseline | |
- version: "linux-arm64" | |
os: ubuntu-latest | |
platform: linux-arm64 | |
- version: "windows-x64" | |
os: windows-latest | |
platform: windows-x64-baseline | |
# Wait https://github.com/oven-sh/bun/issues/9824 | |
# - version: "windows-arm64" | |
# os: windows-latest | |
# platform: windows-arm64 | |
- version: "darwin-x64" | |
os: ubuntu-latest | |
platform: darwin-x64 | |
- version: "darwin-arm64" | |
os: ubuntu-latest | |
platform: darwin-arm64 | |
runs-on: ${{ matrix.os }} | |
outputs: | |
version: ${{ steps.get_version.outputs.VERSION }} | |
platform: ${{ steps.detect_platform.outputs.PLATFORM }} | |
binary_name: ${{ steps.setup_env.outputs.BINARY_NAME }} | |
archive_name: ${{ steps.setup_env.outputs.ARCHIVE_NAME }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: oven-sh/[email protected] | |
with: | |
bun-version: latest | |
- name: Detect Platform and Compress Format | |
id: detect_platform | |
shell: bash | |
run: | | |
PLATFORM="${{ matrix.version }}" | |
echo "PLATFORM=${PLATFORM}" >> $GITHUB_OUTPUT | |
echo "Current platform: ${PLATFORM}" | |
if [[ "${PLATFORM}" == *"linux"* ]]; then | |
echo "COMPRESS_FORMAT=tar.xz" >> $GITHUB_OUTPUT | |
else | |
echo "COMPRESS_FORMAT=zip" >> $GITHUB_OUTPUT | |
fi | |
- name: Extract version number | |
id: get_version | |
shell: bash | |
run: | | |
VERSION=$(jq -r .version < package.json) | |
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT | |
- name: Setup Build Environment | |
id: setup_env | |
shell: bash | |
run: | | |
if [[ "${{ runner.os }}" == "Windows" ]]; then | |
mkdir -p "C:/build/out" | |
mkdir -p "C:/build/logs" | |
else | |
mkdir -p ${{ runner.temp }}/build/out | |
mkdir -p ${{ runner.temp }}/build/logs | |
fi | |
echo "BINARY_NAME=aqua-speed-${{ steps.detect_platform.outputs.PLATFORM }}_v${{ steps.get_version.outputs.VERSION }}" >> $GITHUB_OUTPUT | |
echo "ARCHIVE_NAME=aqua-speed-${{ steps.detect_platform.outputs.PLATFORM }}_v${{ steps.get_version.outputs.VERSION }}.${{ steps.detect_platform.outputs.COMPRESS_FORMAT }}" >> $GITHUB_OUTPUT | |
- name: Update System | |
if: runner.os == 'Linux' | |
run: | | |
sudo apt-get update -y | |
sudo apt-get install -y --only-upgrade tar | |
- name: Install Dependencies (Linux) | |
if: runner.os == 'Linux' | |
uses: nick-fields/retry@v2 | |
with: | |
timeout_seconds: 30 | |
max_attempts: 3 | |
retry_wait_seconds: 5 | |
command: bun install || { echo "Failed to install dependencies"; exit 1; } | |
shell: bash | |
- name: Install Dependencies (Windows) | |
if: runner.os == 'Windows' | |
uses: nick-fields/retry@v2 | |
with: | |
timeout_seconds: 30 | |
max_attempts: 3 | |
retry_wait_seconds: 5 | |
command: bun install | |
shell: powershell | |
- name: Setup Windows Cache Directory | |
if: runner.os == 'Windows' | |
shell: powershell | |
run: | | |
$cacheDir = "$env:USERPROFILE\.bun\install\cache" | |
New-Item -Path $cacheDir -ItemType Directory -Force | |
$acl = Get-Acl $cacheDir | |
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow") | |
$acl.SetAccessRule($rule) | |
Set-Acl $cacheDir $acl | |
- name: Compile Code | |
if: runner.os != 'Windows' | |
shell: bash | |
run: | | |
bun build --compile --sourcemap --minify --bytecode \ | |
--target=bun-${{ matrix.platform }} \ | |
./src/cli.ts \ | |
--outfile ${{ runner.temp }}/build/out/${{ steps.setup_env.outputs.BINARY_NAME }} | |
- name: Compile Code (Windows) | |
if: runner.os == 'Windows' | |
shell: powershell | |
continue-on-error: true | |
run: | | |
$outFileName = "${{ steps.setup_env.outputs.BINARY_NAME }}.exe" | |
$outFilePath = "C:/build/out/$outFileName" | |
try { | |
Write-Output "Starting bun build..." | |
bun build --compile --sourcemap --minify --bytecode ` | |
--target=bun-${{ matrix.platform }} ` | |
./src/cli.ts --outfile $outFilePath | |
if ($LASTEXITCODE -ne 0) { | |
throw "Bun build failed with exit code $LASTEXITCODE" | |
} | |
} catch { | |
Write-Error "Build command failed: $_" | |
exit 1 | |
} | |
if (-not (Test-Path $outFilePath)) { | |
Write-Error "Build process completed but output file is missing" | |
exit 1 | |
} | |
Write-Output "Build process completed successfully" | |
- name: Generate Checksums (Linux/macOS) | |
if: runner.os != 'Windows' | |
working-directory: ${{ runner.temp }}/build/out | |
shell: bash | |
run: | | |
sha1sum ${{ steps.setup_env.outputs.BINARY_NAME }}* > checksum.txt | |
- name: Generate Checksums (Windows) | |
if: runner.os == 'Windows' | |
shell: powershell | |
run: | | |
Set-Location "C:/build/out" | |
$binaryPattern = "${{ steps.setup_env.outputs.BINARY_NAME }}*" | |
$files = Get-ChildItem -Filter $binaryPattern | |
if ($files) { | |
$checksums = @() | |
foreach ($file in $files) { | |
$hash = Get-FileHash -Algorithm SHA1 -Path $file.FullName | |
$checksums += "$($hash.Hash.ToLower()) $($file.Name)" | |
} | |
$checksums | Set-Content -Path "checksum.txt" -Encoding utf8 | |
} else { | |
Write-Error "No files found matching pattern: $binaryPattern" | |
exit 1 | |
} | |
- name: Compress artifacts (Linux) | |
if: startsWith(steps.detect_platform.outputs.PLATFORM, 'linux') | |
working-directory: ${{ runner.temp }}/build/out | |
shell: bash | |
run: | | |
tar -c -I 'xz -5 -T0' -f ${{ steps.setup_env.outputs.ARCHIVE_NAME }} ${{ steps.setup_env.outputs.BINARY_NAME }}* checksum.txt | |
rm -f ${{ steps.setup_env.outputs.BINARY_NAME }} | |
cp checksum.txt ${{ steps.setup_env.outputs.BINARY_NAME }}.checksum.txt | |
- name: Compress artifacts (macOS) | |
if: contains(steps.detect_platform.outputs.PLATFORM, 'darwin') | |
working-directory: ${{ runner.temp }}/build/out | |
shell: bash | |
run: | | |
zip -j ${{ steps.setup_env.outputs.ARCHIVE_NAME }} ${{ steps.setup_env.outputs.BINARY_NAME }}* checksum.txt | |
rm -f ${{ steps.setup_env.outputs.BINARY_NAME }} | |
cp checksum.txt ${{ steps.setup_env.outputs.BINARY_NAME }}.checksum.txt | |
- name: Compress artifacts (Windows) | |
if: runner.os == 'Windows' | |
shell: powershell | |
run: | | |
Set-Location "C:/build/out" | |
$files = Get-ChildItem -Path "${{ steps.setup_env.outputs.BINARY_NAME }}*.exe", "checksum.txt" | |
if ($files) { | |
Compress-Archive -Path $files -DestinationPath "${{ steps.setup_env.outputs.ARCHIVE_NAME }}" -Force | |
Get-ChildItem -Filter "${{ steps.setup_env.outputs.BINARY_NAME }}*.exe" | Remove-Item | |
Copy-Item -Path "checksum.txt" -Destination "${{ steps.setup_env.outputs.BINARY_NAME }}.checksum.txt" -Force | |
} else { | |
Write-Error "No files found to compress" | |
} | |
- name: Upload build artifacts (Linux/macOS) | |
if: runner.os != 'Windows' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ steps.setup_env.outputs.ARCHIVE_NAME }} | |
path: | | |
${{ runner.temp }}/build/out/${{ steps.setup_env.outputs.ARCHIVE_NAME }} | |
${{ runner.temp }}/build/out/${{ steps.setup_env.outputs.BINARY_NAME }}.checksum.txt | |
retention-days: 1 | |
- name: Upload build artifacts (Windows) | |
if: runner.os == 'Windows' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ steps.setup_env.outputs.ARCHIVE_NAME }} | |
path: | | |
C:/build/out/${{ steps.setup_env.outputs.ARCHIVE_NAME }} | |
C:/build/out/${{ steps.setup_env.outputs.BINARY_NAME }}.checksum.txt | |
retention-days: 1 | |
release: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Download build artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
pattern: "aqua-speed-*" | |
path: ${{ runner.temp }}/release/ | |
merge-multiple: true | |
- name: Consolidate checksums | |
working-directory: ${{ runner.temp }}/release/ | |
run: | | |
cat *.checksum.txt > checksums.txt | |
rm *.checksum.txt | |
- name: List all files in the temp directory | |
run: | | |
ls -la ${{ runner.temp }}/release/ | |
- name: Get Previous Version | |
id: get_prev_version | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const releases = await github.rest.repos.listReleases({ | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}); | |
const currentVersion = '${{ needs.build.outputs.version }}'; | |
console.log(`Releases: ${context.repo.owner}/${context.repo.repo}`, releases.data.map(r => r.tag_name)); | |
console.log(`Creating version: v${currentVersion}`); | |
if (releases.data.length === 0) { | |
core.setOutput('version', 'first-commit'); | |
core.setOutput('compare_url', `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/commits`); | |
core.setOutput('changes_text', 'all commits'); | |
} else { | |
const previousRelease = releases.data[0]; | |
const previousTag = previousRelease.tag_name; | |
const previousVersion = previousTag.replace('v', ''); | |
console.log(`Previous version: ${previousTag}`); | |
core.setOutput('version', previousVersion); | |
core.setOutput('compare_url', | |
`${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/compare/v${previousVersion}...v${currentVersion}` | |
); | |
core.setOutput('changes_text', `changes since v${previousVersion}`); | |
} | |
- name: Create GitHub Release | |
uses: ncipollo/release-action@v1 | |
with: | |
artifacts: | | |
${{ runner.temp }}/release/*.tar.xz | |
${{ runner.temp }}/release/*.zip | |
${{ runner.temp }}/release/checksums.txt | |
token: ${{ github.token }} | |
tag: v${{ needs.build.outputs.version }} | |
name: Release ${{ needs.build.outputs.version }} | |
body: | | |
Release of version ${{ needs.build.outputs.version }} | |
[View ${{ steps.get_prev_version.outputs.changes_text }}](${{ steps.get_prev_version.outputs.compare_url }}) | |
draft: false | |
prerelease: false | |
allowUpdates: true | |
generateReleaseNotes: true |