194 lines
8.1 KiB
PowerShell
194 lines
8.1 KiB
PowerShell
# 素材系统镜像构建脚本(历史/内部备用)
|
|
# 当前客户交付主线请使用根目录 deploy-registry.ps1。
|
|
# 本脚本保留测试/正式分层的旧约定,但默认 Harbor/latest 口径不再作为客户升级依据。
|
|
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[ValidateSet("dev", "test", "release")]
|
|
[string]$Environment,
|
|
|
|
[string]$Version = "",
|
|
[string]$HarborHost = "harbor.flnxi.com",
|
|
[string]$HarborProject = "flnxi",
|
|
[switch]$Push = $false,
|
|
[switch]$NoBuild = $false,
|
|
[switch]$AcknowledgeLegacyHarborPush = $false
|
|
)
|
|
|
|
# Harbor 镜像路径规划:
|
|
# ┌─────────────────────────────────────────────────────────────────┐
|
|
# │ harbor.flnxi.com/flnxi/ │
|
|
# ├─────────────────────────────────────────────────────────────────┤
|
|
# │ material/ <- 正式版(客户拉取) │
|
|
# │ ├── flnxi-material:latest <- 最新正式版 │
|
|
# │ ├── flnxi-material:1.0.0 <- 版本标签 │
|
|
# │ └── flnxi-material:1.0.1 │
|
|
# ├─────────────────────────────────────────────────────────────────┤
|
|
# │ material-dev/ <- 测试版(开发测试用) │
|
|
# │ ├── flnxi-material:latest <- 最新测试版 │
|
|
# │ ├── flnxi-material:dev-xxx <- 开发分支 │
|
|
# │ └── flnxi-material:test-xxx<- 测试版本 │
|
|
# └─────────────────────────────────────────────────────────────────┘
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
Write-Host "[WARN] build-material-image.ps1 is a legacy/internal fallback entry." -ForegroundColor Yellow
|
|
Write-Host "[WARN] Customer release, upgrade, and rollback should use root deploy-registry.ps1 with a pinned APP_IMAGE tag." -ForegroundColor Yellow
|
|
Write-Host ""
|
|
|
|
if ($Push -and (-not $AcknowledgeLegacyHarborPush)) {
|
|
Write-Host "ERROR: this script still pushes to legacy Harbor/latest targets and must not be used as the customer release entry." -ForegroundColor Red
|
|
Write-Host "For internal legacy maintenance only, add -AcknowledgeLegacyHarborPush explicitly." -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# 根据环境确定镜像路径
|
|
$ImageName = "flnxi-material"
|
|
switch ($Environment) {
|
|
"dev" {
|
|
$HarborPath = "material-dev"
|
|
$DefaultTag = "dev-$(Get-Date -Format 'yyyyMMdd-HHmm')"
|
|
}
|
|
"test" {
|
|
$HarborPath = "material-dev"
|
|
$DefaultTag = "test-$(Get-Date -Format 'yyyyMMdd')"
|
|
}
|
|
"release" {
|
|
$HarborPath = "material"
|
|
if (-not $Version) {
|
|
# 从 VERSION.txt 读取版本号
|
|
if (Test-Path "VERSION.txt") {
|
|
$Version = (Get-Content "VERSION.txt" -Raw).Trim()
|
|
} else {
|
|
Write-Host "错误: 正式版必须指定版本号,或在 VERSION.txt 中定义" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
}
|
|
$DefaultTag = $Version
|
|
}
|
|
}
|
|
|
|
$Tag = if ($Version) { $Version } else { $DefaultTag }
|
|
$FullImageName = "$HarborHost/$HarborProject/$HarborPath/${ImageName}:${Tag}"
|
|
$LatestImageName = "$HarborHost/$HarborProject/$HarborPath/${ImageName}:latest"
|
|
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "素材系统镜像构建工具" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
Write-Host "环境: $Environment" -ForegroundColor Yellow
|
|
Write-Host "版本: $Tag" -ForegroundColor Yellow
|
|
Write-Host "镜像: $FullImageName" -ForegroundColor Yellow
|
|
Write-Host "推送: $(if ($Push) { '是' } else { '否' })" -ForegroundColor Yellow
|
|
Write-Host ""
|
|
|
|
# 检查 Docker 是否可用
|
|
try {
|
|
docker version | Out-Null
|
|
} catch {
|
|
Write-Host "错误: Docker 未安装或未运行" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# 优先加载本地基础镜像(避免网络问题)
|
|
$LocalImagePath = "docker-images/node/node-18-alpine.tar"
|
|
if (Test-Path $LocalImagePath) {
|
|
Write-Host "检查本地基础镜像..." -ForegroundColor Gray
|
|
$existingImage = docker images node:18-alpine --format "{{.ID}}" 2>$null
|
|
if (-not $existingImage) {
|
|
Write-Host "加载本地基础镜像: $LocalImagePath" -ForegroundColor Cyan
|
|
docker load -i $LocalImagePath
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "本地基础镜像加载成功 ✓" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "警告: 本地基础镜像加载失败,将尝试在线拉取" -ForegroundColor Yellow
|
|
}
|
|
} else {
|
|
Write-Host "本地基础镜像已存在 ✓" -ForegroundColor Green
|
|
}
|
|
} else {
|
|
Write-Host "提示: 未找到本地基础镜像 ($LocalImagePath),将使用在线拉取" -ForegroundColor Yellow
|
|
Write-Host " 建议: 运行 'docker save node:18-alpine -o $LocalImagePath' 保存基础镜像" -ForegroundColor Gray
|
|
}
|
|
Write-Host ""
|
|
|
|
# 构建镜像
|
|
if (-not $NoBuild) {
|
|
Write-Host "开始构建镜像..." -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
# 构建参数
|
|
$buildArgs = @(
|
|
"build",
|
|
"-t", $FullImageName,
|
|
"-t", $LatestImageName,
|
|
"--build-arg", "APP_VERSION=$Tag",
|
|
"."
|
|
)
|
|
|
|
# 执行构建
|
|
$process = Start-Process -FilePath "docker" -ArgumentList $buildArgs -NoNewWindow -Wait -PassThru
|
|
|
|
if ($process.ExitCode -ne 0) {
|
|
Write-Host "构建失败!" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "构建完成 ✓" -ForegroundColor Green
|
|
Write-Host ""
|
|
}
|
|
|
|
# 推送镜像
|
|
if ($Push) {
|
|
Write-Host "检查 Harbor 登录状态..." -ForegroundColor Gray
|
|
$loginCheck = docker login $HarborHost 2>&1
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "请先登录 Harbor:" -ForegroundColor Yellow
|
|
Write-Host " docker login $HarborHost" -ForegroundColor White
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "推送镜像到 Harbor..." -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
# 推送版本标签
|
|
Write-Host "推送 $FullImageName ..." -ForegroundColor Gray
|
|
docker push $FullImageName
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "推送失败!" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# 推送 latest 标签
|
|
Write-Host "推送 $LatestImageName ..." -ForegroundColor Gray
|
|
docker push $LatestImageName
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "推送失败!" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "推送完成 ✓" -ForegroundColor Green
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "操作完成" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
Write-Host "镜像信息:" -ForegroundColor White
|
|
Write-Host " 版本标签: $FullImageName" -ForegroundColor Gray
|
|
Write-Host " 最新标签: $LatestImageName" -ForegroundColor Gray
|
|
Write-Host ""
|
|
|
|
if ($Environment -eq "release") {
|
|
Write-Host "NOTICE: release image built. Customer delivery must pin the full version image, not latest:" -ForegroundColor Yellow
|
|
Write-Host " $FullImageName" -ForegroundColor White
|
|
Write-Host "Current release/upgrade/rollback entry is root deploy-registry.ps1." -ForegroundColor Yellow
|
|
} else {
|
|
Write-Host "NOTICE: test image built under material-dev." -ForegroundColor Yellow
|
|
Write-Host " After testing, publish release with:" -ForegroundColor Gray
|
|
Write-Host " .\deploy-registry.ps1 -Action PublishRelease -Version $Tag -RegistryHost <registry-host> -RegistryPassword `$registryPassword" -ForegroundColor White
|
|
}
|