Skip to content
Open
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
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ env:
NODE_VERSION: "24.19.0"

jobs:
# Parallel to `test` so lint and test failures surface on the same run; type checking lives in Build.
# Parallel to `test` so lint and test failures surface on the same run. Package type checking
# lives in Build; `scripts/` is checked here instead, because it is deliberately kept off
# `pnpm build`'s hot path (see AGENTS.md) and this is the cheap job.
lint:
name: Lint
runs-on: ubuntu-latest
Expand All @@ -39,6 +41,9 @@ jobs:
- name: Lint
run: pnpm lint:check

- name: Type-check scripts/
run: pnpm types:scripts

test:
name: Build and test
runs-on: ubuntu-latest
Expand Down
241 changes: 241 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
name: Preview

# Per-PR preview deployments, on the Cloudflare account named by the CLOUDFLARE_ACCOUNT_ID
# repository variable.
#
# SECURITY — read this before changing the triggers.
#
# Deploying a preview needs a Cloudflare API token that can create Workers, KV namespaces and R2
# buckets on a Cloudflare-owned account. This repository is public, so anyone can open a pull
# request. The load-bearing control is the TRIGGER, not any `if:` below: on a public repository
# GitHub structurally withholds repository secrets from `pull_request` runs whose head is a fork,
# so `secrets.CLOUDFLARE_API_TOKEN` interpolates to the empty string there and no fork PR can
# deploy anything. That is the same guarantee workers-sdk's deploy-previews.yml relies on.
#
# Therefore: `pull_request` only. Never `pull_request_target`, never `workflow_run`, never
# `issue_comment` — each of those runs privileged with the secret available while the code, the
# PR number, or the artifact naming it comes from an untrusted contributor.
#
# The `if:` conditions and the in-job guard are defence in depth, each fail-closed, and exist so
# that a future edit which weakens the trigger still does not leak the token. They are not what
# makes this safe today.
#
# The `cache: pnpm` below is deliberate and is not a poisoning path: a fork PR's Actions cache is
# scoped to `refs/pull/<n>/merge` and cannot be read from another PR or from `main`.

on:
pull_request:
types: [opened, synchronize, reopened, closed]
schedule:
# Nightly, off the hour. GitHub has no equivalent of GitLab's `environment.auto_stop_in`, so
# this is what stops abandoned previews from leaking a KV pair and an R2 bucket each.
- cron: "37 4 * * *"
workflow_dispatch:

# Escalated per job. The preview jobs need no write scope at all beyond the sticky comment.
permissions: {}

env:
NODE_VERSION: "24.19.0"

jobs:
deploy:
name: Deploy preview
if: >-
github.event_name == 'pull_request' &&
github.event.action != 'closed' &&
github.event.pull_request.head.repo.id == github.event.pull_request.base.repo.id &&
contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.pull_request.author_association) &&
github.event.pull_request.user.type != 'Bot' &&
github.head_ref != 'main' &&
github.repository_owner == 'cloudflare'
runs-on: ubuntu-latest
timeout-minutes: 45
concurrency:
# Never cancel: a half-applied preview is worse than a slow one, since the tiers are
# deployed in sequence and an interrupted run leaves the instance wired to stale previews.
group: preview-${{ github.event.pull_request.number }}
cancel-in-progress: false
permissions:
contents: read
pull-requests: write
steps:
# First, before any step can reference a secret. Catches a future edit that flips the
# trigger to `pull_request_target`, and a branch pushed by a since-demoted account or a bot.
- name: Verify the pull request is from a maintainer

@ndisidore ndisidore Aug 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protip: you can skip this whole step by adding a conditional like this to your if
https://github.com/cloudflare/cloudflare-os/blob/main/.github/workflows/bonk-pr.yml#L20

contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.pull_request.author_association)

env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
run: |
permission=$(gh api "repos/$GH_REPO/collaborators/$PR_AUTHOR/permission" \
--jq '.permission')
if [[ ! "$permission" =~ ^(admin|maintain|write)$ ]]; then
echo "$PR_AUTHOR has '$permission' on $GH_REPO; previews require write access."
exit 1
fi

- name: Check out repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

# Ahead of setup-node, which shells out to `pnpm store path` to find the directory it caches.
- name: Enable Corepack
run: corepack enable

