Files
StoreWeb-Publish/Publish.ps1
T

48 lines
1.7 KiB
PowerShell

param(
[ValidateNotNullOrEmpty()]
[string]$Message = ("Publish StoreWeb " + (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'))
)
$ErrorActionPreference = 'Stop'
function Invoke-Git {
& git @args
if ($LASTEXITCODE -ne 0) { throw "Git command failed: git $($args[0])" }
}
Push-Location -LiteralPath $PSScriptRoot
try {
if (!(Test-Path -LiteralPath .git) -or (Invoke-Git branch --show-current) -ne 'main') {
throw 'Run this script from the StoreWeb publish repository on main.'
}
foreach ($file in '.gitignore', 'Store.Api', 'Store.Api.dll', 'Store.Api.deps.json', 'Store.Api.runtimeconfig.json') {
if (!(Test-Path -LiteralPath $file -PathType Leaf)) { throw "Incomplete publish directory: missing $file" }
}
Invoke-Git add --all
Invoke-Git update-index --chmod=+x Store.Api
$configs = Invoke-Git ls-files -- 'appsettings.json' 'appsettings.*.json' '**/appsettings.json' '**/appsettings.*.json'
$ignored = Invoke-Git ls-files --cached --ignored --exclude-standard
if ($configs -or $ignored) { throw 'The index contains configuration or ignored files. Check git status before publishing.' }
& git diff --cached --quiet --exit-code
switch ($LASTEXITCODE) {
0 {
if ((Invoke-Git rev-parse HEAD) -eq (Invoke-Git rev-parse origin/main)) {
Write-Host 'No changes to publish.'
return
}
}
1 { Invoke-Git commit -m $Message }
default { throw 'Cannot inspect staged changes.' }
}
# A commit-based tag makes a failed push safe to retry.
$tag = 'release-' + (Invoke-Git rev-parse --short=12 HEAD)
Invoke-Git push --atomic origin main "HEAD:refs/tags/$tag"
Write-Host "Published $tag"
}
finally {
Pop-Location
}