TExcelTAD
Home › Platform Engineering: Internal Developer Platforms › Backstage Software Templates

Backstage Software Templates

Backstage's scaffolder turns a golden path into a form in the portal. A developer fills in a few fields, presses Create, and the scaffolder runs a list of actions: fetch and render the skeleton, publish a repository, register it in the catalog, open a pull request elsewhere. The template is itself a catalog entity, kind: Template, written in YAML and stored in Git like everything else.

A template for linkly-style services

yaml
apiVersion: scaffolder.backstage.io/v1beta3
kind: Template
metadata:
  name: go-service
  title: Go HTTP service
  description: A Go service with CI, a Helm chart, GitOps delivery, dashboards and SLOs.
  tags: [go, recommended]
spec:
  owner: group:team-platform
  type: service

  parameters:                        # the form: JSON Schema, one object per form page
    - title: About the service
      required: [name, owner]
      properties:
        name:
          title: Name
          type: string
          pattern: '^[a-z][a-z0-9-]{1,28}[a-z0-9]
  


          ui:autofocus: true
        description:
          title: Description
          type: string
        owner:
          title: Owner
          type: string
          ui:field: OwnerPicker
          ui:options:
            catalogFilter:
              kind: Group
        tier:
          title: Tier
          type: string
          enum: [critical, standard, experimental]
          default: standard
    - title: Repository
      required: [repoUrl]
      properties:
        repoUrl:
          title: Repository location
          type: string
          ui:field: RepoUrlPicker
          ui:options:
            allowedHosts: [github.com]
            allowedOwners: [acme]

  steps:
    - id: fetch
      name: Render the skeleton
      action: fetch:template
      input:
        url: ./skeleton
        values:
          name: ${{ parameters.name }}
          description: ${{ parameters.description }}
          owner: ${{ parameters.owner }}
          tier: ${{ parameters.tier }}

    - id: publish
      name: Create the repository
      action: publish:github
      input:
        repoUrl: ${{ parameters.repoUrl }}
        description: ${{ parameters.description }}
        defaultBranch: main
        repoVisibility: internal

    - id: gitops
      name: Add the service to the GitOps repository
      action: publish:github:pull-request
      input:
        repoUrl: github.com?owner=acme&repo=platform-deploy
        branchName: add-${{ parameters.name }}
        title: Add ${{ parameters.name }}
        description: Created by the go-service template for ${{ parameters.owner }}.
        sourcePath: ./gitops
        targetPath: apps/${{ parameters.name }}

    - id: register
      name: Register in the catalog
      action: catalog:register
      input:
        repoContentsUrl: ${{ steps.publish.output.repoContentsUrl }}
        catalogInfoPath: /catalog-info.yaml

  output:
    links:
      - title: Repository
        url: ${{ steps.publish.output.remoteUrl }}
      - title: GitOps pull request
        url: ${{ steps.gitops.output.remoteUrl }}
      - title: Open in catalog
        icon: catalog
        entityRef: ${{ steps.register.output.entityRef }}

How it fits together:

  • parameters is JSON Schema plus ui: hints for the form (react-jsonschema-form). OwnerPicker and RepoUrlPicker are custom fields that query the catalog and build the github.com?owner=acme&repo=shortlinks string the publish actions expect.
  • steps run in order in the Backstage backend. Each step's input can use ${{ parameters.* }} and the outputs of earlier steps (${{ steps.publish.output.repoContentsUrl }}).
  • fetch:template renders ./skeleton (relative to the template file) with Nunjucks into the run's workspace; the following publish actions push that workspace. fetch:plain copies files without rendering.
  • The gitops step shows how a golden path reaches beyond the new repo: it opens a pull request in the GitOps repository with the files from the skeleton's gitops/ folder, so the platform team (or a policy check) reviews the new namespace and Application. A real template would render that folder in its own fetch:template step and keep it out of the service repo.
  • output.links become buttons on the result page.

Available actions depend on the backend modules you install (GitHub, GitLab, Azure DevOps, Bitbucket, plus community modules); the scaffolder's /create/actions page lists the installed ones with their input schemas. You can also write custom actions in TypeScript, for example to call your own provisioning API.

