diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..bed8dba --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,45 @@ +# Build and attach release binaries when a version tag is pushed. +name: Release + +on: + push: + tags: + - "v*" + +permissions: + contents: write + +jobs: + windows: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Build release + run: cargo build --release + + - name: Package + shell: pwsh + run: | + $ver = "${{ github.ref_name }}".TrimStart("v") + $target = "x86_64-pc-windows-msvc" + New-Item -ItemType Directory -Force -Path dist | Out-Null + $exe = "marqdo-$ver-$target.exe" + $zip = "marqdo-$ver-$target.zip" + Copy-Item target/release/marqdo.exe "dist/$exe" + Compress-Archive -Path "dist/$exe" -DestinationPath "dist/$zip" -Force + echo "EXE=$exe" >> $env:GITHUB_ENV + echo "ZIP=$zip" >> $env:GITHUB_ENV + + - name: Upload release assets + uses: softprops/action-gh-release@v2 + with: + files: | + dist/${{ env.EXE }} + dist/${{ env.ZIP }} + fail_on_unmatched_files: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 10391ee..8516cf1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target/ +/dist/ **/*.rs.bk .idea/ .vscode/ diff --git a/scripts/release-windows.ps1 b/scripts/release-windows.ps1 new file mode 100644 index 0000000..5da1e91 --- /dev/null +++ b/scripts/release-windows.ps1 @@ -0,0 +1,60 @@ +# Build Windows release assets and upload them to an existing GitHub Release tag. +# Usage (from repo root): +# powershell -File ./scripts/release-windows.ps1 +# powershell -File ./scripts/release-windows.ps1 -Tag v0.0.4 +param( + [string]$Tag = "" +) + +$ErrorActionPreference = "Stop" +Set-Location (Split-Path $PSScriptRoot -Parent) + +if (-not $Tag) { + $Tag = (Select-String -Path Cargo.toml -Pattern '^version\s*=\s*"([^"]+)"').Matches.Groups[1].Value + $Tag = "v$Tag" +} +$ver = $Tag.TrimStart("v") +$target = "x86_64-pc-windows-msvc" +$dist = Join-Path (Get-Location) "dist" +New-Item -ItemType Directory -Force -Path $dist | Out-Null + +Write-Host "Building release binary for $Tag ..." +cargo build --release -q + +$exeName = "marqdo-$ver-$target.exe" +$zipName = "marqdo-$ver-$target.zip" +$exePath = Join-Path $dist $exeName +$zipPath = Join-Path $dist $zipName +Copy-Item ".\target\release\marqdo.exe" $exePath -Force +if (Test-Path $zipPath) { Remove-Item $zipPath -Force } +Compress-Archive -Path $exePath -DestinationPath $zipPath -Force + +$raw = ("protocol=https`nhost=github.com`n`n" | git credential fill 2>$null) -join "`n" +$user = ([regex]::Match($raw, '(?m)^username=(.+)$')).Groups[1].Value +$token = ([regex]::Match($raw, '(?m)^password=(.+)$')).Groups[1].Value +if (-not $token) { throw "No GitHub credentials from git credential fill; run gh auth login or configure git credentials." } +$pair = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${user}:${token}")) +$headers = @{ + Authorization = "Basic $pair" + Accept = "application/vnd.github+json" + "User-Agent" = "marqdo-release-windows" +} + +$rel = Invoke-RestMethod -Uri "https://api.github.com/repos/cflmy/marqdo/releases/tags/$Tag" -Headers $headers +Write-Host "Uploading to release $($rel.html_url)" + +foreach ($file in @($exeName, $zipName)) { + $path = Join-Path $dist $file + # Replace existing asset with same name + $existing = $rel.assets | Where-Object { $_.name -eq $file } + if ($existing) { + Invoke-RestMethod -Method Delete -Uri $existing.url -Headers $headers | Out-Null + } + $uri = "https://uploads.github.com/repos/cflmy/marqdo/releases/$($rel.id)/assets?name=$([uri]::EscapeDataString($file))" + $bytes = [System.IO.File]::ReadAllBytes($path) + $ctype = if ($file.EndsWith(".zip")) { "application/zip" } else { "application/octet-stream" } + $asset = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -ContentType $ctype -Body $bytes + Write-Host " $($asset.browser_download_url)" +} + +Write-Host "OK"