fix(ci): isolate concurrent runs on a shared Docker daemon and explain readiness failures
CI / verify (pull_request) Successful in 50s
CI / infrastructure-smoke (pull_request) Successful in 14s
CI / migration-smoke (pull_request) Successful in 1m49s
CI / image-build (pull_request) Successful in 19s
CI / deploy-smoke (pull_request) Successful in 41s
CI / verify (pull_request) Successful in 50s
CI / infrastructure-smoke (pull_request) Successful in 14s
CI / migration-smoke (pull_request) Successful in 1m49s
CI / image-build (pull_request) Successful in 19s
CI / deploy-smoke (pull_request) Successful in 41s
PR #51's migration-smoke failed 0.7 s after the build: the suite pinned PostgreSQL to an ephemeral port but inherited S3_PORT=8333 from .env.example, so concurrent runs on one runner (Gitea ignores workflow concurrency) contended for one host port. Compose's own error was discarded, so the failure said only that the services "did not become ready". Isolation, from an audit of every script CI runs: - test-migrations-integration.sh: S3_PORT=0, like POSTGRES_PORT=0. - test-migrations-integration.sh and smoke-infrastructure.sh: project names gain a random suffix; run id plus PID alone is not unique when job containers share low PIDs. - smoke-deploy.sh: the edge /24 is chosen from those no network on the daemon uses, and the first `up` chooses again when a concurrent run wins a race for the same subnet (Docker's address-pool overlap), up to five times. - CI image-build: the image tag includes the run id and attempt, so a pull request and a push of the same commit cannot remove each other's image. - Already isolated: every other published port (ephemeral), the TLS suite's container and network (random), smoke staging directories and image tags (random). Diagnostics: scripts/lib/compose-diagnostics.sh prints the failed `up` command's output, `docker compose ps`, and the last 40 log lines of each service, all redacted (supplied literals, credential-looking NAME=value pairs, URL user information, Authorization headers) with terminal control sequences stripped. All three scripts use it at every readiness check; the infrastructure smoke's unredacted full-log dump is replaced by it. tests/ci-scripts.test.ts pins ephemeral ports, unique project names, the subnet choice, the CI image tag, the diagnostics, and the redaction itself (run through bash). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
c737ebb472
commit
278e66049f
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
# Why a disposable Compose project did not become ready: `docker compose ps` and the last log lines
|
||||
# of every service, so a CI failure says what went wrong (a port already allocated, a container that
|
||||
# exited, a health check that never passed) instead of only that something did.
|
||||
#
|
||||
# source scripts/lib/compose-diagnostics.sh
|
||||
# COMPOSE_DIAGNOSTICS_SECRETS=("$password" "$secret_key") # literal values to mask, optional
|
||||
# compose_report_not_ready "$up_output" "${compose[@]}"
|
||||
#
|
||||
# Everything printed passes through `compose_diagnostics_redact` first. The projects these scripts
|
||||
# start hold only disposable, generated test credentials, but output is redacted anyway: every
|
||||
# literal in COMPOSE_DIAGNOSTICS_SECRETS, the value of any `NAME=value` or `NAME: value` whose name
|
||||
# looks like a credential, the user-information part of any URL, and the rest of any Authorization
|
||||
# header. Terminal control sequences are stripped. Output goes to standard error.
|
||||
|
||||
COMPOSE_DIAGNOSTICS_SECRETS=()
|
||||
COMPOSE_DIAGNOSTICS_TAIL_LINES=40
|
||||
|
||||
# compose_diagnostics_redact: standard input to standard output, with credentials masked.
|
||||
compose_diagnostics_redact() {
|
||||
local secrets=""
|
||||
if ((${#COMPOSE_DIAGNOSTICS_SECRETS[@]} > 0)); then
|
||||
secrets="$(printf '%s\n' "${COMPOSE_DIAGNOSTICS_SECRETS[@]}")"
|
||||
fi
|
||||
# Literal values first, by plain substring rather than as patterns. Very short values are skipped:
|
||||
# masking every occurrence of a two-letter word would only make the output unreadable.
|
||||
COMPOSE_DIAGNOSTICS_LITERALS="$secrets" awk '
|
||||
BEGIN { count = split(ENVIRON["COMPOSE_DIAGNOSTICS_LITERALS"], secret, "\n") }
|
||||
{
|
||||
line = $0
|
||||
for (i = 1; i <= count; i++) {
|
||||
if (length(secret[i]) < 6) {
|
||||
continue
|
||||
}
|
||||
masked = ""
|
||||
while ((at = index(line, secret[i])) > 0) {
|
||||
masked = masked substr(line, 1, at - 1) "[redacted]"
|
||||
line = substr(line, at + length(secret[i]))
|
||||
}
|
||||
line = masked line
|
||||
}
|
||||
print line
|
||||
}
|
||||
' | sed -E \
|
||||
-e 's/\x1b\[[0-9;?]*[A-Za-z]//g' \
|
||||
-e 's#([Aa][Uu][Tt][Hh][Oo][Rr][Ii][Zz][Aa][Tt][Ii][Oo][Nn][[:space:]]*[:=]).*#\1 [redacted]#' \
|
||||
-e 's#([A-Za-z0-9_.-]*([Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|[Pp][Aa][Ss][Ss][Ww][Dd]|[Ss][Ee][Cc][Rr][Ee][Tt]|[Tt][Oo][Kk][Ee][Nn]|[_.-][Kk][Ee][Yy])[A-Za-z0-9_.-]*"?[[:space:]]*[=:][[:space:]]*)("[^"]*"|[^[:space:],;"]+)#\1[redacted]#g' \
|
||||
-e 's#([A-Za-z][A-Za-z0-9+.-]*://)[^/@[:space:]]+@#\1[redacted]@#g'
|
||||
}
|
||||
|
||||
# compose_report_not_ready <up output> <compose command...>: the failed command's own output, then
|
||||
# the project's containers and the tail of each service's logs, all redacted.
|
||||
compose_report_not_ready() {
|
||||
local up_output="$1"
|
||||
shift
|
||||
local service services
|
||||
{
|
||||
if [[ -n "$up_output" ]]; then
|
||||
echo "--- docker compose up (last 20 lines)"
|
||||
printf '%s\n' "$up_output" | tail -n 20 | compose_diagnostics_redact
|
||||
fi
|
||||
echo "--- docker compose ps"
|
||||
"$@" ps --all 2>&1 | compose_diagnostics_redact
|
||||
services="$("$@" config --services 2>/dev/null || true)"
|
||||
for service in $services; do
|
||||
echo "--- docker compose logs ${service} (last ${COMPOSE_DIAGNOSTICS_TAIL_LINES} lines)"
|
||||
"$@" logs --no-color --tail "$COMPOSE_DIAGNOSTICS_TAIL_LINES" "$service" 2>&1 |
|
||||
compose_diagnostics_redact
|
||||
done
|
||||
echo "--- end of diagnostics"
|
||||
} >&2
|
||||
}
|
||||
+57
-11
@@ -22,6 +22,9 @@ set -Eeuo pipefail
|
||||
repository_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$repository_root"
|
||||
|
||||
# shellcheck source=scripts/lib/compose-diagnostics.sh
|
||||
source "$repository_root/scripts/lib/compose-diagnostics.sh"
|
||||
|
||||
for command in docker openssl awk; do
|
||||
command -v "$command" >/dev/null || {
|
||||
echo "smoke-deploy: $command is required." >&2
|
||||
@@ -45,15 +48,34 @@ built_image=""
|
||||
hostname="ekklesia.smoke.test"
|
||||
storage_hostname="storage.smoke.test"
|
||||
|
||||
# A per-run edge subnet, so concurrent runs and the host's own networks do not collide. Fixed
|
||||
# addresses come from the upper half, outside Docker's dynamic range, exactly as documented.
|
||||
octet=$((RANDOM % 200 + 20))
|
||||
edge_subnet="10.249.${octet}.0/24"
|
||||
edge_range="10.249.${octet}.0/25"
|
||||
proxy_address="10.249.${octet}.254"
|
||||
tunnel_address="10.249.${octet}.253"
|
||||
# A test-only client address, also outside the dynamic range, so its limiter bucket is its own.
|
||||
session_client_address="10.249.${octet}.252"
|
||||
# choose_edge_network: a per-run edge subnet that no network on this Docker daemon already uses, so
|
||||
# concurrent runs sharing a runner's daemon do not collide. Two runs can still pick the same free
|
||||
# subnet at the same moment; the first `up` below detects that and chooses again. Fixed addresses come
|
||||
# from the upper half, outside Docker's dynamic range, exactly as documented.
|
||||
choose_edge_network() {
|
||||
local in_use candidate attempt
|
||||
in_use="$(
|
||||
docker network ls --quiet |
|
||||
xargs -r docker network inspect --format '{{range .IPAM.Config}}{{println .Subnet}}{{end}}' 2>/dev/null |
|
||||
sed -n 's#^10\.249\.\([0-9]*\)\.0/24$#\1#p'
|
||||
)" || in_use=""
|
||||
for attempt in $(seq 1 100); do
|
||||
candidate=$((RANDOM % 200 + 20))
|
||||
if ! grep -qx "$candidate" <<<"$in_use"; then
|
||||
octet="$candidate"
|
||||
edge_subnet="10.249.${octet}.0/24"
|
||||
edge_range="10.249.${octet}.0/25"
|
||||
proxy_address="10.249.${octet}.254"
|
||||
tunnel_address="10.249.${octet}.253"
|
||||
# A test-only client address, also outside the dynamic range, so its limiter bucket is its own.
|
||||
session_client_address="10.249.${octet}.252"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
echo "smoke-deploy: no free 10.249.x.0/24 edge subnet on this Docker daemon." >&2
|
||||
exit 1
|
||||
}
|
||||
choose_edge_network
|
||||
|
||||
# daemon_work: where the Docker daemon sees this run's mounted files. The same directory as `work`
|
||||
# on a workstation; a staged copy on the Docker host when this script runs in a job container.
|
||||
@@ -154,6 +176,8 @@ postgres_password="$(secret POSTGRES_PASSWORD)"
|
||||
s3_access_key="$(secret S3_ACCESS_KEY)"
|
||||
s3_secret_key="$(secret S3_SECRET_KEY)"
|
||||
identity_secret="$(secret EKKLESIA_IDENTITY_SECRET)"
|
||||
# Masked in any diagnostics below, along with anything else that looks like a credential.
|
||||
COMPOSE_DIAGNOSTICS_SECRETS=("$postgres_password" "$s3_access_key" "$s3_secret_key" "$identity_secret")
|
||||
|
||||
mkdir -p "$work/env"
|
||||
# set_env <file> <key> <value>: replaces an uncommented KEY= line, keeping the value byte-exact.
|
||||
@@ -207,8 +231,28 @@ EKKLESIA_TUNNEL_ADDRESS=${tunnel_address}
|
||||
EOF
|
||||
|
||||
step "Starting PostgreSQL and object storage"
|
||||
"${compose[@]}" up --detach --wait --wait-timeout 180 postgres object-storage >/dev/null 2>&1 ||
|
||||
# This `up` creates the edge network. If another run claimed the same subnet since it was chosen,
|
||||
# Docker refuses with an address-pool overlap: choose another and start again, a bounded number of
|
||||
# times. Any other failure is reported with the project's containers and logs.
|
||||
for attempt in 1 2 3 4 5; do
|
||||
if up_output="$(
|
||||
"${compose[@]}" up --detach --wait --wait-timeout 180 postgres object-storage 2>&1
|
||||
)"; then
|
||||
break
|
||||
fi
|
||||
if [[ "$up_output" == *"overlap"* ]] && ((attempt < 5)); then
|
||||
echo " edge subnet ${edge_subnet} was taken by a concurrent run; choosing another"
|
||||
"${compose[@]}" down --volumes --remove-orphans --timeout 10 >/dev/null 2>&1 || true
|
||||
choose_edge_network
|
||||
set_env "$work/compose.env" EKKLESIA_EDGE_SUBNET "$edge_subnet"
|
||||
set_env "$work/compose.env" EKKLESIA_EDGE_DYNAMIC_RANGE "$edge_range"
|
||||
set_env "$work/compose.env" EKKLESIA_PROXY_ADDRESS "$proxy_address"
|
||||
set_env "$work/compose.env" EKKLESIA_TUNNEL_ADDRESS "$tunnel_address"
|
||||
continue
|
||||
fi
|
||||
compose_report_not_ready "$up_output" "${compose[@]}"
|
||||
fail "PostgreSQL and object storage did not become healthy."
|
||||
done
|
||||
|
||||
step "Planning, then explicitly applying, migrations against this run's fresh database"
|
||||
"${compose[@]}" run --rm --no-deps migrate >/dev/null || fail "migrate (plan) failed."
|
||||
@@ -216,8 +260,10 @@ step "Planning, then explicitly applying, migrations against this run's fresh da
|
||||
fail "migrate (apply) failed."
|
||||
|
||||
step "Starting the API and the proxy"
|
||||
"${compose[@]}" up --detach --wait --wait-timeout 180 >/dev/null 2>&1 ||
|
||||
if ! up_output="$("${compose[@]}" up --detach --wait --wait-timeout 180 2>&1)"; then
|
||||
compose_report_not_ready "$up_output" "${compose[@]}"
|
||||
fail "the API and proxy did not become healthy."
|
||||
fi
|
||||
|
||||
# Caddy creates its internal CA on first start. The test clients trust it, plus the installation CA
|
||||
# for their direct connection to object-storage.
|
||||
|
||||
@@ -7,6 +7,8 @@ cd "$repository_root"
|
||||
|
||||
# shellcheck source=scripts/lib/compose-job-network.sh
|
||||
source "$repository_root/scripts/lib/compose-job-network.sh"
|
||||
# shellcheck source=scripts/lib/compose-diagnostics.sh
|
||||
source "$repository_root/scripts/lib/compose-diagnostics.sh"
|
||||
|
||||
command -v docker >/dev/null || {
|
||||
echo "Docker is required for the infrastructure smoke test." >&2
|
||||
@@ -28,7 +30,9 @@ if [[ "$curl_help" != *"--aws-sigv4"* ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
raw_project="ekklesia-smoke-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-0}-$$"
|
||||
# Unique per invocation, not only per run: several runs can share a runner's Docker daemon at once,
|
||||
# and the shell's PID is often identical across job containers.
|
||||
raw_project="ekklesia-smoke-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-0}-$$-${RANDOM}${RANDOM}"
|
||||
project="${raw_project//[^a-zA-Z0-9_-]/-}"
|
||||
project="${project,,}"
|
||||
temporary_directory="$(mktemp -d)"
|
||||
@@ -46,6 +50,8 @@ export S3_SECRET_KEY="ekklesia-smoke-secret-key"
|
||||
export S3_BUCKET="ekklesia-smoke"
|
||||
export S3_REGION="us-east-1"
|
||||
export S3_PORT="${EKKLESIA_SMOKE_S3_PORT:-0}"
|
||||
COMPOSE_DIAGNOSTICS_SECRETS=("$POSTGRES_PASSWORD" "$S3_ACCESS_KEY" "$S3_SECRET_KEY")
|
||||
up_output=""
|
||||
|
||||
compose=(
|
||||
docker compose
|
||||
@@ -63,7 +69,7 @@ cleanup() {
|
||||
local cleanup_output
|
||||
trap - EXIT INT TERM
|
||||
if ((exit_status != 0)); then
|
||||
"${compose[@]}" logs --no-color >&2 || true
|
||||
compose_report_not_ready "$up_output" "${compose[@]}" || true
|
||||
fi
|
||||
if ! compose_job_network_disconnect; then
|
||||
echo "Infrastructure smoke network cleanup failed." >&2
|
||||
@@ -88,8 +94,15 @@ trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
|
||||
"${compose[@]}" config --quiet
|
||||
"${compose[@]}" up --detach --wait --wait-timeout 120
|
||||
"${compose[@]}" up --detach --wait --wait-timeout 120
|
||||
# Started twice: the second `up` proves the project is idempotent. A failure's own output is kept for
|
||||
# the cleanup's diagnostics, which also print every container and the tail of its logs.
|
||||
for _ in 1 2; do
|
||||
if ! up_output="$("${compose[@]}" up --detach --wait --wait-timeout 120 2>&1)"; then
|
||||
echo "Disposable PostgreSQL and object storage did not become ready." >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
up_output=""
|
||||
|
||||
database_result="$(
|
||||
"${compose[@]}" exec -T \
|
||||
|
||||
@@ -7,6 +7,8 @@ cd "$repository_root"
|
||||
|
||||
# shellcheck source=scripts/lib/compose-job-network.sh
|
||||
source "$repository_root/scripts/lib/compose-job-network.sh"
|
||||
# shellcheck source=scripts/lib/compose-diagnostics.sh
|
||||
source "$repository_root/scripts/lib/compose-diagnostics.sh"
|
||||
|
||||
command -v docker >/dev/null || {
|
||||
echo "Docker is required for the PostgreSQL migration integration suite." >&2
|
||||
@@ -25,14 +27,20 @@ if ! compose_job_network_initialize; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
raw_project="ekklesia-migration-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-0}-${GITHUB_JOB:-job}-$$"
|
||||
# Unique per invocation, not only per run: several runs (and several jobs of one run) can share a
|
||||
# runner's Docker daemon at once, and the shell's PID is often identical across job containers.
|
||||
raw_project="ekklesia-migration-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-0}-${GITHUB_JOB:-job}-$$-${RANDOM}${RANDOM}"
|
||||
project="${raw_project//[^a-zA-Z0-9_-]/-}"
|
||||
project="${project,,}"
|
||||
|
||||
export POSTGRES_DB="ekklesia_migration_test"
|
||||
export POSTGRES_USER="ekklesia_migration_test"
|
||||
export POSTGRES_PASSWORD="temporary-$project-${RANDOM}-${RANDOM}"
|
||||
# Both host ports are ephemeral: concurrent runs on one Docker daemon must never contend for a
|
||||
# fixed one. `.env.example` pins the storage port for local development, so it is overridden here
|
||||
# exactly like the database's. The bound ports are discovered below.
|
||||
export POSTGRES_PORT="0"
|
||||
export S3_PORT="0"
|
||||
|
||||
compose=(
|
||||
docker compose
|
||||
@@ -72,8 +80,17 @@ fi
|
||||
# and CI already knows how to provision. SeaweedFS is the intended deployment target (ADR 0004), and
|
||||
# proving the storage contract against a mock instead would have hidden the provider quirks this
|
||||
# suite exists to catch.
|
||||
if ! "${compose[@]}" up --detach --wait --wait-timeout 180 postgres object-storage >/dev/null 2>&1; then
|
||||
# Masked in any diagnostics below, along with anything else that looks like a credential.
|
||||
COMPOSE_DIAGNOSTICS_SECRETS=(
|
||||
"$POSTGRES_PASSWORD"
|
||||
"$(grep -E '^S3_ACCESS_KEY=' .env.example | cut -d= -f2-)"
|
||||
"$(grep -E '^S3_SECRET_KEY=' .env.example | cut -d= -f2-)"
|
||||
)
|
||||
if ! up_output="$(
|
||||
"${compose[@]}" up --detach --wait --wait-timeout 180 postgres object-storage 2>&1
|
||||
)"; then
|
||||
echo "Disposable PostgreSQL and object storage did not become ready." >&2
|
||||
compose_report_not_ready "$up_output" "${compose[@]}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user