Files
computer-playbook/scripts/meta/resolve/diff/affected_roles.sh
T
kevinveenbirkenbachandClaude Opus 5 5b8465260f feat(ci): deploy only the variants a change reaches
An image bump in a provider redeployed every variant of every consumer,
including the variants that switch that provider off and therefore never
deploy the bumped image at all. The diff-derived whitelist now narrows each
affected role to the variants whose own round closure still reaches a seed,
emitted as `role#0,2` selection tokens. That closure is walked on the same
variant-merged services maps the matrix planner builds its rounds from, so the
narrowing follows a parent's variant choice across every generation of the
chain. It covers every auto-derived whitelist: the weekly update PRs,
Dependabot, and any other diff-driven run.

The narrowing never shrinks the matrix on partial information. A seed role
keeps every variant, because its own files changed. A role reached only
through a run_after edge keeps every variant too, because the round closure
does not follow that edge and a role whose hits are empty is therefore
unnarrowed rather than dropped.

A pinned token naming a role the discovery query does not return now warns
instead of aborting the run. The diff-derived whitelist legitimately names
roles the lifecycle envelope filters out, and once it pins variants such a
token would abort every chunk; role names are still checked against roles/
before the run starts, so a typo is caught one layer earlier.

The round's variant merge moves out of the deploy inventory package into
utils/roles/applications/variants.py. The CI selection has to build the same
maps, and importing the deploy package for them pulls in a module that reads
INFINITO_SRC_DIR at import time, which the runner host does not set.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-21 22:51:03 +02:00

116 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Compute which roles are affected by the current branch's diff against
# `origin/main`, expanded transitively over run_after + dependencies +
# services edges via cli.meta.roles.applications.resolution.affected.
#
# Output (single line on stdout):
# __ALL__ full deploy needed. Emitted when:
# * the diff is empty, OR
# * any changed path outside
# roles/<role>/ is not a Markdown
# or reStructuredText file (the
# `.md`/`.rst` skip-list mirrors
# scripts/meta/resolve/pr/scope.sh's
# `is_markdown_or_rst`), OR
# * the diff contains only such
# skipped paths and no role seeds
# could be derived, OR
# * any seed role is non-modellable
# in the reverse resolver
# (resolver exit 2), OR
# * the resolver itself errors out
# (any other non-zero exit).
# <token> [<token> ...] restricted deploy set (whitelist).
# A token is a role id, narrowed to the
# variants that reach a seed where not all
# of them do (`web-app-x#0,2`, see
# utils.github.variant.selection).
#
# Pre-conditions:
# - The host Python interpreter referenced by ${PYTHON:-python3} has the
# project's resolver deps installed (e.g. via `make install`).
# - `git fetch origin main` is reachable (caller already did checkout).
#
# Exit codes:
# 0 in all defined output cases (including the fail-safe __ALL__).
# The script is fail-safe by design: any resolver failure widens the
# deploy matrix to "all", it never narrows it.
set -euo pipefail
PYTHON="${PYTHON:-python3}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
cd "$REPO_ROOT"
if [[ -f "scripts/meta/env/load.sh" ]]; then
# shellcheck source=scripts/meta/env/load.sh
source "scripts/meta/env/load.sh"
fi
emit_all() {
echo "__ALL__"
exit 0
}
git fetch --quiet --no-tags --prune --depth=50 origin main >/dev/null 2>&1 ||
git fetch --quiet --no-tags --prune origin main
if base="$(git merge-base origin/main HEAD 2>/dev/null)"; then
mapfile -t changed_paths < <(git diff --name-only "${base}" HEAD --)
else
mapfile -t changed_paths < <(git diff --name-only origin/main HEAD --)
fi
if ((${#changed_paths[@]} == 0)); then
emit_all
fi
declare -A seed_roles=()
for path in "${changed_paths[@]}"; do
[[ -z "${path}" ]] && continue
if [[ "${path}" != roles/*/* ]]; then
case "${path}" in
*.md | *.rst)
continue
;;
esac
emit_all
fi
role="${path#roles/}"
role="${role%%/*}"
[[ -z "${role}" ]] && emit_all
seed_roles["${role}"]=1
done
if ((${#seed_roles[@]} == 0)); then
emit_all
fi
mapfile -t seed_list < <(printf '%s\n' "${!seed_roles[@]}" | sort)
set +e
resolved="$(
"${PYTHON}" -m cli.meta.roles.applications.resolution.affected \
--changed-roles "${seed_list[@]}"
)"
resolver_rc=$?
set -e
if ((resolver_rc != 0)); then
emit_all
fi
resolved="${resolved//$'\r'/}"
resolved="${resolved#"${resolved%%[![:space:]]*}"}"
resolved="${resolved%"${resolved##*[![:space:]]}"}"
if [[ -z "${resolved}" ]]; then
emit_all
fi
printf '%s\n' "${resolved}"