您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

73 行
2.7KB

  1. #Requires -Version 5.1
  2. <#
  3. .SYNOPSIS
  4. Copies .env to the server, then SSHes in to pull the repo and start the container.
  5. .EXAMPLE
  6. .\docker-publish.ps1
  7. .\docker-publish.ps1 -SshKey "~/.ssh/id_rsa"
  8. #>
  9. param(
  10. [string]$SshKey = ""
  11. )
  12. Set-StrictMode -Version Latest
  13. $ErrorActionPreference = "Stop"
  14. # ---------------------------------------------------------------------------
  15. # Configuration
  16. # ---------------------------------------------------------------------------
  17. $SSH_HOST = "192.168.1.200"
  18. $SSH_USER = "root"
  19. $REPO_PATH = "/root/campaign-tracker"
  20. $REPO_URL = "https://onefortheroadgit.sytes.net/dcovington/KCI-CAMPAIGN-TRACKER.git"
  21. # ---------------------------------------------------------------------------
  22. # Helpers
  23. # ---------------------------------------------------------------------------
  24. function Write-Step([string]$msg) {
  25. Write-Host "`n==> $msg" -ForegroundColor Cyan
  26. }
  27. function Get-BaseArgs {
  28. $a = @("-o", "StrictHostKeyChecking=accept-new")
  29. if ($SshKey -ne "") { $a += @("-i", $SshKey) }
  30. return $a
  31. }
  32. # ---------------------------------------------------------------------------
  33. # Step 1 — copy .env (first password prompt)
  34. # ---------------------------------------------------------------------------
  35. Write-Step "Copying .env to $SSH_USER@$SSH_HOST"
  36. $scpArgs = Get-BaseArgs
  37. $scpArgs += ".env", "${SSH_USER}@${SSH_HOST}:${REPO_PATH}/.env"
  38. scp @scpArgs
  39. if ($LASTEXITCODE -ne 0) { Write-Error "scp failed (exit $LASTEXITCODE)." }
  40. # ---------------------------------------------------------------------------
  41. # Step 2 — deploy (second password prompt)
  42. # ---------------------------------------------------------------------------
  43. Write-Step "Deploying on $SSH_USER@$SSH_HOST"
  44. # Deploy whichever branch is currently checked out locally.
  45. $BRANCH = (git rev-parse --abbrev-ref HEAD).Trim()
  46. Write-Host "Branch: $BRANCH" -ForegroundColor Yellow
  47. # Build as a single semicolon-separated string — no newlines, no CRLF risk.
  48. $remoteCmd = "set -e; "
  49. $remoteCmd += "if [ -d '$REPO_PATH/.git' ]; then "
  50. $remoteCmd += "cd '$REPO_PATH' && git fetch origin && git checkout $BRANCH && git pull origin $BRANCH; "
  51. $remoteCmd += "else "
  52. $remoteCmd += "mkdir -p '$REPO_PATH' && git clone --branch $BRANCH '$REPO_URL' '$REPO_PATH'; "
  53. $remoteCmd += "fi; "
  54. $remoteCmd += "docker ps -aq | xargs -r docker rm -f; "
  55. $remoteCmd += "docker run --rm -v '$REPO_PATH':/app -w /app composer:latest install --no-dev --optimize-autoloader --no-interaction; "
  56. $remoteCmd += "cd '$REPO_PATH' && docker compose up -d"
  57. $sshArgs = Get-BaseArgs
  58. $sshArgs += "$SSH_USER@$SSH_HOST", $remoteCmd
  59. ssh @sshArgs
  60. if ($LASTEXITCODE -ne 0) { Write-Error "Deployment failed (exit $LASTEXITCODE)." }
  61. Write-Host "`nDone. Campaign Tracker is running on $SSH_HOST." -ForegroundColor Green

Powered by TurnKey Linux.