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