Automate Windows and Linux releases with Forgejo Actions
Some checks are pending
Release AstroFrame / packages (push) Waiting to run

This commit is contained in:
Elijah 2026-10-03 19:31:43 -07:00
parent f971c374f8
commit 4e6d21de5a
9 changed files with 277 additions and 24 deletions

View file

@ -0,0 +1,16 @@
#requires -Version 7.0
param([string]$Ref = $env:RELEASE_REF)
$ErrorActionPreference = 'Stop'
Set-Location (Split-Path $PSScriptRoot)
[xml]$props = Get-Content Directory.Build.props
$version = [string]$props.Project.PropertyGroup.Version
if ($version -notmatch '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$') { throw 'Directory.Build.props must contain a stable version such as 1.0.0.' }
if ($Ref -like 'refs/tags/*' -and $Ref -cne "refs/tags/v$version") {
throw "Release tag $Ref does not match version $version in Directory.Build.props."
}
if (!$Ref) { throw 'Supply the source ref, for example -Ref refs/tags/v1.0.0 or -Ref refs/heads/main for a build-only run.' }
Write-Output "Validating AstroFrame $version from $Ref"
& ./Build.ps1 -Test
if ($LASTEXITCODE) { throw 'Release validation failed.' }
& ./packaging/Release.ps1 -Version $version -Platform Both -PublishRoot artifacts/server-release/publish -Output artifacts/server-release/packages
Write-Output "Windows and Linux packages ready for AstroFrame $version."

View file

