-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublish.ps1
More file actions
223 lines (185 loc) · 7.15 KB
/
Copy pathPublish.ps1
File metadata and controls
223 lines (185 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Automates version bumping and publishing of SchemaMagic to NuGet.
.DESCRIPTION
This script uses Nerdbank.GitVersioning (nbgv) to automatically determine the version,
build the package, create a git tag, and optionally publish to NuGet.
.PARAMETER PublishToNuGet
If specified, publishes the package to NuGet.org after building.
.PARAMETER ApiKey
NuGet API key for publishing. Can also be set via NUGET_API_KEY environment variable.
.PARAMETER DryRun
If specified, performs all steps except actual publishing and git push.
.EXAMPLE
.\Publish.ps1
# Builds package and creates local tag
.EXAMPLE
.\Publish.ps1 -PublishToNuGet -ApiKey "your-api-key"
# Builds, tags, and publishes to NuGet
.EXAMPLE
.\Publish.ps1 -DryRun
# Simulates the publish process without making changes
#>
[CmdletBinding()]
param(
[switch]$PublishToNuGet,
[string]$ApiKey,
[switch]$DryRun
)
$ErrorActionPreference = "Stop"
# Colors for output
function Write-Header { Write-Host "?? $args" -ForegroundColor Cyan }
function Write-Success { Write-Host "? $args" -ForegroundColor Green }
function Write-Info { Write-Host "?? $args" -ForegroundColor Blue }
function Write-Warning { Write-Host "?? $args" -ForegroundColor Yellow }
function Write-Error { Write-Host "? $args" -ForegroundColor Red }
Write-Header "SchemaMagic Publishing Script"
Write-Host "==========================================" -ForegroundColor Cyan
# Get version from Nerdbank.GitVersioning via the project's MSBuild targets (the
# referenced NuGet package), so this does not depend on the global 'nbgv' CLI tool.
Write-Info "Determining version from git history..."
$project = Join-Path $PSScriptRoot 'SchemaMagic/SchemaMagic.csproj'
$buildOutput = dotnet build $project -t:GetBuildVersion --getProperty:NuGetPackageVersion -nologo -v:quiet -p:TreatWarningsAsErrors=false
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to determine version from Nerdbank.GitVersioning"
exit 1
}
$version = ($buildOutput | Select-Object -Last 1).ToString().Trim()
$fullVersion = $version
Write-Success "Version determined: $fullVersion"
# Check for uncommitted changes
Write-Info "Checking for uncommitted changes..."
$gitStatus = git status --porcelain
if ($gitStatus -and -not $DryRun) {
Write-Error "Uncommitted changes detected. Please commit or stash your changes first."
Write-Host "Run 'git status' to see uncommitted changes."
exit 1
}
Write-Success "Working directory is clean"
# Create tag name
$tagName = "v$version"
Write-Info "Tag will be: $tagName"
# Check if tag already exists
$existingTag = git tag -l $tagName
if ($existingTag) {
Write-Warning "Tag $tagName already exists locally"
$remoteTag = git ls-remote --tags origin $tagName
if ($remoteTag) {
Write-Error "Tag $tagName already exists on remote. Version has already been published."
Write-Info "To publish a new version, update version.json or make additional commits."
exit 1
}
}
# Clean previous builds
Write-Info "Cleaning previous builds..."
if (Test-Path "SchemaMagic/bin") {
Remove-Item "SchemaMagic/bin" -Recurse -Force
}
if (Test-Path "SchemaMagic/obj") {
Remove-Item "SchemaMagic/obj" -Recurse -Force
}
if (Test-Path "SchemaMagic/nupkg") {
Remove-Item "SchemaMagic/nupkg" -Recurse -Force
}
Write-Success "Build directories cleaned"
# Build the package
Write-Header "Building Package"
Write-Info "Building SchemaMagic v$fullVersion..."
dotnet pack SchemaMagic/SchemaMagic.csproj `
-c Release `
-o SchemaMagic/nupkg `
/p:PackageVersion=$fullVersion `
/p:Version=$fullVersion
if ($LASTEXITCODE -ne 0) {
Write-Error "Package build failed"
exit 1
}
Write-Success "Package built successfully"
# List generated packages
$packages = Get-ChildItem "SchemaMagic/nupkg" -Filter "*.nupkg"
Write-Info "Generated packages:"
foreach ($pkg in $packages) {
$sizeKB = [math]::Round($pkg.Length / 1KB, 2)
Write-Host " ?? $($pkg.Name) ($sizeKB KB)" -ForegroundColor White
}
# Create git tag
if ($DryRun) {
Write-Warning "[DRY RUN] Would create tag: $tagName"
} else {
Write-Info "Creating git tag: $tagName..."
git tag -a $tagName -m "Release version $version"
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to create git tag"
exit 1
}
Write-Success "Git tag created: $tagName"
# Push tag to remote to trigger CI/CD
Write-Info "Pushing tag to remote (this will trigger CI/CD pipeline)..."
git push origin $tagName
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to push tag to remote"
Write-Warning "You may need to push manually: git push origin $tagName"
exit 1
}
Write-Success "Tag pushed to remote - CI/CD pipeline will start automatically"
Write-Info "View pipeline at: https://github.com/panoramicdata/SchemaMagic/actions"
}
# Publish to NuGet if requested
if ($PublishToNuGet) {
Write-Header "Publishing to NuGet"
# Get API key
if (-not $ApiKey) {
$ApiKey = $env:NUGET_API_KEY
}
if (-not $ApiKey) {
Write-Error "NuGet API key not provided. Use -ApiKey parameter or set NUGET_API_KEY environment variable."
exit 1
}
$nupkgFile = Get-ChildItem "SchemaMagic/nupkg" -Filter "SchemaMagic.$fullVersion.nupkg" | Select-Object -First 1
if (-not $nupkgFile) {
Write-Error "Package file not found: SchemaMagic.$fullVersion.nupkg"
exit 1
}
if ($DryRun) {
Write-Warning "[DRY RUN] Would publish package: $($nupkgFile.Name)"
} else {
Write-Info "Publishing $($nupkgFile.Name) to NuGet.org..."
dotnet nuget push $nupkgFile.FullName `
--api-key $ApiKey `
--source https://api.nuget.org/v3/index.json `
--skip-duplicate
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to publish package to NuGet"
exit 1
}
Write-Success "Package published to NuGet.org"
Write-Info "View at: https://www.nuget.org/packages/SchemaMagic/$version"
}
} else {
Write-Info "Skipping NuGet publish (use -PublishToNuGet to publish)"
Write-Warning "Note: CI/CD pipeline will handle NuGet publishing automatically"
}
# Summary
Write-Header "Summary"
Write-Success "Version: $fullVersion"
Write-Success "Tag: $tagName"
Write-Success "Package: SchemaMagic/nupkg/SchemaMagic.$fullVersion.nupkg"
if ($DryRun) {
Write-Warning "DRY RUN - No changes were made"
} else {
Write-Success "Tag pushed to remote - CI/CD will now:"
Write-Info " 1. Run all tests"
Write-Info " 2. Build and publish to NuGet"
Write-Info " 3. Create GitHub Release"
Write-Info " 4. Deploy web application"
Write-Info ""
Write-Info "Monitor progress at: https://github.com/panoramicdata/SchemaMagic/actions"
if ($PublishToNuGet) {
Write-Warning "You used -PublishToNuGet flag, but CI/CD will also publish."
Write-Warning "This may result in duplicate publish attempts (harmless with --skip-duplicate)"
}
}
Write-Host ""
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "?? Publish process complete!" -ForegroundColor Green