Files
jobboard/deploy/server/deploy.sh
T

56 lines
2.5 KiB
Bash

#!/usr/bin/env bash
# Run on the server, next to the compose file, or with its directory as $1.
# No remote shell required: the release scripts publish an image
# and stop there, and this is the other half.
#
# SERVICE is the compose service, the one name a project renames in these
# scripts (README, "Starting a project from it"); the tests read it from
# compose.yaml and hold every script to it. APP_DATA is renamed with the other
# APP_ variables.
SERVICE=app
set -euo pipefail
cd "${1:-$(dirname "$(readlink -f "$0")")}"
[[ -s .env ]] || { echo 'Install .env before deploying.' >&2; exit 1; }
# Where the data directory is depends on APP_DATA, which the compose file also
# reads. Pulled out of .env rather than sourced, because Compose's .env is not
# a shell script and running it as one is how a stray quote becomes a surprise.
DATA=$(sed -n 's/^[[:space:]]*APP_DATA=\([^#]*\).*/\1/p' .env | tail -1 | xargs || true)
DATA=${DATA:-./data}
config=$DATA/config.yaml
[[ -s $config ]] || {
echo "Install $config before deploying, from config.example.yaml beside this script." >&2
exit 1
}
# The two keys whose defaults are wrong in a container: `auth.enabled: auto`
# ties the lock to how the process was started, and the default
# database is wherever the config file happens to be. Read with grep and awk,
# because the host need not have a Python with a YAML parser.
if ! grep -Eq '^database:[[:space:]]*[^[:space:]#]' "$config" ||
! awk '/^auth:/ { a = 1; next } /^[^[:space:]#]/ { a = 0 }
a && /^[[:space:]]+enabled:[[:space:]]*[^[:space:]#]/ { found = 1 }
END { exit !found }' "$config"; then
echo "$config must set database and auth.enabled; config.example.yaml beside this script has both." >&2
exit 1
fi
# Pull explicitly, here and nowhere else: the compose file has no pull policy,
# so starting or importing never fetches an image on its own. A registry
# failure must leave the running service alone, not deploy a cache.
echo 'Pulling the configured image…'
if ! docker compose pull "$SERVICE"; then
echo "Image pull failed; $SERVICE was not restarted." >&2
exit 1
fi
# Use the image just pulled. Do not start or recreate other services in the
# file. `--wait` holds until the container's own HEALTHCHECK passes.
if ! docker compose up -d --no-deps --pull never --wait --wait-timeout 240 "$SERVICE"; then
docker compose ps "$SERVICE" >&2 || true
echo "Deployment failed. Check docker compose logs $SERVICE." >&2
exit 1
fi
docker compose ps "$SERVICE"