- name: Set up Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: ${{ env.NODE_VERSION }}
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Deploy preview
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
PREVIEW_WORKERS_DEV_HOST: ${{ vars.PREVIEW_WORKERS_DEV_HOST }}
# Secrets, not vars: these three are uploaded to the backend's Previews settings with
# `wrangler preview secret bulk`, and never written into a config file — Wrangler prints
# the values it finds in one, and this log is public.
PREVIEW_ADMINS: ${{ secrets.PREVIEW_ADMINS }}
CF_ACCESS_AUD: ${{ secrets.CF_ACCESS_AUD }}
CF_ACCESS_ISS: ${{ secrets.CF_ACCESS_ISS }}
# AI Gateway, so a preview's chats use server-managed keys rather than asking each user
# for their own. Optional as a group: with CF_AI_GATEWAY unset the preview is BYOK, but
# once it is set the account id and the Run + Read token are required.
CF_AI_GATEWAY: ${{ secrets.CF_AI_GATEWAY }}
# This is delibaretely set to CF_OS_AI_GATEWAY_ACCOUNT_ID (gets uploaded as CF_AI_GATEWAY_ACCOUNT_ID)
CF_AI_GATEWAY_ACCOUNT_ID: ${{ secrets.CF_OS_AI_GATEWAY_ACCOUNT_ID }}
CF_AI_GATEWAY_API_TOKEN: ${{ secrets.CF_AI_GATEWAY_API_TOKEN }}
CF_AI_GATEWAY_PROVIDERS: ${{ secrets.CF_AI_GATEWAY_PROVIDERS }}
CF_AI_GATEWAY_WAI_DIRECT: ${{ secrets.CF_AI_GATEWAY_WAI_DIRECT }}
# The branch, because the preview name is the first label of its hostname: a preview reads
# as `my-branch-router.<subdomain>.workers.dev`. The cleanup job below passes the
# same ref; the nightly sweep matches previews back to pull requests by slugifying every
# recent head ref the same way.
PREVIEW_NAME: ${{ github.head_ref }}
run: node scripts/preview/preview.ts deploy

# In this same job, deliberately: handing the comment to a privileged second workflow is
# the pattern workers-sdk deleted, because the privileged half read the PR number out of an
# artifact the unprivileged half had named.
- name: Comment the preview URL
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
MARKER: "<!-- preview-deployment -->"
run: |
{
printf '%s\n\n' "$MARKER"
cat output/preview-comment.md
} > output/preview-comment-body.md
jq -n --rawfile body output/preview-comment-body.md '{body: $body}' \
> output/preview-comment.json

# No `| head -1`: the shell runs with `pipefail`, and gh would take a SIGPIPE.
matches=$(gh api "repos/$GH_REPO/issues/$PR_NUMBER/comments" --paginate --jq '
first(.[]
| select(.user.login == "github-actions[bot]")
| select(.body | startswith(env.MARKER))
| .id)')
id=${matches%%$'\n'*}

if [[ -n "$id" ]]; then
gh api --silent --method PATCH "repos/$GH_REPO/issues/comments/$id" \
--input output/preview-comment.json
else
gh api --silent --method POST "repos/$GH_REPO/issues/$PR_NUMBER/comments" \
--input output/preview-comment.json
fi

cleanup:
name: Delete preview
if: >-
github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.head.repo.id == github.event.pull_request.base.repo.id &&
github.event.pull_request.user.type != 'Bot' &&
github.head_ref != 'main' &&
github.repository_owner == 'cloudflare'
runs-on: ubuntu-latest
timeout-minutes: 30
concurrency:
group: preview-${{ github.event.pull_request.number }}
cancel-in-progress: false
permissions:
contents: read
steps:
# The default branch, not the PR's merge ref: a teardown needs the branch's name and nothing
# else from it.
- name: Check out repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.event.repository.default_branch }}
persist-credentials: false

- name: Enable Corepack
run: corepack enable

- name: Set up Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: ${{ env.NODE_VERSION }}
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Delete preview
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
PREVIEW_WORKERS_DEV_HOST: ${{ vars.PREVIEW_WORKERS_DEV_HOST }}
# The same ref the deploy job used, which is what names the preview. GitHub sets it on the
# closed event too. No admin or Access secret is needed to tear one down.
PREVIEW_NAME: ${{ github.head_ref }}
run: node scripts/preview/preview.ts delete