@ -1,30 +1,55 @@
param([string]$Version = '', [string]$Directory = 'artifacts/releases', [switch]$Publish)
#requires -Version 7.0
param([string]$Version = '', [string]$Directory = 'artifacts/releases', [switch]$Publish, [switch]$RequireTag)
$ErrorActionPreference = 'Stop'
Set-Location (Split-Path $PSScriptRoot)
if (!$Version) { [xml]$props = Get-Content Directory.Build.props; $Version = $props.Project.PropertyGroup.Version }
if ($Version -notmatch '^\d+\.\d+\.\d+$') { throw 'Use a stable version such as 1.0.0.' }
if (!$env:ASTROFRAME_RELEASE_TOKEN) { throw 'Set ASTROFRAME_RELEASE_TOKEN to a Gitea token with repository write permission. Never put it in source control.' }
[xml]$props = Get-Content Directory.Build.props
$sourceVersion = [string]$props.Project.PropertyGroup.Version
if (!$Version) { $Version = $sourceVersion }
if ($Version -notmatch '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$') { throw 'Use a stable version such as 1.0.0.' }
if ($Version -ne $sourceVersion) { throw "Package version $Version does not match source version $sourceVersion. Commit the matching version before releasing." }
if (!$env:ASTROFRAME_RELEASE_TOKEN) { throw 'Set ASTROFRAME_RELEASE_TOKEN to the Forgejo Actions token or a repository-write personal token.' }
$headers = @{ Authorization = "token $env:ASTROFRAME_RELEASE_TOKEN" }
$api = 'https://git.elijahkuntz.com/api/v1/repos/Elijah/AstroFrame'
$files = @()
foreach ($channel in @('win-x64','linux-x64')) {
$folder = Join-Path $Directory $channel
$feedPath = Join-Path $folder "releases.$channel.json"
if (!(Test-Path $feedPath)) { throw "Missing release feed: $feedPath" }
if (!(Test-Path -LiteralPath $feedPath)) { throw "Missing release feed: $feedPath" }
$feed = Get-Content $feedPath -Raw | ConvertFrom-Json
$latest = @($feed.Assets | Where-Object { $_.Type -eq 'Full' -and $_.Version -eq $Version })
$latest = @($feed.Assets | Where-Object { $_.Type -eq 'Full' -and $_.Version -eq $Version -and $_.PackageId -eq 'AstroFrame' })
if ($latest.Count -ne 1) { throw "No full $Version release for $channel" }
$files += @(Get-ChildItem -LiteralPath $folder -File | Where-Object { $_.Extension -in @('.exe','.AppImage','.nupkg','.json','.sh','.png','.zip') -or $_.Name -like 'RELEASES-*' })
}
foreach ($file in $files) {
if ($file.Extension -eq '.nupkg' -and $file.Name -notlike "AstroFrame-$Version-*") { continue }
if ($file.Length -eq 0) { throw "Empty release asset: $($file.Name)" }
if (@($feed.Assets | Where-Object { $_.Type -eq 'Full' -and [version]$_.Version -gt [version]$Version }).Count) { throw "Feed $channel contains a newer release than $Version." }
$names = @("releases.$channel.json", "RELEASES-$channel", "assets.$channel.json")
foreach ($asset in @($feed.Assets | Where-Object { $_.Version -eq $Version })) {
if ($asset.FileName -notmatch "^AstroFrame-$([regex]::Escape($Version))-$channel-(full|delta)\.nupkg$") { throw "Unexpected package name in $channel feed." }
$path = Join-Path $folder $asset.FileName
if (!(Test-Path -LiteralPath $path)) { throw "Missing package: $path" }
if ((Get-Item -LiteralPath $path).Length -ne $asset.Size -or (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash -ne $asset.SHA256) { throw "Package does not match its feed checksum: $path" }
$names += $asset.FileName
}
if ($channel -eq 'win-x64') { $names += 'AstroFrame-win-x64-Setup.exe' }
else {
$names += @('AstroFrame-linux-x64.AppImage', 'install-linux.sh', 'astroframe.png')
if (Test-Path -LiteralPath (Join-Path $folder "AstroFrame-$Version-linux-x64.zip")) { $names += "AstroFrame-$Version-linux-x64.zip" }
}
foreach ($name in $names) {
$file = Get-Item -LiteralPath (Join-Path $folder $name)
if ($file.Length -eq 0) { throw "Empty release asset: $name" }
$files += $file
}
}
$commit = (& git rev-parse HEAD).Trim()
if ($LASTEXITCODE) { throw 'Cannot identify the release commit.' }
if (@(& git status --porcelain).Count -gt 0) { throw 'Commit the release source and push it before publishing.' }
$status = @(& git status --porcelain)
if ($LASTEXITCODE) { throw 'Cannot check release source status.' }
if ($status.Count -gt 0) { throw 'Commit the release source and push it before publishing.' }
Invoke-RestMethod "$api/commits/$commit" -Headers $headers | Out-Null
$tag = "v$Version"
$remoteTag = $null
try { $remoteTag = Invoke-RestMethod "$api/tags/$tag" -Headers $headers }
catch { if ([int]$_.Exception.Response.StatusCode -ne 404) { throw } }
if ($RequireTag -and !$remoteTag) { throw "Push tag $tag before running the server release publisher." }
if ($remoteTag -and $remoteTag.commit.sha -ne $commit) { throw "Tag $tag points to another commit. Use the tagged source to publish." }
$release = $null
try { $release = Invoke-RestMethod "$api/releases/tags/$tag" -Headers $headers }
catch { if ([int]$_.Exception.Response.StatusCode -ne 404) { throw } }
@ -35,13 +60,26 @@ if (!$release) {
}
$attached = @(Invoke-RestMethod "$api/releases/$($release.id)/assets" -Headers $headers)
foreach ($file in $files) {
if ($file.Extension -eq '.nupkg' -and $file.Name -notlike "AstroFrame-$Version-*") { continue }
if ($attached.name -contains $file.Name) { throw "Draft already contains $($file.Name). Remove the conflicting asset explicitly before retrying." }
$existing = @($attached | Where-Object name -EQ $file.Name)
if ($existing.Count) {
# Resume partial drafts only when the existing upload is byte-for-byte identical.
$downloadUrl = [uri]$existing[0].browser_download_url
if ($downloadUrl.Scheme -ne 'https' -or $downloadUrl.Host -ne 'git.elijahkuntz.com') { throw 'Unexpected release asset download host.' }
$temporary = Join-Path ([IO.Path]::GetTempPath()) ("AstroFrame-asset-check-" + [guid]::NewGuid())
try {
Invoke-WebRequest -Uri $downloadUrl -Headers $headers -OutFile $temporary
if ((Get-FileHash -LiteralPath $temporary -Algorithm SHA256).Hash -ne (Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256).Hash) { throw "Draft contains different bytes for $($file.Name). Remove that draft asset explicitly before retrying." }
} finally { if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary } }
Write-Output "Already attached: $($file.Name)"
continue
}
$name = [Uri]::EscapeDataString($file.Name)
Invoke-RestMethod "$api/releases/$($release.id)/assets?name=$name" -Method Post -Headers $headers -Form @{ attachment=$file } | Out-Null
$uploaded = Invoke-RestMethod "$api/releases/$($release.id)/assets?name=$name" -Method Post -Headers $headers -Form @{ attachment=$file }
if ($uploaded.name -ne $file.Name -or $uploaded.size -ne $file.Length) { throw "Incomplete upload: $($file.Name). Release remains a draft." }
Write-Output "Attached $($file.Name)"
}
# A failed upload never makes a partial version visible to installed applications.
if ($Publish) {
Invoke-RestMethod "$api/releases/$($release.id)" -Method Patch -Headers $headers -ContentType 'application/json' -Body '{"draft":false}' | Out-Null
Write-Output "Published $($release.html_url)"
} else { Write-Output "Draft release ready: $($release.html_url). Review it, then publish in Gitea." }
} else { Write-Output "Draft release ready: $($release.html_url). Review it, then publish in Forgejo." }

