Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 11 additions & 113 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,122 +18,20 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

defaults:
run:
shell: bash

env:
REGISTRY: ghcr.io

jobs:
build-and-push:
name: Build and push
runs-on: ubuntu-latest
timeout-minutes: 30
# Pull requests build without logging in, pushing or attesting, so the write scopes are never used there.
docker:
name: Docker
uses: BSStudio/.github/.github/workflows/shared-publish.yml@14e6deaf34d65dd4db67c19f146449c1cf99c1cc # v1.3.0
permissions:
contents: read
packages: write
# Mint the OIDC token and write the build provenance attestation.
id-token: write
attestations: write
steps:
- name: Check out
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
# The version is counted from the last release tag.
fetch-depth: 0
persist-credentials: false

- name: Resolve build metadata
id: build-meta
env:
REF_TYPE: ${{ github.ref_type }}
REF_NAME: ${{ github.ref_name }}
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
run: |
echo "hash=${SHA:0:7}" >> "$GITHUB_OUTPUT"

if [ "$REF_TYPE" = "tag" ]; then
echo "version=${REF_NAME#v}" >> "$GITHUB_OUTPUT"
else
# Mirrors `appVersion` in next.config.ts, which versions local builds.
describe=$(git describe --tags --first-parent --long --abbrev=7 --match 'v[0-9]*.[0-9]*.[0-9]*' --exclude '*-*')
[[ $describe =~ ^v([0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)-g([0-9a-f]+)$ ]]
tag=${BASH_REMATCH[1]}
distance=${BASH_REMATCH[2]}
hash=${BASH_REMATCH[3]}
branch=${REF_NAME//[^0-9A-Za-z-]/-}

# Later commits must sort higher in Sentry, which compares the revision numerically and prereleases as text.
if [ "$distance" = 0 ]; then
echo "version=$tag" >> "$GITHUB_OUTPUT"
elif [ "$branch" = main ]; then
echo "version=$tag.$distance+$hash" >> "$GITHUB_OUTPUT"
else
echo "version=$tag.$distance+$branch.$hash" >> "$GITHUB_OUTPUT"
fi
fi

# ghcr has no uppercase in image names. metadata-action lowercases its own
# input, the attest step does not, so both read this one.
echo "image=$REGISTRY/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1

- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ steps.build-meta.outputs.image }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}

- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
build-args: |
NEXT_PUBLIC_SENTRY_DSN=${{ vars.NEXT_PUBLIC_SENTRY_DSN }}
NEXT_PUBLIC_SENTRY_ENVIRONMENT=${{ vars.NEXT_PUBLIC_SENTRY_ENVIRONMENT }}
NEXT_PUBLIC_APP_VERSION=${{ steps.build-meta.outputs.version }}
NEXT_PUBLIC_COMMIT_HASH=${{ steps.build-meta.outputs.hash }}
SENTRY_ORG=${{ vars.SENTRY_ORG }}
SENTRY_PROJECT=${{ vars.SENTRY_PROJECT }}
# Empty on pull requests, which switches source map upload off rather than
# minting a Sentry release for every PR build.
secrets: |
sentry_auth_token=${{ github.event_name != 'pull_request' && secrets.SENTRY_AUTH_TOKEN || '' }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
# Provenance handled by the attest step below; buildkit's own
# attestations would push untagged "unknown/unknown" manifests to ghcr.
provenance: false
sbom: false

- name: Attest build provenance
if: github.event_name != 'pull_request'
uses: actions/attest@1e69f48acb82d1966a394da916b4c1698aa569d6 # v4.2.2
with:
subject-name: ${{ steps.build-meta.outputs.image }}
subject-digest: ${{ steps.build-and-push.outputs.digest }}
# Keep the attestation in GitHub's store only; pushing to ghcr would add
# an untagged referrer artifact. Verify with:
# gh attestation verify oci://<image> --owner BSStudio
push-to-registry: false
with:
build-args: |
NEXT_PUBLIC_SENTRY_DSN=${{ vars.NEXT_PUBLIC_SENTRY_DSN }}
NEXT_PUBLIC_SENTRY_ENVIRONMENT=${{ vars.NEXT_PUBLIC_SENTRY_ENVIRONMENT }}
SENTRY_ORG=${{ vars.SENTRY_ORG }}
SENTRY_PROJECT=${{ vars.SENTRY_PROJECT }}
secrets:
sentry-auth-token: ${{ github.ref_type == 'tag' && secrets.SENTRY_AUTH_TOKEN || '' }}
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- BEGIN:nextjs-agent-rules -->

# This is NOT the Next.js you know
## This is NOT the Next.js you know

This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices.

Expand Down
9 changes: 3 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,17 @@ COPY . .
ARG NEXT_PUBLIC_SENTRY_DSN=""
# Empty falls back to NODE_ENV, i.e. production.
ARG NEXT_PUBLIC_SENTRY_ENVIRONMENT=""
ARG NEXT_PUBLIC_APP_VERSION="dev"
# No `.git` or git binary here, so CI passes what next.config.ts derives locally.
ARG NEXT_PUBLIC_COMMIT_HASH=""
ARG VERSION="dev"
ARG SENTRY_ORG=""
ARG SENTRY_PROJECT=""
ENV NEXT_PUBLIC_SENTRY_DSN=$NEXT_PUBLIC_SENTRY_DSN
ENV NEXT_PUBLIC_SENTRY_ENVIRONMENT=$NEXT_PUBLIC_SENTRY_ENVIRONMENT
ENV NEXT_PUBLIC_APP_VERSION=$NEXT_PUBLIC_APP_VERSION
ENV NEXT_PUBLIC_COMMIT_HASH=$NEXT_PUBLIC_COMMIT_HASH
ENV NEXT_PUBLIC_APP_VERSION=$VERSION
ENV SENTRY_ORG=$SENTRY_ORG
ENV SENTRY_PROJECT=$SENTRY_PROJECT

# Without the token `withSentryConfig` skips source map upload and the build still passes.
RUN --mount=type=secret,id=sentry_auth_token,env=SENTRY_AUTH_TOKEN \
RUN --mount=type=secret,id=SENTRY_AUTH_TOKEN,env=SENTRY_AUTH_TOKEN \
--mount=type=cache,id=next,target=/app/.next/cache \
pnpm build

Expand Down
13 changes: 5 additions & 8 deletions components/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import {
} from "@/components/ui/sidebar";
import { NAV_LABELS } from "@/lib/nav-labels";
import { canViewAdminArea } from "@/lib/permissions";
import { releaseLink } from "@/lib/release-link";
import type { UserRole } from "@/types";

const release = releaseLink(process.env.NEXT_PUBLIC_APP_VERSION ?? "dev");

type NavItem = {
title: string;
url: string;
Expand Down Expand Up @@ -192,18 +195,12 @@ export function AppSidebar({ role }: { role: UserRole }) {
</SidebarContent>
<SidebarFooter>
<a
href={
/^\d+\.\d+\.\d+$/.test(process.env.NEXT_PUBLIC_APP_VERSION ?? "")
? `https://github.com/BSStudio/backstage/releases/tag/v${process.env.NEXT_PUBLIC_APP_VERSION}`
: process.env.NEXT_PUBLIC_COMMIT_HASH
? `https://github.com/BSStudio/backstage/commit/${process.env.NEXT_PUBLIC_COMMIT_HASH}`
: "https://github.com/BSStudio/backstage"
}
href={release.url}
target="_blank"
rel="noopener noreferrer"
className="px-2 pb-1 text-[10px] text-muted-foreground/50 hover:text-muted-foreground group-data-[collapsible=icon]:hidden"
>
{process.env.NEXT_PUBLIC_APP_VERSION}
{release.label}
</a>
</SidebarFooter>
</Sidebar>
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ services:
args:
NEXT_PUBLIC_SENTRY_DSN: ${NEXT_PUBLIC_SENTRY_DSN:-}
NEXT_PUBLIC_SENTRY_ENVIRONMENT: ${NEXT_PUBLIC_SENTRY_ENVIRONMENT:-}
NEXT_PUBLIC_APP_VERSION: ${NEXT_PUBLIC_APP_VERSION:-dev}
NEXT_PUBLIC_COMMIT_HASH: ${NEXT_PUBLIC_COMMIT_HASH:-}
VERSION: ${VERSION:-dev}
SENTRY_ORG: ${SENTRY_ORG:-}
SENTRY_PROJECT: ${SENTRY_PROJECT:-}
restart: unless-stopped
Expand Down
4 changes: 2 additions & 2 deletions docs/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ A sync failure is tagged `sync.target`, `sync.operation`, `sync.job_id` and `mem

**And nothing is collected that the scrubber would only have to drop.** The SDK collects every `dataCollection` category by default, so the restrictive baseline is spelled out rather than inherited — `scrub.ts` is the second line of defence, not the only one. `userInfo`, `cookies` and `httpHeaders` are off and `httpBodies` is none; `urlQueryParams` keeps the query string but denies any key containing `name`, which is how a member's own name arrives at `/api/usernames/suggest?firstName=…&lastName=…` — deny terms match as case-insensitive substrings, so the one term covers every spelling. `databaseQueryData` is pinned for a different reason: it reaches nothing while tracing is off, and the pin is what keeps enabling tracing from shipping the values Prisma binds. The remaining span-scoped categories (`queues`, `genAI`, `graphQL`) have no integration behind them at all and are left alone.

**Release tagging.** Every version string has to parse as semver in Sentry, or the release gets no ordering, no `release.version` filter and no regression detection. Release tags stay `vX.Y.Z`, but the version drops the `v`, which Sentry's parser refuses. A commit carrying a release tag builds as `X.Y.Z`; anything else as `X.Y.Z.<commits since the tag>+<hash>`, or `…+<branch>.<hash>` off `main`, the branch reduced to `[0-9A-Za-z-]`. The distance goes in the fourth, *revision* number because Sentry compares that numerically but a prerelease as plain text, where `dev.10` sorts below `dev.9`; the build code after `+` takes no part in the ordering. Only `vX.Y.Z` tags count, so a prerelease tag never resets the count, and only along the first-parent line, so neither does a tag on a merged branch. A working tree with uncommitted changes never builds as the bare tag. `next.config.ts` derives this locally and the publish workflow in CI, which passes it in as `NEXT_PUBLIC_APP_VERSION` — the two are written to match and nothing ties them together.
**Release tagging.** Every image's version has to parse as semver in Sentry, or the release gets no ordering, no `release.version` filter and no regression detection. Release tags stay `vX.Y.Z`, but the version drops the `v`, which Sentry's parser refuses. A tag build is versioned `X.Y.Z`; every other image `0.0.0+<commit>`, with the commit's first seven characters, and creates no release, because production runs tagged releases only. The shared publish workflow passes the version in as `VERSION`, which the Dockerfile hands to `next build` as `NEXT_PUBLIC_APP_VERSION`; a build without it, such as a local `pnpm build`, is versioned `dev`, which is not semver, so it never creates a release or uploads source maps, even with a token.

The release name is `backstage@<version>`: release names are shared across the whole Sentry organization, so the project slug keeps them apart. `next.config.ts` builds it once into `NEXT_PUBLIC_SENTRY_RELEASE`, which `sentryInitOptions` sets on every runtime and `withSentryConfig` uploads source maps under, so the two cannot name different releases. Source map upload switches itself off unless `SENTRY_AUTH_TOKEN` is present — that secret belongs to the Docker build, not to a local `pnpm build`.
The release name is `backstage@<version>`: release names are shared across the whole Sentry organization, so the project slug keeps them apart. `next.config.ts` builds it once into `NEXT_PUBLIC_SENTRY_RELEASE`, which `sentryInitOptions` sets on every runtime and `withSentryConfig` uploads source maps under, so the two cannot name different releases. Source map upload and the release switch themselves off unless `SENTRY_AUTH_TOKEN` is present and the version is not `dev` — the publish workflow passes that secret to tag builds only.

**Client events tunnel through `/monitoring`.** `tunnelRoute` in `next.config.ts` makes the browser POST envelopes to our own origin instead of `sentry.io`, which ad blockers filter by hostname. The route is generated by the Sentry plugin, so it exists only in a real build — and it is in `publicPaths` in `proxy.ts`, because an error on the login page has no session cookie yet and a redirect would drop the POST body.

Expand Down
4 changes: 2 additions & 2 deletions docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

**Migrations run from the entrypoint**, not a separate Job — one replica, one deploy. `RUN_MIGRATIONS=false` opts a container out. The standalone bundle contains only what the server traced, so the image carries its own copy of the Prisma CLI, npm-installed at build time from the versions in `package.json` so it cannot drift from the generated client. `prisma.config.ts` is copied next to that `node_modules` tree because its `dotenv` and `prisma/config` imports have to resolve somewhere, and the config's relative migrations path then points at the copy beside it. Do not try to shrink that tree by deleting `@prisma/studio-core` or `@prisma/dev`: `build/cli.js` requires both eagerly and the CLI stops loading at all.

**Build args vs secrets.** Every `NEXT_PUBLIC_*` value is a plain build arg — `NEXT_PUBLIC_SENTRY_DSN`, `NEXT_PUBLIC_SENTRY_ENVIRONMENT`, `NEXT_PUBLIC_APP_VERSION` and `NEXT_PUBLIC_COMMIT_HASH`. A DSN is public, the rest name the build or the environment, and `next build` freezes all of them into the bundle, so setting any of them on the running container reports nothing at all. `NEXT_PUBLIC_SENTRY_RELEASE` is the exception: `next.config.ts` derives it from the version and it is not an input. An empty `NEXT_PUBLIC_SENTRY_ENVIRONMENT` falls back to `NODE_ENV`, which is what an unconfigured image reports as. `SENTRY_ORG` and `SENTRY_PROJECT` are build args too: they only address the upload target, and without them `withSentryConfig` has nowhere to send source maps. `SENTRY_AUTH_TOKEN` is the only real secret and comes in as a BuildKit secret mount; a build arg would land in the image history. The publish workflow derives the version from the ref it is building and its distance to the last release tag — hence the full-history checkout — and the hash from the commit, and reads `NEXT_PUBLIC_SENTRY_DSN`, `NEXT_PUBLIC_SENTRY_ENVIRONMENT`, `SENTRY_ORG` and `SENTRY_PROJECT` from repository *variables*, the token from a repository *secret*.
**Build args vs secrets.** Every `NEXT_PUBLIC_*` value comes from a plain build arg — `NEXT_PUBLIC_SENTRY_DSN` and `NEXT_PUBLIC_SENTRY_ENVIRONMENT` under their own names, `NEXT_PUBLIC_APP_VERSION` from `VERSION`. A DSN is public, the rest name the build or the environment, and `next build` freezes all of them into the bundle, so setting any of them on the running container reports nothing at all. `NEXT_PUBLIC_SENTRY_RELEASE` is the exception: `next.config.ts` derives it from the version and it is not an input. An empty `NEXT_PUBLIC_SENTRY_ENVIRONMENT` falls back to `NODE_ENV`, which is what an unconfigured image reports as. `SENTRY_ORG` and `SENTRY_PROJECT` are build args too: they only address the upload target, and without them `withSentryConfig` has nowhere to send source maps. `SENTRY_AUTH_TOKEN` is the only real secret and comes in as a BuildKit secret mount; a build arg would land in the image history. The image is built by the [shared publish workflow](https://github.com/BSStudio/.github/blob/main/docs/workflows.md#calling-the-shared-publish-workflow) of `BSStudio/.github`, which passes `VERSION` (see [Observability](observability.md)); `docker-publish.yml` adds `NEXT_PUBLIC_SENTRY_DSN`, `NEXT_PUBLIC_SENTRY_ENVIRONMENT`, `SENTRY_ORG` and `SENTRY_PROJECT` from repository *variables*, and the token from a repository *secret* on tag builds only.

**Workflows** (`.github/workflows/`), actions pinned to a commit SHA with the version in a comment, mirroring [BSStudio/request-manager](https://github.com/BSStudio/request-manager):

| Workflow | When | What |
| --- | --- | --- |
| `ci.yml` | PR, push to `main` | Biome + tsc, Vitest with coverage → Codecov, commitlint over the PR range |
| `checks.yml` | PR, push to `main` | BSStudio/.github's shared checks: actionlint and zizmor over the workflows, dependency review on PRs |
| `docker-publish.yml` | PR (build only), push to `main`, `v*.*.*` tags | Buildx → `ghcr.io/bsstudio/backstage`, provenance attestation |
| `docker-publish.yml` | PR (build only), push to `main`, `v*.*.*` tags | BSStudio/.github's shared publish workflow: Buildx → `ghcr.io/bsstudio/backstage`, provenance attestation |
| `authentik-contract.yml` | Weekly cron, dispatch, PRs touching `lib/authentik/**` | Diffs Authentik's published spec against `scripts/authentik-contract.json` |

`ci.yml` and `checks.yml` carry no `paths` filter on purpose: they are what `main` is protected on, and a required check that never runs on a docs-only PR blocks that PR instead of passing it. `ci.yml`'s `Lint and typecheck` and `Test` run `prisma generate` first — `app/generated/prisma/` is gitignored and `tsc` resolves through it.
Expand Down
11 changes: 11 additions & 0 deletions lib/release-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const repoUrl = "https://github.com/BSStudio/backstage";

// Tag builds are versioned X.Y.Z, other images 0.0.0+<commit>, local builds dev.
export function releaseLink(version: string): { label: string; url: string } {
if (/^\d+\.\d+\.\d+$/.test(version)) {
return { label: `v${version}`, url: `${repoUrl}/releases/tag/v${version}` };
}
const commit = version.split("+")[1];
if (commit) return { label: commit, url: `${repoUrl}/commit/${commit}` };
return { label: version, url: repoUrl };
}
Loading