|
|
|
@@ -0,0 +1,91 @@ |
|
|
|
#Requires -Version 5.1 |
|
|
|
<# |
|
|
|
.SYNOPSIS |
|
|
|
Pulls the repo on the server, copies .env_prod as .env, then starts 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/kanban" |
|
|
|
$REPO_URL = "https://onefortheroadgit.sytes.net/dcovington/KCI-PHP-KANBAN.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 |
|
|
|
} |
|
|
|
|
|
|
|
# Deploy whichever branch is currently checked out locally. |
|
|
|
$BRANCH = (git rev-parse --abbrev-ref HEAD).Trim() |
|
|
|
Write-Host "Branch: $BRANCH" -ForegroundColor Yellow |
|
|
|
|
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
# Step 1 — clone or pull the repo (first password prompt) |
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
Write-Step "Syncing repo on $SSH_USER@$SSH_HOST" |
|
|
|
|
|
|
|
$prepCmd = "set -e; " |
|
|
|
$prepCmd += "if [ -d '$REPO_PATH/.git' ]; then " |
|
|
|
$prepCmd += "cd '$REPO_PATH' && git fetch origin && git checkout $BRANCH && git pull origin $BRANCH; " |
|
|
|
$prepCmd += "else " |
|
|
|
$prepCmd += "git clone --branch $BRANCH '$REPO_URL' '$REPO_PATH'; " |
|
|
|
$prepCmd += "fi" |
|
|
|
|
|
|
|
$sshArgs = Get-BaseArgs |
|
|
|
$sshArgs += "$SSH_USER@$SSH_HOST", $prepCmd |
|
|
|
ssh @sshArgs |
|
|
|
if ($LASTEXITCODE -ne 0) { Write-Error "Repo sync failed (exit $LASTEXITCODE)." } |
|
|
|
|
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
# Step 2 — copy .env_prod as .env into the now-existing repo dir (second prompt) |
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
Write-Step "Copying .env_prod to $SSH_USER@$SSH_HOST as .env" |
|
|
|
$scpArgs = Get-BaseArgs |
|
|
|
$scpArgs += ".env_prod", "${SSH_USER}@${SSH_HOST}:${REPO_PATH}/.env" |
|
|
|
scp @scpArgs |
|
|
|
if ($LASTEXITCODE -ne 0) { Write-Error "scp failed (exit $LASTEXITCODE)." } |
|
|
|
|
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
# Step 3 — copy the SQLite database (third password prompt) |
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
Write-Step "Copying database/app.sqlite to $SSH_USER@$SSH_HOST" |
|
|
|
$scpDbArgs = Get-BaseArgs |
|
|
|
$scpDbArgs += "database/app.sqlite", "${SSH_USER}@${SSH_HOST}:${REPO_PATH}/database/app.sqlite" |
|
|
|
scp @scpDbArgs |
|
|
|
if ($LASTEXITCODE -ne 0) { Write-Error "scp database failed (exit $LASTEXITCODE)." } |
|
|
|
|
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
# Step 4 — start the container (fourth password prompt) |
|
|
|
# --------------------------------------------------------------------------- |
|
|
|
Write-Step "Starting container on $SSH_USER@$SSH_HOST" |
|
|
|
|
|
|
|
$deployCmd = "set -e; " |
|
|
|
$deployCmd += "docker rm -f kci-kanban-app 2>/dev/null || true; " |
|
|
|
$deployCmd += "cd '$REPO_PATH' && docker compose up -d --build --wait" |
|
|
|
|
|
|
|
$sshArgs = Get-BaseArgs |
|
|
|
$sshArgs += "$SSH_USER@$SSH_HOST", $deployCmd |
|
|
|
ssh @sshArgs |
|
|
|
if ($LASTEXITCODE -ne 0) { Write-Error "Deployment failed (exit $LASTEXITCODE)." } |
|
|
|
|
|
|
|
Write-Host "`nDone. KCI Kanban is running on $SSH_HOST." -ForegroundColor Green |