param( [Parameter(Position = 0)] [string]$Tag = (Get-Date -Format 'yyyyMMdd'), [switch]$SaveArchive ) $ErrorActionPreference = 'Stop' if ($Tag -notmatch '^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,79}$') { Write-Error "无效版本号: $Tag" } $Root = Split-Path -Parent $PSScriptRoot $OutDir = Join-Path $Root '.cache\docker' $Image = "harbor.fuhua91.com/game/sw2:$Tag" $Tar = Join-Path $OutDir "sw2-frontend-$Tag.tar" Write-Host "构建 $Image" $previousAttestations = [Environment]::GetEnvironmentVariable('BUILDX_NO_DEFAULT_ATTESTATIONS', 'Process') try { $env:BUILDX_NO_DEFAULT_ATTESTATIONS = '1' docker buildx version *> $null if ($LASTEXITCODE -eq 0) { docker buildx build --platform linux/amd64 --output type=docker --build-arg "VERSION=$Tag" -t $Image $Root } else { docker build --build-arg "VERSION=$Tag" -t $Image $Root } if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } } finally { [Environment]::SetEnvironmentVariable('BUILDX_NO_DEFAULT_ATTESTATIONS', $previousAttestations, 'Process') } Write-Host "推送 $Image" docker push $Image if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } if ($SaveArchive) { New-Item -ItemType Directory -Force -Path $OutDir | Out-Null if (Test-Path -LiteralPath $Tar) { throw "镜像归档已存在:$Tar" } Write-Host "导出 $Tar" docker save -o $Tar $Image if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } $Item = Get-Item -LiteralPath $Tar Write-Host ("完成 {0} {1:N1} MB" -f $Item.FullName, ($Item.Length / 1MB)) } Write-Host "Harbor 镜像:$Image"