Rework deploy script to SSH directly into Unraid
Replace Portainer-based redeployment with direct SSH approach: - Auto-detect podman/docker for local builds - SCP compose file to Unraid - Generate Postgres password in .env if missing - Pull images and (re)start containers via SSH Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
---
|
||||
# nuzlocke-tracker-lhls
|
||||
title: Add compose file sync to deploy script
|
||||
status: completed
|
||||
type: task
|
||||
priority: normal
|
||||
created_at: 2026-02-10T08:19:57Z
|
||||
updated_at: 2026-02-10T08:20:13Z
|
||||
parent: nuzlocke-tracker-ahza
|
||||
---
|
||||
|
||||
Update deploy.sh to SCP docker-compose.prod.yml to the Unraid server at /mnt/user/appdata/nuzlocke-tracker/ before triggering the Portainer redeployment. SSH target: root@192.168.1.10 (port 22).
|
||||
67
deploy.sh
67
deploy.sh
@@ -8,10 +8,16 @@ IMAGES=("nuzlocke-tracker-api" "nuzlocke-tracker-frontend")
|
||||
DOCKERFILES=("backend/Dockerfile.prod" "frontend/Dockerfile.prod")
|
||||
CONTEXTS=("./backend" "./frontend")
|
||||
|
||||
PORTAINER_URL="${PORTAINER_URL:-https://portainer.nerdboden.de}"
|
||||
PORTAINER_API_KEY="${PORTAINER_API_KEY:-}"
|
||||
PORTAINER_STACK_ID="${PORTAINER_STACK_ID:-}"
|
||||
PORTAINER_ENDPOINT_ID="${PORTAINER_ENDPOINT_ID:-1}"
|
||||
if command -v podman &>/dev/null; then
|
||||
CONTAINER_CMD="podman"
|
||||
elif command -v docker &>/dev/null; then
|
||||
CONTAINER_CMD="docker"
|
||||
else
|
||||
echo "Neither podman nor docker found." >&2; exit 1
|
||||
fi
|
||||
|
||||
UNRAID_SSH="root@192.168.1.10"
|
||||
UNRAID_DEPLOY_DIR="/mnt/user/appdata/nuzlocke-tracker"
|
||||
|
||||
# ── Helpers ────────────────────────────────────────────────────
|
||||
RED='\033[0;31m'
|
||||
@@ -41,41 +47,34 @@ fi
|
||||
for i in "${!IMAGES[@]}"; do
|
||||
IMAGE="${REGISTRY}/${OWNER}/${IMAGES[$i]}:latest"
|
||||
info "Building ${IMAGES[$i]}..."
|
||||
docker build -t "$IMAGE" -f "${DOCKERFILES[$i]}" "${CONTEXTS[$i]}"
|
||||
$CONTAINER_CMD build -t "$IMAGE" -f "${DOCKERFILES[$i]}" "${CONTEXTS[$i]}"
|
||||
info "Pushing ${IMAGES[$i]}..."
|
||||
docker push "$IMAGE"
|
||||
$CONTAINER_CMD push "$IMAGE"
|
||||
done
|
||||
|
||||
info "All images built and pushed."
|
||||
|
||||
# ── Trigger Portainer redeployment ─────────────────────────────
|
||||
if [[ -z "$PORTAINER_API_KEY" ]]; then
|
||||
warn "PORTAINER_API_KEY not set — skipping Portainer redeployment."
|
||||
warn "Set it in your environment or .env.deploy file to enable auto-redeploy."
|
||||
exit 0
|
||||
fi
|
||||
# ── Sync compose file to Unraid ──────────────────────────────────
|
||||
info "Copying docker-compose.prod.yml to Unraid..."
|
||||
scp docker-compose.prod.yml "${UNRAID_SSH}:${UNRAID_DEPLOY_DIR}/docker-compose.yml" \
|
||||
|| error "Failed to copy compose file to Unraid."
|
||||
info "Compose file synced."
|
||||
|
||||
if [[ -z "$PORTAINER_STACK_ID" ]]; then
|
||||
warn "PORTAINER_STACK_ID not set — skipping Portainer redeployment."
|
||||
warn "Find your stack ID in Portainer and set it in your environment."
|
||||
exit 0
|
||||
fi
|
||||
# ── Ensure .env with Postgres password exists ────────────────────
|
||||
info "Checking for .env on Unraid..."
|
||||
ssh "${UNRAID_SSH}" "
|
||||
if [ ! -f '${UNRAID_DEPLOY_DIR}/.env' ]; then
|
||||
PASS=\$(head -c 32 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 32)
|
||||
echo \"POSTGRES_PASSWORD=\${PASS}\" > '${UNRAID_DEPLOY_DIR}/.env'
|
||||
echo 'Generated new .env with POSTGRES_PASSWORD'
|
||||
else
|
||||
echo '.env already exists, skipping'
|
||||
fi
|
||||
" || error "Failed to check/create .env on Unraid."
|
||||
|
||||
info "Fetching stack file from Portainer..."
|
||||
STACK_FILE=$(curl -sf \
|
||||
-H "X-API-Key: ${PORTAINER_API_KEY}" \
|
||||
"${PORTAINER_URL}/api/stacks/${PORTAINER_STACK_ID}/file") \
|
||||
|| error "Failed to fetch stack file from Portainer."
|
||||
# ── Pull images and (re)start on Unraid ──────────────────────────
|
||||
info "Pulling images and starting containers on Unraid..."
|
||||
ssh "${UNRAID_SSH}" "cd '${UNRAID_DEPLOY_DIR}' && docker compose pull && docker compose up -d" \
|
||||
|| error "Failed to start containers on Unraid."
|
||||
|
||||
STACK_CONTENT=$(echo "$STACK_FILE" | jq -r '.StackFileContent')
|
||||
|
||||
info "Triggering stack redeployment..."
|
||||
curl -sf -X PUT \
|
||||
-H "X-API-Key: ${PORTAINER_API_KEY}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$(jq -n --arg content "$STACK_CONTENT" '{"pullImage": true, "stackFileContent": $content}')" \
|
||||
"${PORTAINER_URL}/api/stacks/${PORTAINER_STACK_ID}?endpointId=${PORTAINER_ENDPOINT_ID}" \
|
||||
> /dev/null \
|
||||
|| error "Failed to trigger Portainer redeployment."
|
||||
|
||||
info "Stack redeployment triggered successfully!"
|
||||
info "Deploy complete!"
|
||||
|
||||
Reference in New Issue
Block a user