#requires -Version 7.0 param([string]$Version = '', [string]$Directory = 'artifacts/releases', [switch]$Publish, [switch]$RequireTag) $ErrorActionPreference = 'Stop' Set-Location (Split-Path $PSScriptRoot) [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 -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 -and $_.PackageId -eq 'AstroFrame' }) if ($latest.Count -ne 1) { throw "No full $Version release for $channel" } 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.' } $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/git/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 } } if ($release -and !$release.draft) { throw "$tag is already published. Release assets are immutable; build a new version." } if (!$release) { $body = @{ tag_name=$tag; target_commitish=$commit; name="AstroFrame $Version"; body="AstroFrame $Version for Windows and Linux. See the repository README for installation and update instructions."; draft=$true; prerelease=$false } | ConvertTo-Json $release = Invoke-RestMethod "$api/releases" -Method Post -Headers $headers -ContentType 'application/json' -Body $body } $attached = @(Invoke-RestMethod "$api/releases/$($release.id)/assets" -Headers $headers) foreach ($file in $files) { $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) $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 Forgejo." }