Refactor CI pipeline for GitLab: streamline publish_release job and improve artifact handling

This commit is contained in:
MaddoScientisto 2026-02-21 20:01:31 +01:00
commit f8d4687a97

View file

@ -91,66 +91,47 @@ build_windows:
- "imagecatalog/bin/$BUILD_CONFIG/net10.0-windows/**" - "imagecatalog/bin/$BUILD_CONFIG/net10.0-windows/**"
expire_in: 1 hour expire_in: 1 hour
# Publish and create GitLab Release when building a tag. This job expects a Windows runner with PowerShell and curl available. # Publish and create GitLab Release when building a tag.
publish_release: publish_release:
stage: publish stage: publish
tags: image: mcr.microsoft.com/dotnet/sdk:10.0
- saas-windows-medium-amd64
needs: needs:
- build_windows - job: build_windows
script: artifacts: true
- | script: |
powershell -NoProfile -Command { set -euo pipefail
# Ensure .NET 10 SDK is available (install to user folder if missing) and use that dotnet for publish dotnet --info
$needsInstall = -not (dotnet --list-sdks 2>$null | Select-String '^10\.') echo "Reading MinVer version from build artifacts"
if ($needsInstall) { # locate a minversion file produced by the build artifacts
Write-Host 'Installing .NET 10 SDK using dotnet-install.ps1' minfile=$(find imagecatalog -type f -iname 'minversion' -print | head -n1 || true)
Invoke-WebRequest 'https://dot.net/v1/dotnet-install.ps1' -OutFile dotnet-install.ps1 -UseBasicParsing if [ -z "$minfile" ]; then minfile=$(find . -type f -iname 'minversion' -print | head -n1 || true); fi
.\dotnet-install.ps1 -Channel 10.0 -InstallDir $env:USERPROFILE\.dotnet if [ -z "$minfile" ]; then echo "No minversion file found in artifacts"; exit 1; fi
$dotnetExe = Join-Path $env:USERPROFILE '.dotnet\dotnet.exe' echo "Found minversion file: $minfile"
} else { version=$(cat "$minfile" | tr -d '\r\n')
$dotnetExe = 'dotnet' if [ -z "$version" ]; then echo "minversion was empty"; exit 1; fi
} echo "Using version: $version"
# find the primary artifact to upload (prefer the executable or first file under the build output)
& $dotnetExe publish "imagecatalog\ImageCatalog 2.csproj" -c $env:BUILD_CONFIG -f net10.0-windows -r win-x64 --self-contained false /p:PublishSingleFile=true /p:PublishReadyToRun=true -o publish file=$(find imagecatalog/bin -type f \! -iname 'minversion' -print | head -n1 || true)
Write-Host "Published to $(pwd)\\publish" if [ -z "$file" ]; then file=$(find . -type f -name 'ImageCatalog.*' -print | head -n1 || true); fi
if [ -z "$file" ]; then echo "No artifact file found to attach"; exit 1; fi
# Configure private NuGet source from GitLab Packages using CI_JOB_TOKEN echo "Uploading artifact: $file"
# Fallback to repository variables NUGET_USERNAME/NUGET_PASSWORD for shared runners uploadResp=$(curl --silent --show-error --header "JOB-TOKEN:$CI_JOB_TOKEN" --form "file=@$file" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/uploads")
$nugetUrl = 'https://gitlab.com/api/v4/projects/79509532/packages/nuget/index.json' assetPath=$(echo "$uploadResp" | sed -n 's/.*"url":"\([^\"]*\)".*/\1/p')
if ($env:CI_JOB_TOKEN) { if [ -z "$assetPath" ]; then echo "Upload failed: $uploadResp"; exit 1; fi
Write-Host 'Configuring private NuGet source Nuget-GitLab-AIFotoONLUS using CI_JOB_TOKEN...' assetUrl="$CI_SERVER_URL$assetPath"
try { & $dotnetExe nuget remove source Nuget-GitLab-AIFotoONLUS } catch {} echo "Uploaded asset url: $assetUrl"
& $dotnetExe nuget add source $nugetUrl --name Nuget-GitLab-AIFotoONLUS --username gitlab-ci-token --password $env:CI_JOB_TOKEN --store-password-in-clear-text basename=$(basename "$file")
} elseif ($env:NUGET_USERNAME -and $env:NUGET_PASSWORD) { # create release JSON using the exact minversion value as tag_name
Write-Host 'Configuring private NuGet source Nuget-GitLab-AIFotoONLUS using NUGET_USERNAME/NUGET_PASSWORD...' cat > release.json <<EOF
try { & $dotnetExe nuget remove source Nuget-GitLab-AIFotoONLUS } catch {} {"name":"$version","tag_name":"$version","ref":"$CI_COMMIT_SHA","description":"Automated release from CI (version $version)","assets":{"links":[{"name":"$basename","url":"$assetUrl"}]}}
& $dotnetExe nuget add source $nugetUrl --name Nuget-GitLab-AIFotoONLUS --username $env:NUGET_USERNAME --password $env:NUGET_PASSWORD --store-password-in-clear-text EOF
} else { echo "Creating release for tag: $version"
Write-Host 'No credentials available; skipping private NuGet source configuration.' curl --silent --show-error --header "JOB-TOKEN:$CI_JOB_TOKEN" -X POST -H "Content-Type: application/json" --data @release.json "$CI_API_V4_URL/projects/$CI_PROJECT_ID/releases"
}
# Find the produced EXE in publish folder (fallback to first file if no exe found)
$file = Get-ChildItem -Path publish -Filter *.exe -File | Select-Object -First 1
if (-not $file) { $file = Get-ChildItem -Path publish -File | Select-Object -First 1 }
Write-Host "Uploading $($file.FullName)"
# Upload to GitLab project uploads API to get a public URL for the artifact
$uploadUrl = "$env:CI_API_V4_URL/projects/$env:CI_PROJECT_ID/uploads"
$formData = "file=@$($file.FullName)"
$uploadResp = curl --silent --show-error --header "JOB-TOKEN:$env:CI_JOB_TOKEN" --form $formData $uploadUrl
$uploadJson = $uploadResp | ConvertFrom-Json
$assetUrl = "$env:CI_SERVER_URL$($uploadJson.url)"
Write-Host "Uploaded asset url: $assetUrl"
# Create the release using uploads URL
$body = @{ name = $env:CI_COMMIT_TAG; tag_name = $env:CI_COMMIT_TAG; description = "Automated release from CI"; assets = @{ links = @(@{ name = "$($file.Name)"; url = $assetUrl }) } } | ConvertTo-Json -Depth 10
Invoke-RestMethod -Method Post -Uri "$env:CI_API_V4_URL/projects/$env:CI_PROJECT_ID/releases" -Headers @{ "JOB-TOKEN" = $env:CI_JOB_TOKEN } -Body $body -ContentType "application/json"
}
artifacts: artifacts:
paths:
- publish/*.exe
expire_in: 1 day expire_in: 1 day
only: only:
- tags - tags
# Notes for runner setup: Ensure a GitLab Windows runner with tag 'windows' is registered and has .NET 10 SDK installed. # Notes for runner setup: The job now runs on the public .NET SDK image and downloads artifacts from the
# Use the shell executor on the Windows machine so the job runs in the host PowerShell environment # `build_windows` job via `needs`. It reads the `minversion` file produced by the build artifacts and
# uses that value as the release tag.