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/**"
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:
stage: publish
tags:
- saas-windows-medium-amd64
image: mcr.microsoft.com/dotnet/sdk:10.0
needs:
- build_windows
script:
- |
powershell -NoProfile -Command {
# Ensure .NET 10 SDK is available (install to user folder if missing) and use that dotnet for publish
$needsInstall = -not (dotnet --list-sdks 2>$null | Select-String '^10\.')
if ($needsInstall) {
Write-Host 'Installing .NET 10 SDK using dotnet-install.ps1'
Invoke-WebRequest 'https://dot.net/v1/dotnet-install.ps1' -OutFile dotnet-install.ps1 -UseBasicParsing
.\dotnet-install.ps1 -Channel 10.0 -InstallDir $env:USERPROFILE\.dotnet
$dotnetExe = Join-Path $env:USERPROFILE '.dotnet\dotnet.exe'
} else {
$dotnetExe = 'dotnet'
}
& $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
Write-Host "Published to $(pwd)\\publish"
# Configure private NuGet source from GitLab Packages using CI_JOB_TOKEN
# Fallback to repository variables NUGET_USERNAME/NUGET_PASSWORD for shared runners
$nugetUrl = 'https://gitlab.com/api/v4/projects/79509532/packages/nuget/index.json'
if ($env:CI_JOB_TOKEN) {
Write-Host 'Configuring private NuGet source Nuget-GitLab-AIFotoONLUS using CI_JOB_TOKEN...'
try { & $dotnetExe nuget remove source Nuget-GitLab-AIFotoONLUS } catch {}
& $dotnetExe nuget add source $nugetUrl --name Nuget-GitLab-AIFotoONLUS --username gitlab-ci-token --password $env:CI_JOB_TOKEN --store-password-in-clear-text
} elseif ($env:NUGET_USERNAME -and $env:NUGET_PASSWORD) {
Write-Host 'Configuring private NuGet source Nuget-GitLab-AIFotoONLUS using NUGET_USERNAME/NUGET_PASSWORD...'
try { & $dotnetExe nuget remove source Nuget-GitLab-AIFotoONLUS } catch {}
& $dotnetExe nuget add source $nugetUrl --name Nuget-GitLab-AIFotoONLUS --username $env:NUGET_USERNAME --password $env:NUGET_PASSWORD --store-password-in-clear-text
} else {
Write-Host 'No credentials available; skipping private NuGet source configuration.'
}
# 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"
}
- job: build_windows
artifacts: true
script: |
set -euo pipefail
dotnet --info
echo "Reading MinVer version from build artifacts"
# locate a minversion file produced by the build artifacts
minfile=$(find imagecatalog -type f -iname 'minversion' -print | head -n1 || true)
if [ -z "$minfile" ]; then minfile=$(find . -type f -iname 'minversion' -print | head -n1 || true); fi
if [ -z "$minfile" ]; then echo "No minversion file found in artifacts"; exit 1; fi
echo "Found minversion file: $minfile"
version=$(cat "$minfile" | tr -d '\r\n')
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)
file=$(find imagecatalog/bin -type f \! -iname 'minversion' -print | head -n1 || true)
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
echo "Uploading artifact: $file"
uploadResp=$(curl --silent --show-error --header "JOB-TOKEN:$CI_JOB_TOKEN" --form "file=@$file" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/uploads")
assetPath=$(echo "$uploadResp" | sed -n 's/.*"url":"\([^\"]*\)".*/\1/p')
if [ -z "$assetPath" ]; then echo "Upload failed: $uploadResp"; exit 1; fi
assetUrl="$CI_SERVER_URL$assetPath"
echo "Uploaded asset url: $assetUrl"
basename=$(basename "$file")
# create release JSON using the exact minversion value as tag_name
cat > release.json <<EOF
{"name":"$version","tag_name":"$version","ref":"$CI_COMMIT_SHA","description":"Automated release from CI (version $version)","assets":{"links":[{"name":"$basename","url":"$assetUrl"}]}}
EOF
echo "Creating release for tag: $version"
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"
artifacts:
paths:
- publish/*.exe
expire_in: 1 day
only:
- tags
# Notes for runner setup: Ensure a GitLab Windows runner with tag 'windows' is registered and has .NET 10 SDK installed.
# Use the shell executor on the Windows machine so the job runs in the host PowerShell environment
# Notes for runner setup: The job now runs on the public .NET SDK image and downloads artifacts from the
# `build_windows` job via `needs`. It reads the `minversion` file produced by the build artifacts and
# uses that value as the release tag.