Keeping Services in Step with the Template
Scaffolding happens once. Six months later the template has a new scanner, a memory limit, a runbook link and a newer shared workflow, and the forty services created from it have none of that. Template updates are what make a golden path a living product rather than a one-off copy. This lesson builds the standard technique, a three-way merge, the same approach Copier uses for copier update.
Why not just re-render?
Re-rendering the new template over the service would erase every change the team made since day one (they scaled to 3 replicas and raised CPU). Diffing the service against the new template would show those team changes as differences too. The fix is a three-way merge with three inputs:
| Input | Comes from | Role |
|---|---|---|
| base | the old template version, rendered with the service's recorded answers | what the service looked like when it was created |
| ours | the service as it is today | base + the team's changes |
| theirs | the new template version, rendered with the same answers | base + the platform team's changes |
Changes on one side only are applied automatically. Changes to the same lines on both sides are a conflict that a human resolves. That's exactly what Git does when merging branches, and git merge-file does it for a single file.
The session
It repeats the setup of the previous lesson (template v1.0.0, scaffold shortlinks), then the team raises replicas and CPU, the platform team releases v1.1.0 (workflow @v4, a memory limit, a runbook link, and a higher default CPU request), and update-service.sh merges file by file on a new branch:
# ---- the platform team's template repository ----
mkdir -p golden-go-service/skeleton/.github/workflows golden-go-service/skeleton/deploy && cd golden-go-service
cat > render.sh <<'EOF'
#!/bin/bash
# usage: render.sh <skeleton-dir> <answers.yaml> <out-dir>
set -euo pipefail
yq -o=props "$2" | sed -E 's/^([^ ]+) = (.*)$/s|\\$\\{\\{ values.\1 \\}\\}|\2|g/' > "$3.sed"
(cd "$1" && find . -type f) | sort | while read -r f; do
out="$3/$(echo "$f" | sed -E -f "$3.sed")"
mkdir -p "$(dirname "$out")" && sed -E -f "$3.sed" "$1/$f" > "$out"
done
rm "$3.sed"
EOF
chmod +x render.sh
cat > skeleton/README.md <<'EOF'
# ${{ values.name }}
Owned by ${{ values.owner }}. Created from the go-service golden path.
EOF
cat > skeleton/catalog-info.yaml <<'EOF'
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: ${{ values.name }}
spec:
type: service
owner: ${{ values.owner }}
lifecycle: experimental
EOF
cat > skeleton/.github/workflows/ci.yaml <<'EOF'
name: ci
on: [push]
jobs:
build:
uses: acme/platform-workflows/.github/workflows/go-service.yaml@v3
with:
image: ghcr.io/acme/${{ values.name }}
tag: ${{ github.sha }}
EOF
cat > skeleton/deploy/values.yaml <<'EOF'
image: ghcr.io/acme/${{ values.name }}
replicaCount: 2
resources:
requests: {cpu: 50m, memory: 64Mi}
EOF
git init -q && git add . && git commit -qm "go-service golden path" && git tag v1.0.0
cd ..
# ---- the scaffolder: what a "Create" button runs ----
cat > new-service.sh <<'EOF'
#!/bin/bash
# usage: new-service.sh <name> <owner> [template-version]
set -euo pipefail
name=$1 owner=$2 version=${3:-v1.0.0}
tpl=$(mktemp -d)
git clone -q --depth 1 --branch "$version" "file://$PWD/golden-go-service" "$tpl"
printf 'name: %s\nowner: %s\n' "$name" "$owner" > "$tpl/answers.yaml"
"$tpl/render.sh" "$tpl/skeleton" "$tpl/answers.yaml" "$name"
# remember where the service came from, for later template updates
{ echo "template: golden-go-service"; echo "version: $version"; echo "answers:"; sed 's/^/ /' "$tpl/answers.yaml"; } > "$name/.golden-path.yaml"
rm -rf "$tpl"
git -C "$name" init -q
git -C "$name" add .
git -C "$name" commit -qm "Scaffold $name from golden-go-service $version"
echo "created $name"
EOF
chmod +x new-service.sh
./new-service.sh shortlinks team-growth
# ---- the team works on its service ----
cd shortlinks
sed -i 's/replicaCount: 2/replicaCount: 3/; s/cpu: 50m/cpu: 200m/' deploy/values.yaml
git commit -qam "Scale shortlinks for launch"
cd ..
# ---- the platform team releases v1.1.0 of the template ----
cd golden-go-service
sed -i 's/go-service.yaml@v3/go-service.yaml@v4/' skeleton/.github/workflows/ci.yaml
sed -i 's/cpu: 50m, memory: 64Mi}/cpu: 100m, memory: 64Mi}\n limits: {memory: 128Mi}/' skeleton/deploy/values.yaml
printf '\nRunbook: docs/runbook.md\n' >> skeleton/README.md
git commit -qam "v1.1.0: workflow v4, memory limit, runbook link" && git tag v1.1.0
cd ..
# ---- the updater: a three-way merge per file ----
cat > update-service.sh <<'EOF'
#!/bin/bash
# usage: update-service.sh <service-dir> <new-version>
set -uo pipefail
svc=$1 new=$2
old=$(yq '.version' "$svc/.golden-path.yaml")
yq '.answers' "$svc/.golden-path.yaml" > answers.yaml
for v in "$old" "$new"; do # render both template versions with the same answers
git clone -q --depth 1 --branch "$v" "file://$PWD/golden-go-service" "tpl-$v"
"tpl-$v/render.sh" "tpl-$v/skeleton" answers.yaml "render-$v"
done
git -C "$svc" switch -q -c "template-$new"
conflicts=0
for f in $(cd "render-$new" && find . -type f | sort); do
if [ ! -f "$svc/$f" ]; then cp "render-$new/$f" "$svc/$f"; echo "added $f"; continue; fi
git merge-file -L service -L "$old" -L "$new" "$svc/$f" "render-$old/$f" "render-$new/$f"
rc=$?
if [ $rc -gt 0 ]; then echo "CONFLICT $f"; conflicts=$((conflicts + rc)); else echo "merged $f"; fi
done
yq -i ".version = \"$new\"" "$svc/.golden-path.yaml"
rm -rf tpl-* render-* answers.yaml
echo "$conflicts conflict(s)"
EOF
chmod +x update-service.sh
./update-service.sh shortlinks v1.1.0
cd shortlinks
git status --short
echo "--- deploy/values.yaml"
cat deploy/values.yaml
echo "--- resolved: keep the team's CPU, take the template's limit"
sed -i '/^<<<<<<< /d; /^=======$/,/^>>>>>>> /{/limits/!d}' deploy/values.yaml
cat deploy/values.yaml
git commit -qam "Update to golden-go-service v1.1.0"
git log --oneline
git diff HEAD~1 --statcreated shortlinks
merged ./.github/workflows/ci.yaml
merged ./README.md
merged ./catalog-info.yaml
CONFLICT ./deploy/values.yaml
1 conflict(s)
M .github/workflows/ci.yaml
M .golden-path.yaml
M README.md
M deploy/values.yaml
--- deploy/values.yaml
image: ghcr.io/acme/shortlinks
replicaCount: 3
resources:
<<<<<<< service
requests: {cpu: 200m, memory: 64Mi}
=======
requests: {cpu: 100m, memory: 64Mi}
limits: {memory: 128Mi}
>>>>>>> v1.1.0
--- resolved: keep the team's CPU, take the template's limit
image: ghcr.io/acme/shortlinks
replicaCount: 3
resources:
requests: {cpu: 200m, memory: 64Mi}
limits: {memory: 128Mi}
7edddd1 Update to golden-go-service v1.1.0
d36edcd Scale shortlinks for launch
ae2edac Scaffold shortlinks from golden-go-service v1.0.0
.github/workflows/ci.yaml | 2 +-
.golden-path.yaml | 2 +-
README.md | 2 ++
deploy/values.yaml | 1 +
4 files changed, 5 insertions(+), 2 deletions(-)
Reading the result:
ci.yaml,README.mdchanged only in the template: merged automatically.catalog-info.yamlchanged on neither side.deploy/values.yamlshows the interesting cases.replicaCount: 3changed only on the team's side and was kept. The template's newlimitsline sits right next to therequestsline that both sides changed (team: 200m, template: 100m), so Git can't decide: conflict markers, labelled with the service and the two template versions.- The resolution keeps the team's CPU and takes the template's limit. The update is a normal commit on a branch (
template-v1.1.0), so in real life it becomes a pull request that CI tests and the team reviews. .golden-path.yamlnow saysv1.1.0, so the next update uses v1.1.0 as its base.
Rolling updates out to many services
At platform scale, a bot does this for every service:
- Find all services created from the template (search the catalog, or every repo's
.golden-path.yaml). - For each one behind the latest version, run the update on a branch and open a pull request with the template's release notes in the description. Renovate can do the "bump the reusable workflow/library chart version" part by itself.
- Label PRs with conflicts so the team knows it needs a human.
- Track adoption: how many services are on the newest version, how old the oldest is. That number belongs on the platform's dashboard (Platform KPIs).
services = { # service -> template version, read from each repo's .golden-path.yaml
"shortlinks": "1.1.0", "redirector": "1.1.0", "analytics": "1.0.0",
"qr-codes": "0.9.2", "abuse-filter": "1.1.0", "billing-sync": "0.8.0",
}
latest = "1.1.0"
key = lambda v: tuple(int(p) for p in v.split("."))
behind = {s: v for s, v in services.items() if key(v) < key(latest)}
print(f"on latest {latest}: {len(services) - len(behind)}/{len(services)}")
for s, v in sorted(behind.items(), key=lambda kv: key(kv[1])):
major_gap = key(latest)[0] - key(v)[0]
print(f" {s:13} {v:6} {'major update' if major_gap else 'minor update'}")on latest 1.1.0: 3/6
billing-sync 0.8.0 major update
qr-codes 0.9.2 major update
analytics 1.0.0 minor update
Making updates cheap
- Keep the copied surface small. Every line in the skeleton is a line that can conflict. Move shared logic into versioned things the service only references: reusable CI workflows, a Helm library chart, a base Kustomization, a shared dashboard generator.
- Semver the template. A major version means "needs manual work"; say what in the release notes.
- Never re-render without the recorded answers, and never edit
.golden-path.yamlby hand except to fix answers. - Test updates in the template's CI: scaffold with v(N-1), apply some typical team changes, update to vN, build and validate the result.
Test yourself
In a template update, what is the 'base' of the three-way merge?
- The service today
- The new template version rendered with the service's answers
- The old template version rendered with the service's answers
- An empty directory
The team changed line 5 of a file and the new template changed line 20 of the same file. What happens?
- Conflict
- Both changes are applied automatically
- The template wins
- The team's version wins
What is the best way to reduce conflicts in template updates?
- Forbid teams from editing files
- Keep the copied surface small: move shared logic into versioned references such as reusable workflows and library charts
- Update less often
- Disable git merge-file