Configure private registry and Git repository
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
123
scripts/push-images-to-private-registry.ps1
Normal file
123
scripts/push-images-to-private-registry.ps1
Normal file
@@ -0,0 +1,123 @@
|
||||
# 推送 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 ""
|
||||
95
scripts/push-to-private-repo.ps1
Normal file
95
scripts/push-to-private-repo.ps1
Normal file
@@ -0,0 +1,95 @@
|
||||
# 推送代码到私有 Git 仓库
|
||||
# 用途:将代码推送到私有仓库(默认推送到私有仓库)
|
||||
|
||||
param(
|
||||
[string]$CommitMessage = "Update: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
|
||||
[switch]$PushToGitHub,
|
||||
[switch]$PushToBoth
|
||||
)
|
||||
|
||||
Write-Host "=== 推送代码到 Git 仓库 ===" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# 检查 Git 状态
|
||||
$status = git status --porcelain 2>$null
|
||||
if ($status) {
|
||||
Write-Host "发现更改的文件:" -ForegroundColor Cyan
|
||||
git status --short
|
||||
Write-Host ""
|
||||
|
||||
# 添加所有更改
|
||||
Write-Host "添加所有更改..." -ForegroundColor Yellow
|
||||
git add .
|
||||
Write-Host "✓ 文件已添加到暂存区" -ForegroundColor Green
|
||||
|
||||
# 提交
|
||||
Write-Host "提交更改..." -ForegroundColor Yellow
|
||||
Write-Host "提交信息: $CommitMessage" -ForegroundColor Gray
|
||||
git commit -m $CommitMessage
|
||||
Write-Host "✓ 更改已提交" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✓ 没有需要提交的更改" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# 检查远程仓库
|
||||
$privateRemote = git remote get-url private 2>$null
|
||||
$originRemote = git remote get-url origin 2>$null
|
||||
|
||||
if (-not $privateRemote -and -not $originRemote) {
|
||||
Write-Host "错误: 未配置远程仓库" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 确定推送目标
|
||||
if ($PushToBoth) {
|
||||
$pushToPrivate = $true
|
||||
$pushToOrigin = $true
|
||||
} elseif ($PushToGitHub) {
|
||||
$pushToPrivate = $false
|
||||
$pushToOrigin = $true
|
||||
} else {
|
||||
# 默认推送到私有仓库
|
||||
$pushToPrivate = $true
|
||||
$pushToOrigin = $false
|
||||
}
|
||||
|
||||
# 推送到私有仓库
|
||||
if ($pushToPrivate -and $privateRemote) {
|
||||
Write-Host ""
|
||||
Write-Host "推送到私有仓库..." -ForegroundColor Yellow
|
||||
Write-Host "远程仓库: $($privateRemote -replace '://.*@', '://****@')" -ForegroundColor Gray
|
||||
Write-Host "分支: main" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
git push private main
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "✓ 代码已成功推送到私有仓库" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✗ 推送到私有仓库失败" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
# 推送到 GitHub(如果指定)
|
||||
if ($pushToOrigin -and $originRemote) {
|
||||
Write-Host ""
|
||||
Write-Host "推送到 GitHub..." -ForegroundColor Yellow
|
||||
Write-Host "远程仓库: $($originRemote -replace '://.*@', '://****@')" -ForegroundColor Gray
|
||||
Write-Host "分支: main" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
git push origin main
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "✓ 代码已成功推送到 GitHub" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✗ 推送到 GitHub 失败" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "=== 完成 ===" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "使用说明:" -ForegroundColor Cyan
|
||||
Write-Host " - 默认推送到私有仓库: .\scripts\push-to-private-repo.ps1" -ForegroundColor Gray
|
||||
Write-Host " - 推送到 GitHub: .\scripts\push-to-private-repo.ps1 -PushToGitHub" -ForegroundColor Gray
|
||||
Write-Host " - 推送到两个仓库: .\scripts\push-to-private-repo.ps1 -PushToBoth" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
78
scripts/test-registry-connection.ps1
Normal file
78
scripts/test-registry-connection.ps1
Normal file
@@ -0,0 +1,78 @@
|
||||
# 测试私有 Registry 连接
|
||||
# 用途:验证 Docker Registry 连接和认证
|
||||
|
||||
Write-Host "=== 测试私有 Registry 连接 ===" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# 读取 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']
|
||||
|
||||
if (-not $registryUrl -or -not $registryUsername -or -not $registryPassword) {
|
||||
Write-Host "错误: Registry 配置不完整" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "配置信息:" -ForegroundColor Cyan
|
||||
Write-Host " Registry URL: $registryUrl" -ForegroundColor Gray
|
||||
Write-Host " 用户名: $registryUsername" -ForegroundColor Gray
|
||||
Write-Host " 密码: ****" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
# 测试网络连接
|
||||
Write-Host "1. 测试网络连接..." -ForegroundColor Yellow
|
||||
$testResult = Test-NetConnection -ComputerName $registryUrl -Port 443 -InformationLevel Quiet -WarningAction SilentlyContinue 2>$null
|
||||
if ($testResult) {
|
||||
Write-Host "✓ 网络连接正常" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "⚠ 网络连接可能有问题" -ForegroundColor Yellow
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
# 测试 Docker 登录
|
||||
Write-Host "2. 测试 Docker 登录..." -ForegroundColor Yellow
|
||||
$loginResult = echo $registryPassword | docker login $registryUrl -u $registryUsername --password-stdin 2>&1
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Host "✓ Docker 登录成功" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "✗ Docker 登录失败" -ForegroundColor Red
|
||||
Write-Host " 请检查:" -ForegroundColor Yellow
|
||||
Write-Host " - Registry URL 是否正确" -ForegroundColor Gray
|
||||
Write-Host " - 用户名和密码是否正确" -ForegroundColor Gray
|
||||
Write-Host " - Registry 服务是否正常运行" -ForegroundColor Gray
|
||||
exit 1
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
# 测试 Registry 访问
|
||||
Write-Host "3. 测试 Registry 访问..." -ForegroundColor Yellow
|
||||
$images = docker images 2>&1
|
||||
if ($images -match $registryUrl) {
|
||||
Write-Host "✓ Registry 访问正常" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "⚠ 无法验证 Registry 访问(可能需要先推送镜像)" -ForegroundColor Yellow
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "=== 测试完成 ===" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "Registry 连接正常,可以使用以下命令推送镜像:" -ForegroundColor Cyan
|
||||
$pushScript = ".\scripts\push-images-to-private-registry.ps1"
|
||||
Write-Host " $pushScript" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Reference in New Issue
Block a user