buildDockerImage_apq2.ps1 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # mixapi 远程构建 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/user/dockerimg"
  12. $repoDir = "dockerimg"
  13. $buildDir = "mixapi/overseas"
  14. # Docker 镜像配置
  15. $imageName = "amwpfiqvy/mixapi"
  16. # tmux 会话名
  17. $tmuxSession = "dbx"
  18. # ============================================================
  19. # 主程序
  20. # ============================================================
  21. Write-Host ""
  22. Write-Host "========================================" -ForegroundColor Cyan
  23. Write-Host " mixapi 远程构建 Docker 镜像 (海外版)" -ForegroundColor Cyan
  24. Write-Host "========================================" -ForegroundColor Cyan
  25. Write-Host ""
  26. # Docker 镜像标签:提示用户输入(默认包含 latest,-l 排除 latest)
  27. Write-Host "请输入额外的 Docker 镜像标签 (多个用空格分隔,-l=排除latest,留空则仅推送 latest): " -ForegroundColor Magenta -NoNewline
  28. $imageTagInput = Read-Host
  29. $inputTags = @()
  30. $excludeLatest = $false
  31. if ($imageTagInput -ne "") {
  32. $inputTags = $imageTagInput -split '\s+' | Where-Object { $_ -ne "" }
  33. if ($inputTags -contains "-l") {
  34. $excludeLatest = $true
  35. $inputTags = $inputTags | Where-Object { $_ -ne "-l" }
  36. }
  37. }
  38. # 构建最终标签列表
  39. if ($excludeLatest) {
  40. $imageTags = $inputTags
  41. } else {
  42. $imageTags = @("latest") + $inputTags | Select-Object -Unique
  43. }
  44. # 构建 -t 参数列表
  45. $tagParams = ($imageTags | ForEach-Object { "-t ${imageName}:$_" }) -join " "
  46. Write-Host "镜像标签: $($imageTags -join ', ')" -ForegroundColor Yellow
  47. Write-Host "目标架构: linux/amd64, linux/arm64" -ForegroundColor Yellow
  48. Write-Host ""
  49. # 支持命令行参数覆盖默认配置
  50. if ($args.Count -ge 1) { $remoteUser = $args[0] }
  51. if ($args.Count -ge 2) { $remoteHost = $args[1] }
  52. if ($args.Count -ge 3) { $remotePort = $args[2] }
  53. Write-Host "连接到: $remoteUser@$remoteHost`:$remotePort" -ForegroundColor Yellow
  54. Write-Host ""
  55. # ============================================================
  56. # 构建远程执行的 Shell 脚本
  57. # ============================================================
  58. # 生成远程脚本内容(写入临时文件执行,避免引号嵌套问题)
  59. $scriptContent = @"
  60. #!/bin/bash
  61. cd ~
  62. # 检查并配置 QEMU 用户模式模拟(多架构构建必需)
  63. echo "检查 QEMU 用户模式模拟..."
  64. if ! docker run --rm --privileged multiarch/qemu-user-static --reset -p yes 2>/dev/null; then
  65. echo "警告: QEMU 配置失败,多架构构建可能会失败"
  66. fi
  67. # 克隆或更新仓库
  68. if [ ! -d "$repoDir" ]; then
  69. echo "目录不存在,正在克隆仓库..."
  70. git clone $repoUrl $repoDir
  71. else
  72. echo "目录已存在,跳过克隆"
  73. fi
  74. cd $repoDir
  75. # 拉取最新代码
  76. echo "正在拉取最新代码..."
  77. git pull
  78. # 进入构建目录
  79. cd $buildDir
  80. # 构建并推送 Docker 镜像(多架构)
  81. echo "正在构建并推送 Docker 镜像..."
  82. docker buildx build \
  83. --platform linux/amd64,linux/arm64 \
  84. $tagParams \
  85. --cache-from type=local,src=`$HOME/.buildx-cache \
  86. --cache-to type=local,dest=`$HOME/.buildx-cache,mode=max \
  87. --push .
  88. "@
  89. # 将脚本内容转为 base64,避免特殊字符问题
  90. $scriptBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent))
  91. # tmux 命令:解码并执行脚本
  92. $remoteCmd = "tmux has-session -t $tmuxSession 2>/dev/null || tmux new-session -d -s $tmuxSession; " +
  93. "tmux send-keys -t $tmuxSession 'echo $scriptBase64 | base64 -d > /tmp/build_mixapi.sh && bash /tmp/build_mixapi.sh' Enter"
  94. # ============================================================
  95. # 执行远程命令
  96. # ============================================================
  97. Write-Host "正在发送命令到 tmux 会话: $tmuxSession" -ForegroundColor Yellow
  98. Write-Host ""
  99. & ssh -p $remotePort "$remoteUser@$remoteHost" $remoteCmd
  100. if ($LASTEXITCODE -eq 0) {
  101. Write-Host ""
  102. Write-Host "========================================" -ForegroundColor Green
  103. Write-Host " 命令已发送到 tmux 会话: $tmuxSession" -ForegroundColor Green
  104. Write-Host "========================================" -ForegroundColor Green
  105. Write-Host ""
  106. Write-Host "推送目标:" -ForegroundColor Yellow
  107. foreach ($tag in $imageTags) {
  108. Write-Host " - ${imageName}:${tag}" -ForegroundColor Cyan
  109. }
  110. Write-Host ""
  111. Write-Host "查看构建进度:" -ForegroundColor Yellow
  112. Write-Host " ssh -t -p $remotePort $remoteUser@$remoteHost `"tmux attach -t $tmuxSession`"" -ForegroundColor Cyan
  113. } else {
  114. Write-Host ""
  115. Write-Host "构建失败,请检查错误信息" -ForegroundColor Red
  116. }
  117. Write-Host ""
  118. Read-Host "按回车退出"