buildDockerImage_u24_docker.ps1 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # mixapi 远程构建 Docker 镜像脚本 - 国内版本
  2. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  3. # ============================================================
  4. # 配置区
  5. # ============================================================
  6. # 远程服务器配置
  7. $remoteUser = "root"
  8. $remoteHost = "192.168.1.99"
  9. $remotePort = "22"
  10. # Git 仓库配置
  11. $repoUrl = "https://gogs-apq2.zalhb.com/Apq/dockerimg"
  12. $repoDir = "dockerimg"
  13. $buildDir = "mixapi/china"
  14. # Docker 镜像配置
  15. $imageName = "registry.cn-chengdu.aliyuncs.com/apq/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 ""
  48. # 支持命令行参数覆盖默认配置
  49. if ($args.Count -ge 1) { $remoteUser = $args[0] }
  50. if ($args.Count -ge 2) { $remoteHost = $args[1] }
  51. if ($args.Count -ge 3) { $remotePort = $args[2] }
  52. Write-Host "连接到: $remoteUser@$remoteHost`:$remotePort" -ForegroundColor Yellow
  53. Write-Host ""
  54. # ============================================================
  55. # 构建远程执行的 Shell 脚本
  56. # ============================================================
  57. # 生成远程脚本内容(写入临时文件执行,避免引号嵌套问题)
  58. $scriptContent = @"
  59. #!/bin/bash
  60. cd ~
  61. # 检查并配置 QEMU 用户模式模拟(多架构构建必需)
  62. echo "检查 QEMU 用户模式模拟..."
  63. if ! docker run --rm --privileged multiarch/qemu-user-static --reset -p yes 2>/dev/null; then
  64. echo "警告: QEMU 配置失败,多架构构建可能会失败"
  65. fi
  66. # 克隆或更新仓库
  67. if [ ! -d "$repoDir" ]; then
  68. echo "目录不存在,正在克隆仓库..."
  69. git clone $repoUrl $repoDir
  70. else
  71. echo "目录已存在,跳过克隆"
  72. fi
  73. cd $repoDir
  74. # 拉取最新代码
  75. echo "正在拉取最新代码..."
  76. git pull
  77. # 进入构建目录
  78. cd $buildDir
  79. # 构建并推送 Docker 镜像
  80. echo "正在构建并推送 Docker 镜像..."
  81. docker buildx build \
  82. --platform linux/amd64,linux/arm64 \
  83. $tagParams \
  84. --cache-from type=local,src=`$HOME/.buildx-cache \
  85. --cache-to type=local,dest=`$HOME/.buildx-cache,mode=max \
  86. --push .
  87. "@
  88. # 将脚本内容转为 base64,避免特殊字符问题
  89. $scriptBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent))
  90. # tmux 命令:解码并执行脚本
  91. $remoteCmd = "tmux has-session -t $tmuxSession 2>/dev/null || tmux new-session -d -s $tmuxSession; " +
  92. "tmux send-keys -t $tmuxSession 'echo $scriptBase64 | base64 -d > /tmp/build_mixapi.sh && bash /tmp/build_mixapi.sh' Enter"
  93. # ============================================================
  94. # 执行远程命令
  95. # ============================================================
  96. Write-Host "正在发送命令到 tmux 会话: $tmuxSession" -ForegroundColor Yellow
  97. Write-Host ""
  98. & ssh -p $remotePort "$remoteUser@$remoteHost" $remoteCmd
  99. if ($LASTEXITCODE -eq 0) {
  100. Write-Host ""
  101. Write-Host "========================================" -ForegroundColor Green
  102. Write-Host " 命令已发送到 tmux 会话: $tmuxSession" -ForegroundColor Green
  103. Write-Host "========================================" -ForegroundColor Green
  104. Write-Host ""
  105. Write-Host "推送目标:" -ForegroundColor Yellow
  106. foreach ($tag in $imageTags) {
  107. Write-Host " - ${imageName}:${tag}" -ForegroundColor Cyan
  108. }
  109. Write-Host ""
  110. Write-Host "查看构建进度:" -ForegroundColor Yellow
  111. Write-Host " ssh -t -p $remotePort $remoteUser@$remoteHost `"tmux attach -t $tmuxSession`"" -ForegroundColor Cyan
  112. } else {
  113. Write-Host ""
  114. Write-Host "构建失败,请检查错误信息" -ForegroundColor Red
  115. }
  116. Write-Host ""
  117. Read-Host "按回车退出"