|
- #Requires -Version 5.1
- <#
- .SYNOPSIS
- Copies .env to the server, then SSHes in to pull the repo and start the container.
- .EXAMPLE
- .\docker-publish.ps1
- .\docker-publish.ps1 -SshKey "~/.ssh/id_rsa"
- #>
-
- param(
- [string]$SshKey = ""
- )
-
- Set-StrictMode -Version Latest
- $ErrorActionPreference = "Stop"
-
- # ---------------------------------------------------------------------------
- # Configuration
- # ---------------------------------------------------------------------------
- $SSH_HOST = "192.168.1.200"
- $SSH_USER = "root"
- $REPO_PATH = "/root/campaign-tracker"
- $REPO_URL = "https://onefortheroadgit.sytes.net/dcovington/KCI-CAMPAIGN-TRACKER.git"
-
- # ---------------------------------------------------------------------------
- # Helpers
- # ---------------------------------------------------------------------------
- function Write-Step([string]$msg) {
- Write-Host "`n==> $msg" -ForegroundColor Cyan
- }
-
- function Get-BaseArgs {
- $a = @("-o", "StrictHostKeyChecking=accept-new")
- if ($SshKey -ne "") { $a += @("-i", $SshKey) }
- return $a
- }
-
- # ---------------------------------------------------------------------------
- # Step 1 — copy .env (first password prompt)
- # ---------------------------------------------------------------------------
- Write-Step "Copying .env to $SSH_USER@$SSH_HOST"
- $scpArgs = Get-BaseArgs
- $scpArgs += ".env", "${SSH_USER}@${SSH_HOST}:${REPO_PATH}/.env"
- scp @scpArgs
- if ($LASTEXITCODE -ne 0) { Write-Error "scp failed (exit $LASTEXITCODE)." }
-
- # ---------------------------------------------------------------------------
- # Step 2 — deploy (second password prompt)
- # ---------------------------------------------------------------------------
- Write-Step "Deploying on $SSH_USER@$SSH_HOST"
-
- # Deploy whichever branch is currently checked out locally.
- $BRANCH = (git rev-parse --abbrev-ref HEAD).Trim()
- Write-Host "Branch: $BRANCH" -ForegroundColor Yellow
-
- # Build as a single semicolon-separated string — no newlines, no CRLF risk.
- $remoteCmd = "set -e; "
- $remoteCmd += "if [ -d '$REPO_PATH/.git' ]; then "
- $remoteCmd += "cd '$REPO_PATH' && git fetch origin && git checkout $BRANCH && git pull origin $BRANCH; "
- $remoteCmd += "else "
- $remoteCmd += "mkdir -p '$REPO_PATH' && git clone --branch $BRANCH '$REPO_URL' '$REPO_PATH'; "
- $remoteCmd += "fi; "
- $remoteCmd += "docker ps -aq | xargs -r docker rm -f; "
- $remoteCmd += "docker run --rm -v '$REPO_PATH':/app -w /app composer:latest install --no-dev --optimize-autoloader --no-interaction; "
- $remoteCmd += "cd '$REPO_PATH' && docker compose up -d"
-
- $sshArgs = Get-BaseArgs
- $sshArgs += "$SSH_USER@$SSH_HOST", $remoteCmd
- ssh @sshArgs
- if ($LASTEXITCODE -ne 0) { Write-Error "Deployment failed (exit $LASTEXITCODE)." }
-
- Write-Host "`nDone. Campaign Tracker is running on $SSH_HOST." -ForegroundColor Green
|