- .NET 9 SDK installed
- NuGet API key saved in
.secrets/nuget-api-key.txt - Git repository with clean working directory
# Read API key from file
$API_KEY = Get-Content ".secrets/nuget-api-key.txt" -Raw
$API_KEY = $API_KEY.Trim()
# Get current version
nbgv get-version
# Build and pack
dotnet restore
dotnet build --configuration Release
dotnet pack SchemaMagic/SchemaMagic.csproj --configuration Release --output ./nupkg
# Publish to NuGet
dotnet nuget push ./nupkg/*.nupkg `
--api-key $API_KEY `
--source https://api.nuget.org/v3/index.json `
--skip-duplicate# Clean previous builds
dotnet clean
# Restore dependencies
dotnet restore# Run all tests
dotnet test --configuration Release# Build in Release mode
dotnet build --configuration Release# Install nbgv if not already installed
dotnet tool install -g nbgv
# Check current version
nbgv get-version
# Get version details
nbgv get-version -v SemVer2# Create NuGet package
dotnet pack SchemaMagic/SchemaMagic.csproj `
--configuration Release `
--output ./nupkg `
--no-build# List generated packages
Get-ChildItem ./nupkg
# Verify package contents
nuget verify -All ./nupkg/*.nupkg# Load API key
$API_KEY = (Get-Content ".secrets/nuget-api-key.txt").Trim()
# Publish package
dotnet nuget push ./nupkg/*.nupkg `
--api-key $API_KEY `
--source https://api.nuget.org/v3/index.json `
--skip-duplicate
# Or publish specific version
dotnet nuget push "./nupkg/SchemaMagic.1.0.21.nupkg" `
--api-key $API_KEY `
--source https://api.nuget.org/v3/index.json# Get the version
$VERSION = nbgv get-version -v SemVer2
# Create annotated tag
git tag -a "v$VERSION" -m "Release v$VERSION"
# Push tag to GitHub
git push origin "v$VERSION"# Install from local nupkg
dotnet tool install -g SchemaMagic --add-source ./nupkg --version 1.0.21
# Test the tool
schemamagic --version
schemamagic --help
# Uninstall after testing
dotnet tool uninstall -g SchemaMagicIf you have a test NuGet feed (like Azure Artifacts):
# Add test source
dotnet nuget add source "https://pkgs.dev.azure.com/yourorg/_packaging/test/nuget/v3/index.json" `
--name "TestFeed" `
--username "user" `
--password "pat_token" `
--store-password-in-clear-text
# Push to test feed
dotnet nuget push ./nupkg/*.nupkg `
--api-key "az" `
--source "TestFeed"# Use --skip-duplicate to ignore if version exists
dotnet nuget push ./nupkg/*.nupkg `
--api-key $API_KEY `
--source https://api.nuget.org/v3/index.json `
--skip-duplicate- Verify key in
.secrets/nuget-api-key.txthas no extra spaces - Check key hasn't expired at https://www.nuget.org/account/apikeys
- Ensure key has "Push" permissions
- NuGet.org does NOT allow re-publishing the same version
- Increment version in
version.jsonor make a new commit - Use
--skip-duplicateto continue if version exists
# Full clean and rebuild
dotnet clean
Remove-Item -Recurse -Force ./nupkg
dotnet restore
dotnet build --configuration Release
dotnet pack SchemaMagic/SchemaMagic.csproj --configuration Release --output ./nupkgSave this as publish-manual.ps1:
#Requires -Version 7.0
param(
[switch]$DryRun,
[switch]$SkipTests,
[string]$ApiKeyFile = ".secrets/nuget-api-key.txt"
)
Write-Host "?? SchemaMagic Manual Publish Script" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
# Check API key file exists
if (-not (Test-Path $ApiKeyFile)) {
Write-Host "? API key file not found: $ApiKeyFile" -ForegroundColor Red
exit 1
}
# Load API key
$API_KEY = (Get-Content $ApiKeyFile -Raw).Trim()
if ([string]::IsNullOrWhiteSpace($API_KEY)) {
Write-Host "? API key file is empty" -ForegroundColor Red
exit 1
}
# Get version
Write-Host "?? Getting version..." -ForegroundColor Yellow
$VERSION = nbgv get-version -v SemVer2
Write-Host "? Version: $VERSION" -ForegroundColor Green
# Clean
Write-Host "?? Cleaning..." -ForegroundColor Yellow
dotnet clean
Remove-Item -Recurse -Force ./nupkg -ErrorAction SilentlyContinue
# Restore
Write-Host "?? Restoring dependencies..." -ForegroundColor Yellow
dotnet restore
# Run tests
if (-not $SkipTests) {
Write-Host "?? Running tests..." -ForegroundColor Yellow
dotnet test --configuration Release
if ($LASTEXITCODE -ne 0) {
Write-Host "? Tests failed" -ForegroundColor Red
exit 1
}
}
# Build
Write-Host "?? Building..." -ForegroundColor Yellow
dotnet build --configuration Release --no-restore
# Pack
Write-Host "?? Packing NuGet package..." -ForegroundColor Yellow
dotnet pack SchemaMagic/SchemaMagic.csproj `
--configuration Release `
--output ./nupkg `
--no-build
# List packages
Write-Host "?? Generated packages:" -ForegroundColor Yellow
Get-ChildItem ./nupkg/*.nupkg | ForEach-Object {
Write-Host " $_" -ForegroundColor Cyan
}
if ($DryRun) {
Write-Host "?? DRY RUN - Skipping publish" -ForegroundColor Yellow
Write-Host "? Build successful! Package ready at ./nupkg/" -ForegroundColor Green
exit 0
}
# Publish
Write-Host "?? Publishing to NuGet.org..." -ForegroundColor Yellow
dotnet nuget push ./nupkg/*.nupkg `
--api-key $API_KEY `
--source https://api.nuget.org/v3/index.json `
--skip-duplicate
if ($LASTEXITCODE -eq 0) {
Write-Host "? Published successfully!" -ForegroundColor Green
Write-Host "?? Package: https://www.nuget.org/packages/SchemaMagic/$VERSION" -ForegroundColor Cyan
Write-Host ""
Write-Host "?? Next steps:" -ForegroundColor Yellow
Write-Host " 1. Create git tag: git tag -a v$VERSION -m 'Release v$VERSION'" -ForegroundColor White
Write-Host " 2. Push tag: git push origin v$VERSION" -ForegroundColor White
Write-Host " 3. Wait 5-10 minutes for NuGet indexing" -ForegroundColor White
Write-Host " 4. Test: dotnet tool update -g SchemaMagic" -ForegroundColor White
} else {
Write-Host "? Publish failed" -ForegroundColor Red
exit 1
}Usage:
# Test build without publishing
.\publish-manual.ps1 -DryRun
# Skip tests and publish
.\publish-manual.ps1 -SkipTests
# Full build and publish
.\publish-manual.ps1# Wait 5-10 minutes for NuGet indexing, then:
# Search for package
dotnet tool search SchemaMagic
# Install latest version
dotnet tool install -g SchemaMagic
# Or update existing
dotnet tool update -g SchemaMagic
# Verify installation
schemamagic --version- ? Always test locally first with
--add-source ./nupkg - ? Run tests before publishing to ensure quality
- ? Use semantic versioning (MAJOR.MINOR.PATCH)
- ? Create git tag after successful publish
- ? Document changes in CHANGELOG.md
- ? Never commit API keys - keep them in .gitignored files
- ? Use --skip-duplicate to avoid errors on re-runs
- ? Wait for indexing before testing installation (5-10 min)
- NuGet Package: https://www.nuget.org/packages/SchemaMagic
- API Keys Management: https://www.nuget.org/account/apikeys
- NuGet Documentation: https://docs.microsoft.com/en-us/nuget/
- Nerdbank.GitVersioning: https://github.com/dotnet/Nerdbank.GitVersioning