View file

@ -1,3 +1,4 @@
#requires -Version 7.0
param(
[string]$Version = '',
[ValidateSet('Both','Windows','Linux')][string]$Platform = 'Both',
@ -14,14 +15,15 @@ $env:NUGET_PACKAGES = Join-Path $root '.tools/packages'
$env:DOTNET_CLI_TELEMETRY_OPTOUT = '1'
New-Item -ItemType Directory -Force '.tools/feed' | Out-Null
$sdk = Join-Path $root '.tools/dotnet/dotnet.exe'
if (!(Test-Path $sdk)) { $sdk = 'dotnet' }
if (!$IsWindows -or !(Test-Path $sdk)) { $sdk = 'dotnet' }
$localVpk = Join-Path $root '.tools/vpk/vpk.exe'
if (!(Test-Path $localVpk)) {
$useLocalVpk = $IsWindows -and (Test-Path $localVpk)
if (!$useLocalVpk) {
& dotnet tool restore
if ($LASTEXITCODE) { throw 'Velopack restore failed. Its CLI requires a .NET 10 SDK/runtime.' }
}
function Invoke-Vpk([string[]]$Arguments) {
if (Test-Path $localVpk) { & $localVpk @Arguments } else { & dotnet tool run vpk -- @Arguments }
if ($useLocalVpk) { & $localVpk @Arguments } else { & dotnet tool run vpk -- @Arguments }
if ($LASTEXITCODE) { throw 'Velopack packaging failed.' }
}
foreach ($rid in @('win-x64','linux-x64')) {

View file

@ -0,0 +1,8 @@
param([Parameter(Mandatory)][ValidatePattern('^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$')][string]$Version)
$ErrorActionPreference = 'Stop'
$path = Join-Path (Split-Path $PSScriptRoot) 'Directory.Build.props'
$content = Get-Content -LiteralPath $path -Raw
if ([regex]::Matches($content, '<Version>[^<]+</Version>').Count -ne 1) { throw 'Expected one Version in Directory.Build.props.' }
$content = [regex]::Replace($content, '<Version>[^<]+</Version>', "<Version>$Version</Version>")
[IO.File]::WriteAllText($path, $content)
Write-Output "AstroFrame version set to $Version. Commit and push the source, then push tag v$Version to build and publish automatically."