Validating Catalog Files in CI
Backstage is forgiving: an entity with a misspelt owner still appears in the catalog, just with a broken link, and an entity that fails validation shows up as a processing error in the portal's logs and in the entity inspector, which nobody reads. The fix is the same as for Kubernetes manifests (kubeconform): check catalog files in the pull request, where the author is still looking.
What to check
| Layer | Example rule | Tool |
|---|---|---|
| Syntax | valid YAML, one entity per document | yq |
| Schema | Component has spec.type, lifecycle, owner; lifecycle is one of the agreed values |
yq, or JSON Schema (ajv, check-jsonschema) |
| References | owner is an existing Group; system exists |
yq against the org file, or the Backstage API |
| House rules | production components have docs, a system and an on-call annotation | your own script, or scorecards |
Backstage itself ships a validator package (@backstage/catalog-model's entity validation) and the community has CLI wrappers around it, but the house rules are always yours, and they're easy to express with yq.
A catalog linter in the pipeline
The session sets up an org file and three repositories' catalog-info.yaml files, one of them with three mistakes, then runs check-catalog.sh the way a CI job would on each pull request:
mkdir -p org repos/linkly repos/analytics repos/qr-codes
cat > org/groups.yaml <<'EOF'
apiVersion: backstage.io/v1alpha1
kind: Group
metadata: {name: team-links}
spec: {type: team, children: []}
---
apiVersion: backstage.io/v1alpha1
kind: Group
metadata: {name: team-growth}
spec: {type: team, children: []}
EOF
cat > repos/linkly/catalog-info.yaml <<'EOF'
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: linkly
annotations: {backstage.io/techdocs-ref: "dir:.", pagerduty.com/service-id: P1LNK}
spec: {type: service, lifecycle: production, owner: team-links, system: links}
EOF
cat > repos/analytics/catalog-info.yaml <<'EOF'
apiVersion: backstage.io/v1alpha1
kind: Component
metadata: {name: analytics}
spec: {type: service, lifecycle: experimental, owner: group:team-growth}
EOF
cat > repos/qr-codes/catalog-info.yaml <<'EOF'
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: QR_Codes
spec: {type: service, lifecycle: prod, owner: team-qr}
EOF
cat > check-catalog.sh <<'EOF'
#!/bin/bash
# usage: check-catalog.sh <org.yaml> <catalog-info.yaml>...
set -uo pipefail
org=$1; shift
groups=$(yq -N 'select(.kind == "Group") | .metadata.name' "$org")
errors=0
fail() { echo " ERROR $1"; errors=$((errors + 1)); }
for f in "$@"; do
echo "$f"
for i in $(seq 0 $(( $(yq -N 'document_index' "$f" | wc -l) - 1 ))); do
e() { yq -N "select(document_index == $i) | $1" "$f"; }
[ "$(e .kind)" = Component ] || continue
name=$(e .metadata.name)
[[ $name =~ ^[a-z0-9]+(-[a-z0-9]+)*$ ]] || fail "$name: name must be lowercase words joined by '-'"
for field in type lifecycle owner; do
[ "$(e ".spec.$field")" != null ] || fail "$name: spec.$field is required"
done
lifecycle=$(e .spec.lifecycle)
case $lifecycle in experimental|production|deprecated) ;;
*) fail "$name: lifecycle '$lifecycle' is not experimental|production|deprecated" ;; esac
owner=$(e .spec.owner); owner=${owner#group:}
grep -qx "$owner" <<< "$groups" || fail "$name: owner '$owner' is not a known group"
if [ "$lifecycle" = production ]; then
[ "$(e '.metadata.annotations["backstage.io/techdocs-ref"]')" != null ] || fail "$name: production needs TechDocs"
[ "$(e .spec.system)" != null ] || fail "$name: production needs a system"
fi
done
done
echo "$errors error(s)"
[ "$errors" -eq 0 ]
EOF
chmod +x check-catalog.sh
./check-catalog.sh org/groups.yaml repos/*/catalog-info.yaml; echo "exit $?"repos/analytics/catalog-info.yaml
repos/linkly/catalog-info.yaml
repos/qr-codes/catalog-info.yaml
ERROR QR_Codes: name must be lowercase words joined by '-'
ERROR QR_Codes: lifecycle 'prod' is not experimental|production|deprecated
ERROR QR_Codes: owner 'team-qr' is not a known group
3 error(s)
exit 1
linkly and analytics pass (group:team-growth is accepted, because the script strips the optional kind prefix). QR_Codes breaks three rules. team-qr is the most valuable catch: an owner that doesn't exist is an incident waiting for a pager that rings nowhere. Notice what didn't fire: the "production needs TechDocs" rule, because the lifecycle prod isn't production. Rules that depend on other fields are only as good as those fields, so validate the enums first.
Note: in a real pipeline the org file comes from the catalog itself (the Backstage API, below) or from a central
orgrepository, so new teams don't need a pull request to every service repo.
Asking the live catalog with jq
Backstage's catalog REST API returns entities as JSON (GET /api/catalog/entities/by-query?filter=kind=component), each with its computed relations. A trimmed response, saved as a file, and the questions you'd ask it:
cat > entities.json <<'EOF'
{"items": [
{"kind": "Component", "metadata": {"name": "linkly", "annotations": {"backstage.io/techdocs-ref": "dir:."}},
"spec": {"lifecycle": "production", "owner": "team-links"},
"relations": [{"type": "ownedBy", "targetRef": "group:default/team-links"},
{"type": "dependsOn", "targetRef": "resource:default/linkly-db"}]},
{"kind": "Component", "metadata": {"name": "analytics", "annotations": {}},
"spec": {"lifecycle": "experimental", "owner": "team-growth"},
"relations": [{"type": "ownedBy", "targetRef": "group:default/team-growth"},
{"type": "dependsOn", "targetRef": "resource:default/linkly-db"}]},
{"kind": "Component", "metadata": {"name": "old-admin", "annotations": {}},
"spec": {"lifecycle": "production", "owner": "user:ada"},
"relations": [{"type": "ownedBy", "targetRef": "user:default/ada"}]}
], "totalItems": 3}
EOF
echo "production components without TechDocs:"
jq -r '.items[] | select(.spec.lifecycle == "production" and (.metadata.annotations["backstage.io/techdocs-ref"] | not)) | .metadata.name' entities.json
echo "owned by a person:"
jq -r '.items[] | select(any(.relations[]; .type == "ownedBy" and (.targetRef | startswith("user:")))) | .metadata.name' entities.json
echo "dependents per resource:"
jq -c '[.items[] | .metadata.name as $n | .relations[] | select(.type == "dependsOn") | {r: .targetRef, n: $n}] | group_by(.r) | map({(.[0].r): map(.n)}) | add' entities.jsonproduction components without TechDocs:
old-admin
owned by a person:
old-admin
dependents per resource:
{"resource:default/linkly-db":["linkly","analytics"]}
The same queries, run nightly against the full catalog, feed a "catalog health" report: entities without owners, owners that are people, production services without docs. In the portal itself, scorecards present the same checks per service.
Test yourself
Why validate catalog files in the pull request rather than relying on Backstage?
- Backstage can't read YAML
- Backstage accepts many mistakes silently (broken references, processing errors in logs); the PR is where the author can fix them
- To save memory
- Backstage has no API
In the session, why did the 'production needs TechDocs' rule not fire for QR_Codes?
- It has TechDocs
- Its lifecycle
prodis notproduction, so the conditional rule never applied - The script crashed
- QR_Codes is not a Component