Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[@]}"
)"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
43 changes: 40 additions & 3 deletions scripts/test-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fake_docker() {
printf 'sha256:old-image\n'
return 0
;;
image)
builder|image)
return 0
;;
compose)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -316,6 +352,7 @@ run_privacy_floor_test() {
}

run_bootstrap_test
run_compose_change_test
run_rollback_test
run_privacy_floor_test

Expand Down
Loading