sweep:
name: Sweep abandoned previews
if: >-
(github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') &&
github.repository_owner == 'cloudflare'
runs-on: ubuntu-latest
timeout-minutes: 60
concurrency:
group: preview-sweep
cancel-in-progress: false
permissions:
contents: read
pull-requests: read
steps:
- name: Check out repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Enable Corepack
run: corepack enable

- name: Set up Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: ${{ env.NODE_VERSION }}
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Sweep previews whose PR is closed, or older than 7 days
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
PREVIEW_WORKERS_DEV_HOST: ${{ vars.PREVIEW_WORKERS_DEV_HOST }}
# Reads every recent pull request's head branch, to match live previews back to them.
GITHUB_TOKEN: ${{ github.token }}
run: node scripts/preview/preview.ts sweep
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ wrangler.staging.jsonc
# Site-specific dev server configs.
wrangler.dev.jsonc

# Scratch output from scripts/preview (the PR-preview comment body).
/output/

# macOS
.DS_Store

Expand Down
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ With that said, we are happy to accept small, trivially-verified PRs that fix a
If you have a big idea you'd like us to consider, feel free to [open a discussion](https://github.com/cloudflare/cloudflare-os/discussions) about it.

This policy may change in the future as the project matures. Until then, thank you for your understanding.

## What CI runs on your pull request

Lint, build and tests ([`ci.yml`](.github/workflows/ci.yml)) run on every pull request, including
those from forks.

Preview deployments ([`preview.yml`](.github/workflows/preview.yml)) do **not**, this is
deliberate. Deploying a preview requires a Cloudflare API token that can create Workers
and storage on a Cloudflare-owned account, and GitHub structurally withholds repository secrets
from `pull_request` runs whose head is a fork. If you see the preview job skipped on your PR,
that is working as intended — a maintainer will deploy one if the change needs manual review.
See [`.github/workflows/README.md`](.github/workflows/README.md).
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
"scripts": {
"build": "vp run -r --cache build",
"run-local": "node scripts/run-local.mjs",
"test": "node --test scripts/*.test.js && vp run --filter '!cloudflare-os' --cache test",
"test": "node --test 'scripts/**/*.test.js' 'scripts/**/*.test.ts' && vp run --filter '!cloudflare-os' --cache test",
"preview:config": "node scripts/preview/preview.ts config",
"preview:deploy": "node scripts/preview/preview.ts deploy",
"preview:delete": "node scripts/preview/preview.ts delete",
"preview:sweep": "node scripts/preview/preview.ts sweep",
"dev-client": "cd packages/workshop-frontend && pnpm run dev",
"dev-server": "node run-dev-server.js",
"clean": "vp run -r clean",
"lint:check": "vp lint",
"lint:fix": "vp lint --fix",
"types:check": "pnpm run build",
"lint": "pnpm run lint:check && pnpm run types:check",
"types:scripts": "tsc -p scripts/tsconfig.json",
"lint": "pnpm run lint:check && pnpm run types:scripts && pnpm run types:check",
"types:generate": "node scripts/generate-worker-types.mjs"
},
"devDependencies": {
"@types/node": "26.1.0",
"aws4fetch": "^1.0.20",
"jsonc-parser": "^3.3.1",
"typescript": "catalog:",
Expand Down
5 changes: 3 additions & 2 deletions packages/workshop-backend/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import type { ProductAnalyticsRecord } from "./analytics";
declare global {
namespace Cloudflare {
interface Env {
// Deployment-wide admin usernames.
ADMINS?: string[];
// Deployment-wide admin usernames: a JSON binding, or the same array as a JSON string
// (which is what a secret binding, can carry).
ADMINS?: string[] | string;

// Workers AI binding (injected by generate-wrangler-prod / run-dev-server; not in base wrangler.jsonc).
WORKERS_AI: Ai;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions scripts/env-passthrough.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ const EXPECTED = {
},
// `build-gatekeeper-configurator.mjs` is covered in detail by
// build-gatekeeper-configurator.test.js, which pins its reads against the shared task's `env`.
// `build-release.mjs` and `run-local.mjs` are invoked directly, never as vp tasks.
// `build-release.mjs`, `run-local.mjs` and `preview/` are invoked directly, never as vp tasks.
scripts: {
forwarded: ["VITE_FRONTEND_ERROR_REPORTING"],
external: ["CI_COMMIT_SHA", "CI_PIPELINE_IID", "VITE_BACKEND_HOST"],
external: [
"CF_ACCESS_AUD", "CF_ACCESS_ISS", "CF_AI_GATEWAY", "CF_AI_GATEWAY_ACCOUNT_ID",
"CF_AI_GATEWAY_API_TOKEN", "CF_AI_GATEWAY_PROVIDERS", "CF_AI_GATEWAY_WAI_DIRECT",
"CI_COMMIT_SHA", "CI_PIPELINE_IID", "CLOUDFLARE_ACCOUNT_ID", "CLOUDFLARE_API_TOKEN",
"GITHUB_REPOSITORY", "GITHUB_TOKEN", "PREVIEW_ADMINS", "PREVIEW_NAME",
"PREVIEW_WORKERS_DEV_HOST", "PREVIEW_WRANGLER", "VITE_BACKEND_HOST",
],
},
};

Expand Down
Loading
Loading