25 lines
1.4 KiB
Bash
25 lines
1.4 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
if [ "$(id -u)" -ne 0 ]; then exec sudo -n bash "$0" "$@"; fi
|
|
sha=${1:?release SHA required}
|
|
[[ "$sha" =~ ^[0-9a-f]{40}$ ]]
|
|
exec 9>/var/lock/prompt-studio.lock
|
|
flock -w 300 9
|
|
latest=$(git -c safe.directory="$PWD" ls-remote origin refs/heads/main | cut -f1)
|
|
if [ "$latest" != "$sha" ]; then
|
|
git -c safe.directory="$PWD" fetch origin main
|
|
git -c safe.directory="$PWD" diff --quiet "$sha" origin/main -- apps/studio scripts/deploy-studio.sh .gitea/workflows/studio.yml pnpm-workspace.yaml || exit 0
|
|
fi
|
|
mkdir -p /opt/prompt-studio
|
|
previous=$(cat /opt/prompt-studio/image 2>/dev/null || true)
|
|
write_compose(){ printf 'services:\n studio:\n image: %s\n restart: unless-stopped\n ports: ["127.0.0.1:18134:3000"]\n networks:\n waterfall-wiki:\n aliases: [prompt-studio]\nnetworks:\n waterfall-wiki:\n external: true\n name: prompt_default\n' "$1" > /opt/prompt-studio/compose.yml; }
|
|
write_compose "prompt-studio:$sha"
|
|
rollback(){ if [ -n "$previous" ]; then write_compose "$previous"; docker compose -p prompt-studio -f /opt/prompt-studio/compose.yml up -d; fi; }
|
|
trap rollback ERR
|
|
docker compose -p prompt-studio -f /opt/prompt-studio/compose.yml up -d
|
|
for attempt in {1..40}; do
|
|
if curl -fsS http://127.0.0.1:18134/studio/api/health >/dev/null; then printf '%s' "prompt-studio:$sha" > /opt/prompt-studio/image; exit 0; fi
|
|
sleep 2
|
|
done
|
|
false
|