From 2f9c38508239d88f5e4c2f8b355a558713cad01a Mon Sep 17 00:00:00 2001 From: Cailyn Sinclair Date: Tue, 17 Nov 2026 07:20:53 -0800 Subject: [PATCH] Reconcile production storage settings Force service recreation when production Compose files change, and cap unused Docker build cache after successful deployments. --- scripts/deploy.sh | 32 ++++++++++++++++++++++++++----- scripts/test-deploy.sh | 43 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index cfc2259..9a1539b 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -57,6 +57,22 @@ case "$DEPLOY_STRATEGY" in esac current_commit="$(git rev-parse HEAD)" +changed_files=() + +if [ "$previous_commit" != "$current_commit" ]; then + mapfile -d '' changed_files < <( + git diff --name-only --no-renames -z "$previous_commit" "$current_commit" + ) +fi + +production_compose_changed="false" +for changed_file in "${changed_files[@]}"; do + case "$changed_file" in + compose.yml|compose.prod.yml) + production_compose_changed="true" + ;; + esac +done privacy_cutover_file="${PRIVACY_EMAIL_CUTOVER_FILE:-$APP_DIR/.privacy-email-cutover}" if [ -f "$privacy_cutover_file" ]; then @@ -97,9 +113,6 @@ if [ "$DEPLOY_TARGET" = "auto" ]; then # behavior of replacing the complete application deployment. resolved_target="all" else - mapfile -d '' changed_files < <( - git diff --name-only --no-renames -z "$previous_commit" "$current_commit" - ) resolved_target="$( bash scripts/classify-deploy-target.sh "${changed_files[@]}" )" @@ -169,13 +182,18 @@ fi # Compose usage compatible with the default `local` tag. export APP_IMAGE_TAG="$current_commit" +compose_recreate_args=() +if [ "$production_compose_changed" = "true" ]; then + compose_recreate_args=(--force-recreate) +fi + if ! "${COMPOSE[@]}" build api; then echo "Application image build failed; no application containers were replaced." >&2 print_retry_hint exit 1 fi -if ! "${COMPOSE[@]}" up -d --wait \ +if ! "${COMPOSE[@]}" up -d "${compose_recreate_args[@]}" --wait \ --wait-timeout "$DEPLOY_WAIT_TIMEOUT_SECONDS" postgres redis; then echo "Failed to start backing services; no application containers were replaced." >&2 print_retry_hint @@ -223,7 +241,7 @@ reload_nginx() { echo "nginx is not running; starting it without replacing dependencies." fi - "${compose_command[@]}" up -d --no-deps --wait \ + "${compose_command[@]}" up -d --no-deps "${compose_recreate_args[@]}" --wait \ --wait-timeout "$DEPLOY_WAIT_TIMEOUT_SECONDS" nginx || return 1 "${compose_command[@]}" exec -T nginx nginx -s reload @@ -323,3 +341,7 @@ echo "Deployment complete: $resolved_target now uses letscube-app:$current_commi if ! docker image prune -af --filter "until=168h"; then echo "Deployment succeeded, but unused image cleanup failed." >&2 fi + +if ! docker builder prune -af --max-used-space 2GB; then + echo "Deployment succeeded, but build cache cleanup failed." >&2 +fi diff --git a/scripts/test-deploy.sh b/scripts/test-deploy.sh index 805e303..342794c 100755 --- a/scripts/test-deploy.sh +++ b/scripts/test-deploy.sh @@ -58,7 +58,7 @@ fake_docker() { printf 'sha256:old-image\n' return 0 ;; - image) + builder|image) return 0 ;; compose) @@ -225,6 +225,42 @@ run_bootstrap_test() { fi assert_contains "$(<"$log")" 'docker image prune -af --filter until=168h' + assert_contains "$(<"$log")" 'docker builder prune -af --max-used-space 2GB' +} + +run_compose_change_test() { + local scenario="$TEST_ROOT/compose-change" + local app_dir="$scenario/app" + local log="$scenario/commands.log" + local output="$scenario/output.log" + local git_state="$scenario/git-state" + local rollback_marker="$scenario/rollback" + local previous_commit="1111111111111111111111111111111111111111" + local current_commit="2222222222222222222222222222222222222222" + + mkdir -p "$scenario/tmp" + prepare_app "$app_dir" + + PATH="$FAKE_BIN:$PATH" \ + APP_DIR="$app_dir" \ + DEPLOY_TEST_LOG="$log" \ + DEPLOY_WAIT_TIMEOUT_SECONDS=17 \ + ENV_FILE=.env.prod \ + FAKE_CHANGED_PATH=compose.prod.yml \ + FAKE_CURRENT_COMMIT="$current_commit" \ + FAKE_GIT_STATE="$git_state" \ + FAKE_PREVIOUS_COMMIT="$previous_commit" \ + FAKE_ROLLBACK_MARKER="$rollback_marker" \ + FAKE_RUNNING_API=api-container \ + FAKE_RUNNING_NGINX=nginx-container \ + FAKE_RUNNING_SOCKET=socket-container \ + TMPDIR="$scenario/tmp" \ + bash "$app_dir/scripts/deploy.sh" > "$output" 2>&1 + + assert_contains "$(<"$log")" \ + 'up -d --force-recreate --wait --wait-timeout 17 postgres redis' + assert_contains "$(<"$log")" \ + 'up -d --no-deps --force-recreate --wait --wait-timeout 17 nginx' } run_rollback_test() { @@ -273,8 +309,8 @@ run_rollback_test() { assert_contains "$(<"$log")" "git show $previous_commit:compose.yml" assert_contains "$(<"$log")" "git show $previous_commit:compose.prod.yml" - if grep -q 'docker image prune' "$log"; then - echo 'Failed deployment removed images.' >&2 + if grep -Eq 'docker (image|builder) prune' "$log"; then + echo 'Failed deployment removed Docker data.' >&2 exit 1 fi } @@ -316,6 +352,7 @@ run_privacy_floor_test() { } run_bootstrap_test +run_compose_change_test run_rollback_test run_privacy_floor_test