#!/usr/bin/env bash # Compares the fully resolved config of an old, full docker-compose file against a new # base compose file + override compose file, to confirm a migration from "one full compose # file per environment" to "one shared base + small per-environment override files" # (see: https://github.com/compose-spec/compose-spec/blob/master/13-merge.md) produced an # identical stack. Generic - not tied to this project; copy it wherever you're doing the # same kind of migration, or keep one copy and point to it from any project's directory. # # Usage: compose-config-diff.sh [OLD_FILE] [NEW_FILE] [OVERRIDE_FILE] # OLD_FILE defaults to docker-compose.prod.old.yml # NEW_FILE defaults to docker-compose.prod.yml # OVERRIDE_FILE defaults to docker-compose.prod.override.yml (optional - if missing, # NEW_FILE is compared on its own, with a warning) # # Run from the directory containing these files (and .env, if the project uses one - # docker compose picks it up from the current directory as usual, same as any other # `docker compose` invocation there). # # Writes nothing to disk - both configs are held in memory and diffed via process # substitution. set -uo pipefail OLD="${1:-docker-compose.prod.old.yml}" NEW="${2:-docker-compose.prod.yml}" OVERRIDE="${3:-docker-compose.prod.override.yml}" if [[ ! -f "$OLD" ]]; then echo "Missing $OLD - run this from the directory containing it, or pass its path as \$1." >&2 exit 2 fi if [[ ! -f "$NEW" ]]; then echo "Missing $NEW - run this from the directory containing it, or pass its path as \$2." >&2 exit 2 fi new_files=(-f "$NEW") new_desc="$NEW" if [[ -f "$OVERRIDE" ]]; then new_files+=(-f "$OVERRIDE") new_desc="$NEW + $OVERRIDE" else echo "Note: $OVERRIDE not found - comparing against $NEW on its own." >&2 fi old_config="$(docker compose -f "$OLD" config)" old_status=$? new_config="$(docker compose "${new_files[@]}" config)" new_status=$? if [[ $old_status -ne 0 || $new_status -ne 0 ]]; then echo "FAILED: docker compose config errored (see messages above) - cannot compare." >&2 exit 2 fi if diff -u <(printf '%s\n' "$old_config") <(printf '%s\n' "$new_config"); then echo "OK: resolved config is identical between $OLD and $new_desc." exit 0 else echo "MISMATCH: resolved config differs (diff above) between $OLD and $new_desc." >&2 exit 1 fi