| 1 | # Publish multi-platform builds to GitLab: Generic Package + optional GitLab Release with asset links. |
| 2 | # Run from repo root on Windows (no Runner). Cross-compiles linux-x64, osx-x64 from Windows. |
| 3 | # |
| 4 | # Required env (or -GitLabUrl / -Token): |
| 5 | # GITLAB_URL, GITLAB_TOKEN (Personal Access Token, api) |
| 6 | # |
| 7 | # Usage: |
| 8 | # .\scripts\publish-release-win.ps1 -Version 0.5.1 |
| 9 | # .\scripts\publish-release-win.ps1 -Version 0.5.1 -CreateRelease |
| 10 | # .\scripts\publish-release-win.ps1 -Version 0.5.1 -Rids win-x64,linux-x64 |
| 11 | |
| 12 | param( |
| 13 | [Parameter(Mandatory = $true)] |
| 14 | [string] $Version, |
| 15 | [string] $Tag = "v$Version", |
| 16 | [string] $GitLabUrl, |
| 17 | [string] $Token, |
| 18 | [string] $ProjectPath = "Krawler/agent-notes-mcp", |
| 19 | [string[]] $Rids = @("win-x64", "linux-x64", "osx-x64"), |
| 20 | [string] $ReleaseDescription = "", |
| 21 | [switch] $CreateRelease |
| 22 | ) |
| 23 | |
| 24 | $ErrorActionPreference = "Stop" |
| 25 | $baseUrl = if ($GitLabUrl) { $GitLabUrl.TrimEnd('/') } |
| 26 | elseif ($env:GITLAB_URL) { $env:GITLAB_URL.TrimEnd('/') } |
| 27 | else { $null } |
| 28 | $token = if ($Token) { $Token } else { $env:GITLAB_TOKEN } |
| 29 | $pkgName = "agent-notes-mcp" |
| 30 | if (-not $baseUrl -or -not $token) { throw "Set GITLAB_URL and GITLAB_TOKEN (or pass -GitLabUrl and -Token)." } |
| 31 | $projectId = $ProjectPath -replace '/', '%2F' |
| 32 | $api = "$baseUrl/api/v4" |
| 33 | $zipPaths = @() |
| 34 | |
| 35 | foreach ($rid in $Rids) { |
| 36 | $zipName = "agent-notes-mcp-$rid.zip" |
| 37 | $outDir = "publish-release-temp-$rid" |
| 38 | if (Test-Path $outDir) { Remove-Item -Recurse -Force $outDir } |
| 39 | |
| 40 | Write-Host "Building $rid ..." |
| 41 | dotnet publish -c Release -r $rid -o $outDir |
| 42 | if ($LASTEXITCODE -ne 0) { |
| 43 | Write-Warning "dotnet publish -r $rid failed; skipping." |
| 44 | continue |
| 45 | } |
| 46 | |
| 47 | $zipPath = Join-Path $PWD $zipName |
| 48 | if (Test-Path $zipPath) { Remove-Item -Force $zipPath } |
| 49 | Compress-Archive -Path "$outDir\*" -DestinationPath $zipPath |
| 50 | Remove-Item -Recurse -Force $outDir |
| 51 | $zipPaths += @{ Name = $zipName; Path = $zipPath } |
| 52 | Write-Host " -> $zipName" |
| 53 | } |
| 54 | |
| 55 | if ($zipPaths.Count -eq 0) { Write-Error "No builds succeeded." } |
| 56 | |
| 57 | foreach ($z in $zipPaths) { |
| 58 | $uploadUrl = "$api/projects/$projectId/packages/generic/$pkgName/$Version/$($z.Name)" |
| 59 | Write-Host "Uploading $($z.Name) ..." |
| 60 | Invoke-RestMethod -Uri $uploadUrl -Method Put -InFile $z.Path -Headers @{ "PRIVATE-TOKEN" = $token } -ContentType "application/octet-stream" |
| 61 | } |
| 62 | |
| 63 | if ($CreateRelease) { |
| 64 | $commitSha = (git rev-parse HEAD).Trim() |
| 65 | $desc = if ($ReleaseDescription) { $ReleaseDescription } else { "Pre-built: $($Rids -join ', ') (no Runner)." } |
| 66 | $body = @{ |
| 67 | tag_name = $Tag |
| 68 | ref = $commitSha |
| 69 | name = "Release $Tag" |
| 70 | description = $desc |
| 71 | } | ConvertTo-Json |
| 72 | Invoke-RestMethod -Uri "$api/projects/$projectId/releases" -Method Post -Headers @{ "PRIVATE-TOKEN" = $token } -Body $body -ContentType "application/json" |
| 73 | Write-Host "Release $Tag created." |
| 74 | } |
| 75 | |
| 76 | foreach ($z in $zipPaths) { |
| 77 | $assetUrl = "$api/projects/$projectId/packages/generic/$pkgName/$Version/$($z.Name)" |
| 78 | $linkBody = @{ name = $z.Name; url = $assetUrl; link_type = "package" } | ConvertTo-Json |
| 79 | try { |
| 80 | Invoke-RestMethod -Uri "$api/projects/$projectId/releases/$Tag/assets/links" -Method Post -Headers @{ "PRIVATE-TOKEN" = $token } -Body $linkBody -ContentType "application/json; charset=utf-8" |
| 81 | Write-Host "Asset link added: $($z.Name)" |
| 82 | } catch { |
| 83 | Write-Warning "Could not add asset link for $($z.Name): $_" |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | foreach ($z in $zipPaths) { Remove-Item -Force $z.Path -ErrorAction SilentlyContinue } |
| 88 | Write-Host "Done." |
| 89 | |