Add polling Docker deployments

Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
Amp
2026-08-22 07:09:49 +00:00
co-authored by heaust
parent eb7508d1c1
commit df8b69e09d
3 changed files with 96 additions and 6 deletions
+19 -6
View File
@@ -3,13 +3,16 @@ set -euo pipefail
repository="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
expected_repository="$HOME/yawn"
state_directory="$HOME/.local/state/yawn"
deployed_revision_file="$state_directory/deployed-revision"
compose=(docker compose --project-name yawn --project-directory "$repository" --file "$repository/compose.yaml")
if [[ "$repository" != "$expected_repository" ]]; then
printf 'Expected the repository at %s, found %s\n' "$expected_repository" "$repository" >&2
exit 1
fi
for command in git docker; do
for command in git docker flock; do
if ! command -v "$command" >/dev/null 2>&1; then
printf 'Missing required command: %s\n' "$command" >&2
exit 1
@@ -20,6 +23,13 @@ if ! docker compose version >/dev/null 2>&1; then
exit 1
fi
mkdir -p "$state_directory"
exec 9>"$state_directory/deploy.lock"
if ! flock -n 9; then
printf 'Another Yawn deployment is already running.\n'
exit 0
fi
cd "$repository"
if [[ "$(git branch --show-current)" != "main" ]]; then
@@ -41,10 +51,10 @@ if [[ "$(git rev-parse HEAD)" != "$fetched" ]]; then
fi
printf 'Building the production image...\n'
docker compose build --pull yawn
docker compose up -d --remove-orphans yawn
"${compose[@]}" build --pull yawn
"${compose[@]}" up -d --remove-orphans yawn
container="$(docker compose ps -q yawn)"
container="$("${compose[@]}" ps -q yawn)"
if [[ -z "$container" ]]; then
printf 'Docker Compose did not create the Yawn container.\n' >&2
exit 1
@@ -55,11 +65,14 @@ for _ in {1..60}; do
status="$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container")"
case "$status" in
healthy)
temporary_revision="$(mktemp "$state_directory/deployed-revision.XXXXXX")"
git rev-parse HEAD >"$temporary_revision"
mv -f -- "$temporary_revision" "$deployed_revision_file"
printf 'The Yawn container is healthy.\n'
exit 0
;;
unhealthy | exited | dead)
docker compose logs --tail 50 yawn >&2
"${compose[@]}" logs --tail 50 yawn >&2
exit 1
;;
esac
@@ -67,5 +80,5 @@ for _ in {1..60}; do
done
printf 'Yawn did not become healthy before the timeout.\n' >&2
docker compose logs --tail 50 yawn >&2
"${compose[@]}" logs --tail 50 yawn >&2
exit 1