Publish to GitHub
About 953 words
Backstage can write a policy's rendered file to a GitHub repository: as one pull request it keeps up to date, or as a direct push. Applying the file to the tailnet stays with that repository, for example with Tailscale's GitOps action. This guide sets that up.
1. Say where the file goes
Add spec.publish to the policy entity:
apiVersion: specht-labs.de/v1alpha1
kind: Tailscale
metadata:
name: my-tailnet
spec:
type: policy
owner: group:platform
tailnet: example.ts.net
publish:
repo: acme/network
path: tailscale/policy.hujson
flow: pullRequest| Field | Required | Meaning |
|---|---|---|
repo | yes | owner/name of a repository on GitHub.com |
path | yes | the file in that repository, such as tailscale/policy.hujson |
flow | no | pullRequest (the default) or push |
branch | no | the branch the file lands on; the repository's default branch if unset |
pullRequestBranch | no | for pullRequest, the branch Backstage owns and force-pushes; backstage/tailscale-<policy name> if unset |
2. Allow the repository in Backstage
The catalog says where each policy goes. Backstage's own config says which repositories it may write to at all:
# app-config.yaml
tailscale:
allowedRepos:
- acme/networkThe comparison ignores case. A policy whose repository isn't listed isn't published: the console shows Not published: acme/network is not in Backstage's tailscale.allowedRepos, and the backend logs a warning on every run. This keeps someone who can edit a policy entity from pointing Backstage at a repository you didn't mean it to write to.
3. Give Backstage credentials
The backend uses the credentials of Backstage's GitHub integration for github.com. A GitHub App is the better choice, since its access is limited to the repositories you install it on:
# app-config.yaml
integrations:
github:
- host: github.com
apps:
- $include: github-app-backstage-credentials.yamlThe App needs these repository permissions on the policy repository:
| Permission | Access | Used for |
|---|---|---|
| Contents | Read and write | reading the current file, creating the commit, updating the branch |
| Pull requests | Read and write | finding, opening, updating and closing the pull request |
| Metadata | Read | always required by GitHub; reading the default branch |
With flow: push, Contents is enough, but keep Pull requests if you might switch flows. A personal access token works too (token: in place of apps:); a fine-grained one needs the same permissions.
GitHub Enterprise Server isn't supported. The backend calls https://api.github.com directly.
4. Wait for the hour, or run it now
The backend schedules a task, tailscale-publish, that runs every 60 minutes, starting three minutes after Backstage starts so the catalog can settle. Each run reads the catalog fresh, renders every policy with spec.publish, and publishes each one. A run that takes longer than five minutes is stopped.
To run it right away, trigger tailscale-publish from the Scheduled Tasks view of Backstage's DevTools plugin, if your Backstage has it.
The console's header shows where each policy goes and links to the pull request or commit of the last run. The last run's outcome is kept in memory: a restart clears it, and with several backend replicas only the one that ran the task knows it.
What pullRequest does
Each run:
- Reads the file at
pathon the base branch (branch, or the default branch). If it already matches the rendered file, Backstage closes its open pull request, if there is one, and stops. - If
pullRequestBranchdoesn't hold the rendered file yet, Backstage builds one commit with the file on top of the base branch's head and pointspullRequestBranchat it, creating the branch if needed. The branch is force-pushed, so it always holds exactly one commit and the pull request shows exactly what the catalog would change. - Updates the title and description of the open pull request from
pullRequestBranch, or opens one.
Don't push your own commits to pullRequestBranch: the next change to the rendered file replaces them. Change the entities in the catalog instead.
The pull request's title is feat(tailscale): render <tailnet> (<n> grants, <n> SSH rules, <n> tests), using the policy's tailnet or, without one, its name. The description has:
- the policy entity (a link when
app.baseUrlis set) and the number of its parts; - a table with the number of entries in each section;
- how many grants come from the catalog, and how many consumptions a grant entity already allows;
- a Problems section with every error and warning of the policy;
- a reminder to change the catalog, not the file.
The commit message is the title followed by Rendered by Backstage from the Tailscale entities in its catalog.
Problems don't stop publishing. A policy with errors still gets its pull request, with the errors listed in it, so read the Problems section before you merge. An unresolved group, for example, renders as written and may not mean what you expect.
What push does
Each run reads the file on branch (or the default branch). If it differs from the rendered file, Backstage commits the file on top of the branch's head and moves the branch to that commit, without force. If someone pushed in between, the update fails, the run logs the error, and the next run tries again on top of their commit.
A branch protection rule that blocks direct pushes makes every run fail. Use pullRequest for protected branches.
Applying the file
Backstage stops at the repository. Set up the repository so that a merge to the base branch applies the file to the tailnet, and so that pull requests test it first. Tailscale's GitOps action can do both: test the file on a pull request, and apply it on merge.
