buildDockerImage_apq2.ps1 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. # 克隆或更新仓库
  63. if [ ! -d "$repoDir" ]; then
  64. echo "目录不存在,正在克隆仓库..."
  65. git clone $repoUrl $repoDir
  66. else
  67. echo "目录已存在,跳过克隆"
  68. fi
  69. cd $repoDir
  70. # 拉取最新代码
  71. echo "正在拉取最新代码..."
  72. git pull
  73. # 进入构建目录
  74. cd $buildDir
  75. # 构建并推送 Docker 镜像(多架构)
  76. echo "正在构建并推送 Docker 镜像..."
  77. docker buildx build \
  78. --platform linux/amd64,linux/arm64 \
  79. $tagParams \
  80. --cache-from type=local,src=`$HOME/.buildx-cache \
  81. --cache-to type=local,dest=`$HOME/.buildx-cache,mode=max \
  82. --push .
  83. "@
  84. # 将脚本内容转为 base64,避免特殊字符问题
  85. $scriptBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent))
  86. # tmux 命令:解码并执行脚本
  87. $remoteCmd = "tmux has-session -t $tmuxSession 2>/dev/null || tmux new-session -d -s $tmuxSession; " +
  88. "tmux send-keys -t $tmuxSession 'echo $scriptBase64 | base64 -d > /tmp/build_mixapi.sh && bash /tmp/build_mixapi.sh' Enter"
  89. # ============================================================
  90. # 执行远程命令
  91. # ============================================================
  92. Write-Host "正在发送命令到 tmux 会话: $tmuxSession" -ForegroundColor Yellow
  93. Write-Host ""
  94. & ssh -p $remotePort "$remoteUser@$remoteHost" $remoteCmd
  95. if ($LASTEXITCODE -eq 0) {
  96. Write-Host ""
  97. Write-Host "========================================" -ForegroundColor Green
  98. Write-Host " 命令已发送到 tmux 会话: $tmuxSession" -ForegroundColor Green
  99. Write-Host "========================================" -ForegroundColor Green
  100. Write-Host ""
  101. Write-Host "推送目标:" -ForegroundColor Yellow
  102. foreach ($tag in $imageTags) {
  103. Write-Host " - ${imageName}:${tag}" -ForegroundColor Cyan
  104. }
  105. Write-Host ""
  106. Write-Host "查看构建进度:" -ForegroundColor Yellow
  107. Write-Host " ssh -t -p $remotePort $remoteUser@$remoteHost `"tmux attach -t $tmuxSession`"" -ForegroundColor Cyan
  108. } else {
  109. Write-Host ""
  110. Write-Host "构建失败,请检查错误信息" -ForegroundColor Red
  111. }
  112. Write-Host ""
  113. Read-Host "按回车退出"