How the scaffolder runs a template, modelled

The interesting part of the engine is the data flow: parameters and step outputs feed the next step's inputs. A small model with fake actions:

python · try it yourself
import re

parameters = {"name": "shortlinks", "owner": "group:team-growth",
              "repoUrl": "github.com?owner=acme&repo=shortlinks"}

steps = [
    {"id": "fetch", "action": "fetch:template",
     "input": {"url": "./skeleton", "values": {"name": "${{ parameters.name }}"}}},
    {"id": "publish", "action": "publish:github",
     "input": {"repoUrl": "${{ parameters.repoUrl }}"}},
    {"id": "register", "action": "catalog:register",
     "input": {"repoContentsUrl": "${{ steps.publish.output.repoContentsUrl }}",
               "catalogInfoPath": "/catalog-info.yaml"}},
]

def fake_action(action, inp):
    if action == "fetch:template":
        return {}
    if action == "publish:github":
        q = dict(kv.split("=") for kv in inp["repoUrl"].split("?")[1].split("&"))
        base = f"https://github.com/{q['owner']}/{q['repo']}"
        return {"remoteUrl": base, "repoContentsUrl": f"{base}/blob/main"}
    if action == "catalog:register":
        return {"entityRef": "component:default/shortlinks"}

def resolve(value, ctx):
    if isinstance(value, dict):
        return {k: resolve(v, ctx) for k, v in value.items()}
    def lookup(m):
        obj = ctx
        for part in m.group(1).split("."):
            obj = obj[part]
        return obj
    return re.sub(r"\$\{\{ ([\w.]+) \}\}", lookup, value)

ctx = {"parameters": parameters, "steps": {}}
for step in steps:
    inp = resolve(step["input"], ctx)
    out = fake_action(step["action"], inp)
    ctx["steps"][step["id"]] = {"output": out}
    print(f"[{step['id']}] {step['action']}")
    for k, v in inp.items():
        print(f"    in  {k} = {v}")
    for k, v in out.items():
        print(f"    out {k} = {v}")
[fetch] fetch:template
    in  url = ./skeleton
    in  values = {'name': 'shortlinks'}
[publish] publish:github
    in  repoUrl = github.com?owner=acme&repo=shortlinks
    out remoteUrl = https://github.com/acme/shortlinks
    out repoContentsUrl = https://github.com/acme/shortlinks/blob/main
[register] catalog:register
    in  repoContentsUrl = https://github.com/acme/shortlinks/blob/main
    in  catalogInfoPath = /catalog-info.yaml
    out entityRef = component:default/shortlinks

Writing templates that age well

  • Keep the skeleton in the template's repository, next to template.yaml, and register the template through a catalog location. Version it with tags like any other golden path (Scaffolding).
  • Test the template in CI: render the skeleton with sample values (the backstage-cli and the scaffolder's Template Editor page help locally; a dry run is available through the scaffolder API) and validate the output as in the Mini Project.
  • Few parameters. Every field is a decision the platform didn't make.
  • Protect the scaffolder's credentials. The backend holds a token (better: a GitHub App) that can create repositories in your organisation: scope it tightly and log every run.
  • Template != update mechanism. Backstage renders once; plan how existing services get template changes (Keeping Services in Step).
  • Escape GitHub Actions expressions in skeleton files: ${{ github.sha }} would be rendered by Nunjucks, so wrap such blocks in {% raw %}…{% endraw %}.

Test yourself

How does a later step use the repository URL created by publish:github?

  1. It can't
  2. Through ${{ steps.publish.output.repoContentsUrl }} (or remoteUrl)
  3. From an environment variable
  4. By querying GitHub again

What is the parameters section of a v1beta3 template?

  1. Helm values
  2. JSON Schema (with ui: hints) that defines the form the developer fills in
  3. Environment variables
  4. A list of actions

Which action renders the skeleton directory with the template's values?

  1. fetch:plain
  2. fetch:template
  3. catalog:register
  4. publish:github