buildDockerImage_apq2.ps1 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Cognio 远程构建 Docker 镜像脚本(海外服务器版本,无国内加速)
  2. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  3. # ============================================================
  4. # 配置区
  5. # ============================================================
  6. # 远程服务器配置
  7. $remoteUser = "root"
  8. $remoteHost = "vps-apq2.zalhb.com"
  9. $remotePort = "22"
  10. # Git 仓库配置
  11. $repoUrl = "https://github.com/0xReLogic/Cognio.git"
  12. $repoDir = "Cognio"
  13. # Docker 镜像配置
  14. $imageName = "amwpfiqvy/cognio"
  15. # tmux 会话名
  16. $tmuxSession = "dbx"
  17. # ============================================================
  18. # 主程序
  19. # ============================================================
  20. Write-Host ""
  21. Write-Host "========================================" -ForegroundColor Cyan
  22. Write-Host " Cognio 远程构建 Docker 镜像" -ForegroundColor Cyan
  23. Write-Host "========================================" -ForegroundColor Cyan
  24. Write-Host ""
  25. # Docker 镜像标签:提示用户输入(默认包含 latest,-l 排除 latest)
  26. $imageTagInput = Read-Host "请输入额外的 Docker 镜像标签 (多个用空格分隔,-l=排除latest,留空则仅推送 latest)"
  27. $inputTags = @()
  28. $excludeLatest = $false
  29. if ($imageTagInput -ne "") {
  30. $inputTags = $imageTagInput -split '\s+' | Where-Object { $_ -ne "" }
  31. if ($inputTags -contains "-l") {
  32. $excludeLatest = $true
  33. $inputTags = $inputTags | Where-Object { $_ -ne "-l" }
  34. }
  35. }
  36. # 构建最终标签列表
  37. if ($excludeLatest) {
  38. $imageTags = $inputTags
  39. } else {
  40. $imageTags = @("latest") + $inputTags | Select-Object -Unique
  41. }
  42. # 构建 -t 参数列表
  43. $tagParams = ($imageTags | ForEach-Object { "-t ${imageName}:$_" }) -join " "
  44. Write-Host "镜像标签: $($imageTags -join ', ')" -ForegroundColor Yellow
  45. Write-Host ""
  46. # 支持命令行参数覆盖默认配置
  47. if ($args.Count -ge 1) { $remoteUser = $args[0] }
  48. if ($args.Count -ge 2) { $remoteHost = $args[1] }
  49. if ($args.Count -ge 3) { $remotePort = $args[2] }
  50. Write-Host "连接到: $remoteUser@$remoteHost`:$remotePort" -ForegroundColor Yellow
  51. Write-Host ""
  52. # ============================================================
  53. # 构建远程执行的 Shell 脚本
  54. # ============================================================
  55. # 生成远程脚本内容(写入临时文件执行,避免引号嵌套问题)
  56. $scriptContent = @"
  57. #!/bin/bash
  58. cd ~
  59. if [ -d "$repoDir" ]; then
  60. echo "目录已存在,还原本地修改并拉取最新代码..."
  61. cd $repoDir
  62. git checkout .
  63. git pull
  64. else
  65. echo "目录不存在,正在克隆仓库..."
  66. git clone $repoUrl
  67. cd $repoDir
  68. fi
  69. echo "正在构建并推送 Docker 镜像..."
  70. docker buildx build \
  71. --platform linux/amd64,linux/arm64 \
  72. $tagParams \
  73. --cache-from type=local,src=`$HOME/.buildx-cache \
  74. --cache-to type=local,dest=`$HOME/.buildx-cache,mode=max \
  75. --push \
  76. .
  77. "@
  78. # 将脚本内容转为 base64,避免特殊字符问题
  79. $scriptBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent))
  80. # tmux 命令:解码并执行脚本
  81. $remoteCmd = "tmux has-session -t $tmuxSession 2>/dev/null || tmux new-session -d -s $tmuxSession; " +
  82. "tmux send-keys -t $tmuxSession 'echo $scriptBase64 | base64 -d > /tmp/build_cognio.sh && bash /tmp/build_cognio.sh' Enter"
  83. # ============================================================
  84. # 执行远程命令
  85. # ============================================================
  86. Write-Host "正在发送命令到 tmux 会话: $tmuxSession" -ForegroundColor Yellow
  87. Write-Host ""
  88. & ssh -p $remotePort "$remoteUser@$remoteHost" $remoteCmd
  89. if ($LASTEXITCODE -eq 0) {
  90. Write-Host ""
  91. Write-Host "========================================" -ForegroundColor Green
  92. Write-Host " 命令已发送到 tmux 会话: $tmuxSession" -ForegroundColor Green
  93. Write-Host "========================================" -ForegroundColor Green
  94. Write-Host ""
  95. Write-Host "推送目标:" -ForegroundColor Yellow
  96. foreach ($tag in $imageTags) {
  97. Write-Host " - ${imageName}:${tag}" -ForegroundColor Cyan
  98. }
  99. Write-Host ""
  100. Write-Host "查看构建进度:" -ForegroundColor Yellow
  101. Write-Host " ssh -t -p $remotePort $remoteUser@$remoteHost `"tmux attach -t $tmuxSession`"" -ForegroundColor Cyan
  102. } else {
  103. Write-Host ""
  104. Write-Host "部署失败,请检查错误信息" -ForegroundColor Red
  105. }
  106. Write-Host ""
  107. pause