Skip to content

修复第 2 轮(CI/部署安全):按审查报告加固 GitHub Actions 工作流 - #118

Merged
todosdev[bot] merged 2 commits into
mainfrom
tds/conv-01a149b2-c844-78ee-a0ef-92936234aa0e
Oct 17, 2026
Merged

todosdev[bot] merged 2 commits into
mainfrom
tds/conv-01a149b2-c844-78ee-a0ef-92936234aa0e

Conversation

@todosdev

@todosdev todosdev Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Hardens the GitHub Actions workflows per the CI/deploy section of the code-review report (round 2: CI-C2–C8). The changes are least-privilege tokens, removing the long-lived PAT_TOKEN, deploying exactly the commit CI verified, and putting production deploys behind an environment, a concurrency group and a post-deploy smoke test.
  • D1 migrations now run in the deploy pipeline, before the code is deployed. For databases originally created by the HTTP /setup route, a prep script first records the already-applied migrations as a baseline so wrangler doesn't re-run them.
  • Only config and workflow files change. Nothing under apps/*/src is touched, to avoid conflicts with the parallel round-1 security PR.

Changes

  • ci.yml
    • Top-level permissions: contents: read; checkout uses persist-credentials: false.
    • pnpm install --frozen-lockfile.
    • New audit job running pnpm audit --prod --audit-level=high. It is non-blocking (continue-on-error) because of existing react-router@7.17.0 advisories (2 high, 3 moderate; fixed in ≥ 7.18.2).
  • tag.yml / release.yml
    • tag.yml drops PAT_TOKEN and pushes the tag with GITHUB_TOKEN (contents: write on that job only). The user-supplied tag reaches the scripts through env, not inline expressions.
    • A tag pushed with GITHUB_TOKEN does not trigger other workflows, so tag.yml calls release.yml through the new workflow_call entry point.
    • release.yml keeps its push: tags v* trigger, so a manually pushed tag still releases. Top-level permissions: {} with job-scoped contents: write; the unneeded checkout is removed.
  • deploy.yml
    • The job only runs when CI succeeded on a push to main of this repo. The old branches: [main] filter alone also matched fork PRs from a branch named main. Manual dispatch is allowed only from main.
    • Checks out workflow_run.head_sha with persist-credentials: false.
    • Adds environment: production (url vars.APP_URL) and a concurrency: deploy-production group that never cancels a deploy already running.
    • Resource IDs are passed through env and format-checked before the sed injection. The step fails if any _PLACEHOLDER is left in wrangler.toml.
    • Step order: install → inject → build → prepare/apply D1 migrations → deploy. Migrations run before the code, so they must stay backward compatible (expand → deploy → contract).
    • Smoke test: /api/health must return status == "ok" and / must return successfully. /api/config is also checked when the pipeline manages migrations.
    • When the smoke test fails, the job logs a wrangler rollback hint; nothing is rolled back automatically.
  • .github/scripts/d1-prepare-migrations.sh (new)
    • Detects the state of the remote D1 database and decides whether wrangler migrations can run.
    • Baselines d1_migrations from the setup route's _schema_migrations, using the convention schema_vN ⇔ migrations/000N_*.sql.
    • If tables exist but there is no history at all, it baselines 0001_init.sql only after confirming every 0001 table and links.user_seq exist; otherwise it fails.
    • Skips the migration steps (with a warning) when TABLE_PREFIX is set or prefixed tables are detected.
  • apps/worker/package.json
    • db:migrate now passes --remote. Under wrangler v4 it previously applied migrations to the local database.
    • Both migrate scripts target the binding DB instead of the name shortlink, which did not match the placeholder in wrangler.toml.
  • turbo.json: the deploy task gets "cache": false, so a cache hit can no longer skip wrangler deploy.

Required GitHub configuration

  • Create the production environment with required reviewers, and restrict its deployment branches to main.
  • Add a variable APP_URL. The smoke test fails without it.
  • CLOUDFLARE_API_TOKEN needs D1 edit permission for the migration step.
  • If TABLE_PREFIX is set in Cloudflare, mirror it as a GitHub variable.
  • If tag rulesets exist, allow GitHub Actions to create v* tags.
  • The PAT_TOKEN secret is no longer used; deleting it is optional.

Testing

  • actionlint 1.7.12 (with shellcheck) passes on all workflows; shellcheck passes on the script; pnpm check passes.
  • d1-prepare-migrations.sh was run against local D1 (with --remote swapped for --local) in these states:
    • empty database → apply
    • setup-route database with schema_v1 recorded → baselines 0001_init.sql, and wrangler then reports "No migrations to apply"
    • database already tracked by wrangler → apply
    • prefixed tables → skip
    • TABLE_PREFIX set → skip
    • full 0001 schema with no history → baselines 0001_init.sql
    • partial schema / missing links.user_seq → fails
  • Confirmed that wrangler 4.103.0 accepts the binding name DB for d1 migrations, and that pnpm audit works from the lockfile alone, without node_modules.
  • Not run, needs a real GitHub run: environment approval, workflow_call permission flow, remote migrations against production D1, and the smoke test against the live URL.

Todos added 2 commits October 17, 2026 19:59
- CI-C3/C4: least-privilege permissions; tag.yml drops PAT and calls release.yml
- CI-C2/C8: pin deploy to CI head_sha, production env, concurrency, smoke test
- CI-C5/C6/C7: D1 migrations before deploy, frozen lockfile + audit, validate IDs

Co-Authored-By: Claude Code <noreply@anthropic.com>

Checkpoint: gKV4K2rDLsiq3Qz1ay8M5
When tables exist without any migration history, check that every
0001_init.sql table and links.user_seq exist before marking it applied;
otherwise fail instead of baselining a partial or drifted schema.

Checkpoint: 8BGviyy8TdefHfg4_zijF
@todosdev
todosdev Bot merged commit f54b3e8 into main Oct 17, 2026
1 of 2 checks passed
@mapxn
mapxn deleted the tds/conv-01a149b2-c844-78ee-a0ef-92936234aa0e branch October 18, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants