114 lines
5.7 KiB
PowerShell
114 lines
5.7 KiB
PowerShell
# 由壹点3D素材 - 构建基础镜像脚本
|
||
# 功能:一次性构建包含所有依赖的基础镜像
|
||
# 用途:当依赖变化时运行,后续部署直接使用此镜像
|
||
# 状态:旧构建缓存方案,不是当前 Git 增量部署的默认前置步骤
|
||
|
||
param(
|
||
[string]$BuildServerIP = '192.168.40.206',
|
||
[string]$BuildServerUser = 'root',
|
||
[string]$BuildServerPassword = $env:BUILD_SERVER_PASSWORD,
|
||
[string]$ImageName = 'youyidian-3d-base',
|
||
[string]$ImageTag = 'latest'
|
||
)
|
||
|
||
# 设置编码
|
||
try {
|
||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||
$OutputEncoding = [System.Text.Encoding]::UTF8
|
||
chcp 65001 | Out-Null
|
||
} catch {}
|
||
|
||
Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
|
||
Write-Host "║ ║" -ForegroundColor Cyan
|
||
Write-Host "║ 由壹点3D素材 - 构建基础镜像 ║" -ForegroundColor Cyan
|
||
Write-Host "║ ║" -ForegroundColor Cyan
|
||
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
if ([string]::IsNullOrWhiteSpace($BuildServerPassword)) {
|
||
throw '缺少构建机密码,请先设置 BUILD_SERVER_PASSWORD 环境变量'
|
||
}
|
||
|
||
function Invoke-BuildServer {
|
||
param([string]$Command, [switch]$Silent)
|
||
$result = & "C:\Program Files\PuTTY\plink.exe" -batch -pw $BuildServerPassword "$BuildServerUser@$BuildServerIP" $Command 2>&1
|
||
if (-not $Silent) { $result | ForEach-Object { Write-Host " $_" } }
|
||
return $result
|
||
}
|
||
|
||
function Upload-File {
|
||
param([string]$LocalPath, [string]$RemotePath)
|
||
Write-Host " 上传:$LocalPath" -ForegroundColor Yellow
|
||
& "C:\Program Files\PuTTY\pscp.exe" -batch -pw $BuildServerPassword $LocalPath "$BuildServerUser@$BuildServerIP`:$RemotePath" 2>&1 | Out-Null
|
||
}
|
||
|
||
$RemoteBuildDir = "/opt/youyidian-3d-base-build"
|
||
|
||
# 历史 Node 18 缓存不能跨 ABI 复用;由当前锁文件重新安装原生依赖。
|
||
Write-Host "📦 步骤 1:核对依赖锁文件" -ForegroundColor Magenta
|
||
if (!(Test-Path 'package-lock.json')) { throw '缺少 package-lock.json,拒绝构建' }
|
||
Write-Host ""
|
||
|
||
# 步骤 2:准备构建目录
|
||
Write-Host "📦 步骤 2:准备构建目录" -ForegroundColor Magenta
|
||
Invoke-BuildServer -Command "mkdir -p $RemoteBuildDir" -Silent | Out-Null
|
||
|
||
Upload-File -LocalPath "Dockerfile.base" -RemotePath "$RemoteBuildDir/Dockerfile"
|
||
Upload-File -LocalPath "package.json" -RemotePath "$RemoteBuildDir/package.json"
|
||
Upload-File -LocalPath "package-lock.json" -RemotePath "$RemoteBuildDir/package-lock.json"
|
||
Upload-File -LocalPath "prisma/schema.prisma" -RemotePath "$RemoteBuildDir/schema.prisma"
|
||
|
||
Invoke-BuildServer -Command "mkdir -p $RemoteBuildDir/prisma && mv $RemoteBuildDir/schema.prisma $RemoteBuildDir/prisma/" -Silent | Out-Null
|
||
|
||
Write-Host "✅ 构建目录已准备" -ForegroundColor Green
|
||
Write-Host ""
|
||
|
||
# 步骤 3:构建基础镜像
|
||
Write-Host "🔨 步骤 3:构建基础镜像" -ForegroundColor Magenta
|
||
Write-Host " 镜像名称:$ImageName`:$ImageTag" -ForegroundColor Cyan
|
||
Write-Host " 预计时间:1-2 分钟" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
$buildCmd = @"
|
||
cd $RemoteBuildDir && \
|
||
export DOCKER_BUILDKIT=1 && \
|
||
docker build -t $ImageName`:$ImageTag -f Dockerfile . 2>&1
|
||
"@
|
||
|
||
Invoke-BuildServer -Command $buildCmd
|
||
|
||
$imageCheck = Invoke-BuildServer -Command "docker images $ImageName`:$ImageTag --format '{{.Repository}}:{{.Tag}}'" -Silent
|
||
if ($imageCheck -match "$ImageName`:$ImageTag") {
|
||
$imageSize = Invoke-BuildServer -Command "docker images $ImageName`:$ImageTag --format '{{.Size}}'" -Silent
|
||
Write-Host ""
|
||
Write-Host "✅ 基础镜像构建完成(大小:$imageSize)" -ForegroundColor Green
|
||
} else {
|
||
Write-Host ""
|
||
Write-Host "❌ 基础镜像构建失败" -ForegroundColor Red
|
||
exit 1
|
||
}
|
||
Write-Host ""
|
||
|
||
# 步骤 4:保存构建哈希
|
||
Write-Host "📝 步骤 4:保存依赖哈希" -ForegroundColor Magenta
|
||
if (Test-Path "package-lock.json") {
|
||
$localHash = (Get-FileHash "package-lock.json" -Algorithm MD5).Hash
|
||
Invoke-BuildServer -Command "echo '$localHash' > /opt/build-cache/.base-image-hash" -Silent | Out-Null
|
||
Write-Host "✅ 依赖哈希已保存:$localHash" -ForegroundColor Green
|
||
}
|
||
Write-Host ""
|
||
|
||
# 完成
|
||
Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Green
|
||
Write-Host "║ ║" -ForegroundColor Green
|
||
Write-Host "║ ✅ 基础镜像构建完成! ║" -ForegroundColor Green
|
||
Write-Host "║ ║" -ForegroundColor Green
|
||
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Green
|
||
Write-Host ""
|
||
Write-Host "🎉 后续部署将直接使用此基础镜像" -ForegroundColor Cyan
|
||
Write-Host " 包含:系统包 + node_modules + Prisma Client" -ForegroundColor Cyan
|
||
Write-Host " 构建速度:从 2-3 分钟 → 30-60 秒" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
Write-Host "💡 提示:只有在依赖变化时才需要重新构建基础镜像" -ForegroundColor Yellow
|
||
Write-Host ""
|