feat: bundle multi-arch offline images into fnOS package
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
param(
|
||||
[string]$FnpackPath = ""
|
||||
[string]$FnpackPath = "",
|
||||
[switch]$SkipImageExport
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
@@ -7,6 +8,79 @@ $root = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
|
||||
$project = Join-Path $root "fnos\havline"
|
||||
$version = (Get-Content -Raw (Join-Path $root "havline\VERSION")).Trim()
|
||||
|
||||
function Resolve-DockerCli {
|
||||
if ($env:DOCKER_BIN) { return $env:DOCKER_BIN }
|
||||
$command = Get-Command docker -ErrorAction SilentlyContinue
|
||||
if ($command) { return $command.Source }
|
||||
$default = "C:\Program Files\Docker\Docker\resources\bin\docker.exe"
|
||||
if (Test-Path -LiteralPath $default) { return $default }
|
||||
throw "找不到 docker 命令"
|
||||
}
|
||||
|
||||
function Export-OfflineImage {
|
||||
param(
|
||||
[string]$DockerBin,
|
||||
[string]$BuildContext,
|
||||
[string]$Platform,
|
||||
[string]$Arch,
|
||||
[string]$Version,
|
||||
[string]$OutputDir
|
||||
)
|
||||
|
||||
$tarName = "havline-$Version-$Arch.tar"
|
||||
$tar = Join-Path $OutputDir $tarName
|
||||
$gz = Join-Path $OutputDir "havline-$Version-$Arch.tar.gz"
|
||||
|
||||
Push-Location $BuildContext
|
||||
try {
|
||||
& $DockerBin buildx build `
|
||||
--platform $Platform `
|
||||
--build-arg VERSION_FILE=VERSION `
|
||||
--provenance=false `
|
||||
--sbom=false `
|
||||
--output "type=docker,name=havline:$Version,dest=$tar" `
|
||||
.
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "导出 $Arch 镜像失败,退出码:$LASTEXITCODE"
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
Compress-GzipFile -Source $tar -Destination $gz
|
||||
|
||||
Remove-Item -LiteralPath $tar -Force
|
||||
Write-Output "镜像归档:$gz"
|
||||
}
|
||||
|
||||
function Compress-GzipFile {
|
||||
param(
|
||||
[string]$Source,
|
||||
[string]$Destination
|
||||
)
|
||||
|
||||
$sourceStream = [System.IO.File]::OpenRead($Source)
|
||||
try {
|
||||
$targetStream = [System.IO.File]::Create($Destination)
|
||||
try {
|
||||
$gzip = New-Object System.IO.Compression.GZipStream($targetStream, [System.IO.Compression.CompressionLevel]::Optimal)
|
||||
try {
|
||||
$sourceStream.CopyTo($gzip)
|
||||
}
|
||||
finally {
|
||||
$gzip.Dispose()
|
||||
}
|
||||
}
|
||||
finally {
|
||||
$targetStream.Dispose()
|
||||
}
|
||||
}
|
||||
finally {
|
||||
$sourceStream.Dispose()
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Path -LiteralPath $project)) {
|
||||
throw "找不到飞牛应用工程:$project"
|
||||
}
|
||||
@@ -24,6 +98,24 @@ if ($LASTEXITCODE -ge 8) {
|
||||
throw "复制飞牛应用工程失败,robocopy 退出码:$LASTEXITCODE"
|
||||
}
|
||||
|
||||
$imageDir = Join-Path $stage "app\docker"
|
||||
New-Item -ItemType Directory -Force $imageDir | Out-Null
|
||||
|
||||
if (-not $SkipImageExport) {
|
||||
$dockerBin = Resolve-DockerCli
|
||||
$buildContext = Join-Path $root "havline"
|
||||
Export-OfflineImage -DockerBin $dockerBin -BuildContext $buildContext -Platform "linux/amd64" -Arch "amd64" -Version $version -OutputDir $imageDir
|
||||
Export-OfflineImage -DockerBin $dockerBin -BuildContext $buildContext -Platform "linux/arm64" -Arch "arm64" -Version $version -OutputDir $imageDir
|
||||
}
|
||||
else {
|
||||
foreach ($arch in @("amd64", "arm64")) {
|
||||
$required = Join-Path $imageDir "havline-$version-$arch.tar.gz"
|
||||
if (-not (Test-Path -LiteralPath $required)) {
|
||||
throw "缺少离线镜像:$required"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Push-Location $PSScriptRoot
|
||||
try {
|
||||
& $FnpackPath build -d $stage
|
||||
@@ -35,6 +127,8 @@ try {
|
||||
New-Item -ItemType Directory -Force $dist | Out-Null
|
||||
Copy-Item -LiteralPath $output -Destination (Join-Path $dist "havline-$version.fpk") -Force
|
||||
Write-Output "FPK: $(Join-Path $dist "havline-$version.fpk")"
|
||||
$sizeMB = [math]::Round((Get-Item (Join-Path $dist "havline-$version.fpk")).Length / 1MB, 1)
|
||||
Write-Output "FPK 大小:$sizeMB MB"
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
|
||||
Reference in New Issue
Block a user