gongxiangp2/setup-build-cache.ps1

151 lines
7.9 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 由壹点3D素材 - 构建机缓存初始化脚本
# 功能:一次性准备所有缓存文件,后续构建直接使用,零解压、零网络
# 状态:旧构建缓存方案,不是当前 Git 增量部署的默认前置步骤
param(
[string]$BuildServerIP = '192.168.42.134',
[string]$BuildServerUser = 'root',
[string]$BuildServerPassword = 'Jsjj@8888'
)
# 设置编码
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 ""
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 → $RemotePath" -ForegroundColor Yellow
$result = & "C:\Program Files\PuTTY\pscp.exe" -batch -pw $BuildServerPassword $LocalPath "$BuildServerUser@$BuildServerIP`:$RemotePath" 2>&1
return $LASTEXITCODE -eq 0
}
# 步骤 1:创建缓存目录
Write-Host "📁 步骤 1:创建缓存目录" -ForegroundColor Magenta
Invoke-BuildServer -Command "mkdir -p /opt/build-cache/alpine-apk /opt/build-cache/node_modules" -Silent | Out-Null
Write-Host "✅ 缓存目录已创建" -ForegroundColor Green
Write-Host ""
# 步骤 2:处理 Alpine 包
Write-Host "📦 步骤 2:准备 Alpine 系统包" -ForegroundColor Magenta
$alpineCheck = Invoke-BuildServer -Command "ls /opt/build-cache/alpine-apk/*.apk 2>/dev/null | wc -l" -Silent
if ($alpineCheck -match "^0") {
Write-Host " Alpine 包未解压,开始处理..." -ForegroundColor Yellow
# 检查本地是否有压缩包
if (Test-Path ".build-cache/alpine-packages.tar.gz") {
Write-Host " 从本地上传..." -ForegroundColor Yellow
Upload-File -LocalPath ".build-cache/alpine-packages.tar.gz" -RemotePath "/tmp/alpine-packages.tar.gz" | Out-Null
} else {
Write-Host " ❌ 本地未找到 .build-cache/alpine-packages.tar.gz" -ForegroundColor Red
Write-Host " 请先准备此文件" -ForegroundColor Red
exit 1
}
Write-Host " 解压到缓存目录(一次性操作)..." -ForegroundColor Yellow
Invoke-BuildServer -Command "tar -xzf /tmp/alpine-packages.tar.gz -C /opt/build-cache/alpine-apk/ && rm /tmp/alpine-packages.tar.gz" -Silent | Out-Null
$apkCount = Invoke-BuildServer -Command "ls /opt/build-cache/alpine-apk/*.apk | wc -l" -Silent
Write-Host "✅ Alpine 包已解压($apkCount 个 .apk 文件)" -ForegroundColor Green
} else {
Write-Host "✅ Alpine 包已存在(跳过)" -ForegroundColor Green
}
Write-Host ""
# 步骤 3:处理 node_modules
Write-Host "📦 步骤 3:准备 node_modules" -ForegroundColor Magenta
$nodeCheck = Invoke-BuildServer -Command "test -d /opt/build-cache/node_modules/.bin && echo 'EXISTS' || echo 'MISSING'" -Silent
if ($nodeCheck -match "MISSING") {
Write-Host " node_modules 未解压,开始处理..." -ForegroundColor Yellow
# 检查本地是否有压缩包
if (Test-Path ".build-cache/node_modules.tar.gz") {
Write-Host " 从本地上传..." -ForegroundColor Yellow
Upload-File -LocalPath ".build-cache/node_modules.tar.gz" -RemotePath "/tmp/node_modules.tar.gz" | Out-Null
} else {
Write-Host " ❌ 本地未找到 .build-cache/node_modules.tar.gz" -ForegroundColor Red
Write-Host " 请先准备此文件" -ForegroundColor Red
exit 1
}
Write-Host " 解压到缓存目录(一次性操作)..." -ForegroundColor Yellow
Invoke-BuildServer -Command "tar -xzf /tmp/node_modules.tar.gz -C /opt/build-cache/ && rm /tmp/node_modules.tar.gz" -Silent | Out-Null
Write-Host " 设置权限..." -ForegroundColor Yellow
Invoke-BuildServer -Command "chmod -R +x /opt/build-cache/node_modules/.bin" -Silent | Out-Null
Write-Host "✅ node_modules 已解压" -ForegroundColor Green
} else {
Write-Host "✅ node_modules 已存在(跳过)" -ForegroundColor Green
}
Write-Host ""
# 步骤 4:加载基础镜像
Write-Host "🐳 步骤 4:准备基础镜像" -ForegroundColor Magenta
$imageCheck = Invoke-BuildServer -Command "docker images node:18-alpine --format '{{.Repository}}:{{.Tag}}'" -Silent
if ($imageCheck -notmatch "node:18-alpine") {
Write-Host " 基础镜像不存在,开始处理..." -ForegroundColor Yellow
# 检查本地是否有镜像文件
if (Test-Path ".build-cache/node-18-alpine.tar") {
Write-Host " 从本地上传..." -ForegroundColor Yellow
Upload-File -LocalPath ".build-cache/node-18-alpine.tar" -RemotePath "/tmp/node-18-alpine.tar" | Out-Null
Write-Host " 加载到 Docker..." -ForegroundColor Yellow
Invoke-BuildServer -Command "docker load -i /tmp/node-18-alpine.tar && rm /tmp/node-18-alpine.tar" -Silent | Out-Null
Write-Host "✅ 基础镜像已加载" -ForegroundColor Green
} else {
Write-Host " ⚠️ 本地未找到 .build-cache/node-18-alpine.tar" -ForegroundColor Yellow
Write-Host " 将在首次构建时自动拉取" -ForegroundColor Yellow
}
} else {
Write-Host "✅ 基础镜像已存在(跳过)" -ForegroundColor Green
}
Write-Host ""
# 步骤 5:显示缓存状态
Write-Host "📊 缓存状态总览" -ForegroundColor Magenta
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host ""
Write-Host "Alpine 包:" -ForegroundColor Yellow
Invoke-BuildServer -Command "du -sh /opt/build-cache/alpine-apk/ 2>/dev/null && ls /opt/build-cache/alpine-apk/*.apk | wc -l | xargs echo ' 文件数:'" -Silent
Write-Host ""
Write-Host "node_modules:" -ForegroundColor Yellow
Invoke-BuildServer -Command "du -sh /opt/build-cache/node_modules/ 2>/dev/null" -Silent
Write-Host ""
Write-Host "Docker 镜像:" -ForegroundColor Yellow
Invoke-BuildServer -Command "docker images node:18-alpine --format ' {{.Repository}}:{{.Tag}} - {{.Size}}'" -Silent
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 "🎉 现在可以运行 deploy-remote-build.ps1 进行部署" -ForegroundColor Cyan
Write-Host " 后续构建将直接使用这些缓存,零解压、零网络依赖" -ForegroundColor Cyan
Write-Host ""