128 lines
6.1 KiB
PowerShell
128 lines
6.1 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"
|
||
|
||
# 步骤 1:检查缓存
|
||
Write-Host "📦 步骤 1:检查缓存文件" -ForegroundColor Magenta
|
||
$cacheCheck = Invoke-BuildServer -Command "test -d /opt/build-cache/alpine-apk && test -d /opt/build-cache/node_modules && echo 'OK' || echo 'MISSING'" -Silent
|
||
if ($cacheCheck -notmatch "OK") {
|
||
Write-Host "❌ 缓存文件不完整,请先运行 setup-build-cache.ps1" -ForegroundColor Red
|
||
exit 1
|
||
}
|
||
Write-Host "✅ 缓存文件已就绪" -ForegroundColor Green
|
||
Write-Host ""
|
||
|
||
# 步骤 2:准备构建目录
|
||
Write-Host "📦 步骤 2:准备构建目录" -ForegroundColor Magenta
|
||
Invoke-BuildServer -Command "mkdir -p $RemoteBuildDir/cache" -Silent | Out-Null
|
||
|
||
# 使用硬链接复制缓存(零额外空间,瞬间完成)
|
||
Write-Host " 准备缓存文件(使用硬链接,零额外空间)..." -ForegroundColor Yellow
|
||
Invoke-BuildServer -Command @"
|
||
cd $RemoteBuildDir && \
|
||
rm -rf cache/alpine-apk cache/node_modules 2>/dev/null || true && \
|
||
mkdir -p cache && \
|
||
cp -al /opt/build-cache/alpine-apk cache/ && \
|
||
cp -al /opt/build-cache/node_modules cache/ && \
|
||
echo 'OK'
|
||
"@ -Silent | Out-Null
|
||
|
||
Upload-File -LocalPath "Dockerfile.base" -RemotePath "$RemoteBuildDir/Dockerfile"
|
||
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 ""
|