buildDockerImage_u24_docker.ps1 4.9 KB

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