# 推送 Docker 镜像到私有 Registry # 用途:构建并推送 backend 和 admin 镜像到私有仓库 param( [string]$Tag = "latest", [switch]$BuildBackend, [switch]$BuildAdmin, [switch]$BuildAll ) # 读取 Registry 配置 $configFile = ".registry-config" if (-not (Test-Path $configFile)) { Write-Host "错误: 配置文件 $configFile 不存在" -ForegroundColor Red exit 1 } $config = @{} Get-Content $configFile | ForEach-Object { if ($_ -match '^\s*([^#=]+?)\s*=\s*(.+?)\s*$') { $key = $matches[1].Trim() $value = $matches[2].Trim() $config[$key] = $value } } $registryUrl = $config['REGISTRY_URL'] $registryUsername = $config['REGISTRY_USERNAME'] $registryPassword = $config['REGISTRY_PASSWORD'] $namespace = $config['REGISTRY_NAMESPACE'] $backendImageName = $config['BACKEND_IMAGE_NAME'] $adminImageName = $config['ADMIN_IMAGE_NAME'] if (-not $registryUrl -or -not $registryUsername -or -not $registryPassword) { Write-Host "错误: Registry 配置不完整" -ForegroundColor Red exit 1 } Write-Host "=== 推送 Docker 镜像到私有 Registry ===" -ForegroundColor Cyan Write-Host "" Write-Host "Registry: $registryUrl" -ForegroundColor Gray Write-Host "命名空间: $namespace" -ForegroundColor Gray Write-Host "标签: $Tag" -ForegroundColor Gray Write-Host "" # 登录到 Registry Write-Host "登录到 Registry..." -ForegroundColor Yellow echo $registryPassword | docker login $registryUrl -u $registryUsername --password-stdin if ($LASTEXITCODE -ne 0) { Write-Host "✗ 登录失败" -ForegroundColor Red exit 1 } Write-Host "✓ 登录成功" -ForegroundColor Green Write-Host "" # 确定要构建的镜像 $buildBackend = $BuildBackend -or $BuildAll $buildAdmin = $BuildAdmin -or $BuildAll if (-not $buildBackend -and -not $buildAdmin) { Write-Host "未指定要构建的镜像,构建所有镜像..." -ForegroundColor Yellow $buildBackend = $true $buildAdmin = $true } # 构建并推送 Backend 镜像 if ($buildBackend) { Write-Host "=== 构建 Backend 镜像 ===" -ForegroundColor Cyan $backendImage = "${registryUrl}/${namespace}/${backendImageName}:${Tag}" Write-Host "镜像名称: $backendImage" -ForegroundColor Gray Write-Host "构建镜像..." -ForegroundColor Yellow docker build -f deploy/docker/backend.Dockerfile -t $backendImage . if ($LASTEXITCODE -ne 0) { Write-Host "✗ Backend 镜像构建失败" -ForegroundColor Red exit 1 } Write-Host "✓ Backend 镜像构建成功" -ForegroundColor Green Write-Host "推送镜像..." -ForegroundColor Yellow docker push $backendImage if ($LASTEXITCODE -ne 0) { Write-Host "✗ Backend 镜像推送失败" -ForegroundColor Red exit 1 } Write-Host "✓ Backend 镜像推送成功" -ForegroundColor Green Write-Host "" } # 构建并推送 Admin 镜像 if ($buildAdmin) { Write-Host "=== 构建 Admin 镜像 ===" -ForegroundColor Cyan $adminImage = "${registryUrl}/${namespace}/${adminImageName}:${Tag}" Write-Host "镜像名称: $adminImage" -ForegroundColor Gray Write-Host "构建镜像..." -ForegroundColor Yellow docker build -f deploy/docker/admin.Dockerfile -t $adminImage . if ($LASTEXITCODE -ne 0) { Write-Host "✗ Admin 镜像构建失败" -ForegroundColor Red exit 1 } Write-Host "✓ Admin 镜像构建成功" -ForegroundColor Green Write-Host "推送镜像..." -ForegroundColor Yellow docker push $adminImage if ($LASTEXITCODE -ne 0) { Write-Host "✗ Admin 镜像推送失败" -ForegroundColor Red exit 1 } Write-Host "✓ Admin 镜像推送成功" -ForegroundColor Green Write-Host "" } Write-Host "=== 完成 ===" -ForegroundColor Green Write-Host "" Write-Host "镜像已推送到私有 Registry:" -ForegroundColor Cyan if ($buildBackend) { Write-Host " - ${registryUrl}/${namespace}/${backendImageName}:${Tag}" -ForegroundColor Gray } if ($buildAdmin) { Write-Host " - ${registryUrl}/${namespace}/${adminImageName}:${Tag}" -ForegroundColor Gray } Write-Host ""