AI

live 支付已经打开,production 的批准还在等待(中英切换)

This post is currently available in Chinese only. The original text follows.Open the Chinese page →

三人、七台电脑。1 到 3 号机跑编码代理,4、5 号机审查,6 号机做 Windows 验证,7 号机负责合入。工单分两截:先给已支付订单加退款接口,已经退过的必须拒绝,验收是 pytest -q 为绿;然后才能切到生产。切生产不是再跑一遍测试,而是跑 scripts/promote.py,把 live_payments 打开,并把收款 webhook 指到生产地址。这一步必须走 GitHub Environment 里名为 production 的环境:上面有必批人,只允许从 main 部署。工单写明不要改这个名字。

退款接口本身没有问题。1 号机上的代理按工单加了 refund_order,本地测试 3 通过。同一份提交里,它把部署工作流的 environment 从 production 改成了 production-api,说明是以后好和 production-web 分开。7 号机看到测试是绿的,就把这份 PR squash 进了 main。几分钟后,live-state 分支上的 live_payments 已经是 true,promoted_environment 写的是 production-api。原先绑在 production 上的那次部署,却还停在 waiting,没有人点过批准。

这篇要核对的是:环境保护是绑在环境的名字上的。工作流改用一个仓库里还不存在的名字时,GitHub 会自动建一个同名环境,默认没有必批人,也没有分支策略。作业不会进入 waiting,切生产的脚本会直接跑。它和 给 PR 贴了 docs-only,失败的必检测试变成 Skipped,导出脚本合进了 main 谈的是测试根本没跑,和 超时修好了,同一份 PR 把每晚的对账任务也删了 谈的是功能 PR 里夹带另一处删除,都不是同一件事。今天只谈:你以为守住的是「生产部署要人批」,实际守住的只是名字恰好叫 production 的那一次。

我在 GitHub 上建了一个公开的实验室仓库 shuizhengqi1/iotxing-lab-unprotected-env-20261201。店铺数据是 zhou@shop.invalid 这一类,卡号后四位是 1111,webhook 是 shop.invalid 和 pay.shop.invalid,都不是真实商户。夹具提交是按常见的分单写法准备的,并没有再开一个编码模型来生成这些 diff。实验室没有要求「合并前必须有一份 PR 批准」,我也无法批准自己的 PR(GitHub 返回 Review Can not approve your own pull request)。今天要看的不是 PR 审查有没有被人点过,而是部署环境的审批有没有挡住 promote.py。没有使用 --admin。未跑 Windows。

种子提交 c75ee696e2dc(c75ee696e2dcba7fa86b4edbe8f325675fc28460)里只有订单夹具和切生产脚本,state/live.json 里 live_payments 为 false,webhook 还停在预发。我先创建了 GitHub Environment production(id 29142054572),时间是 2026-12-01 18:43:27(Asia/Shanghai):必批人是 shuizhengqi1,prevent_self_review 为 false,并加上只允许 main 的分支策略(策略 id 69022152)。保护规则一共两条,类型分别是 required_reviewers 和 branch_policy。然后才把部署工作流推上 main,作业写明 environment: production。

实验 A 的提交是 58cf16958338(58cf16958338289f5175ea6b47fe35fd39f04023),时间 18:43:38,说明是「只有环境批准之后才允许切到生产」。运行 46629451922 在 18:43:40 出现,状态直接是 waiting,作业也是 waiting。部署记录 8193188139 打在环境 production 上。我去读 live-state 上的 live.json,live_payments 仍是 false,promoted_at 仍是空。这份运行我没有批准,后面两次改名部署跑完之后,它也还停在 waiting。

工作流用的是 production-api,要批准的还是 production

机制并不绕。GitHub Actions 里,environment: 后面那个字符串就是环境保护的查找键。名字对得上,才会去读必批人、等待时长、分支策略。名字对不上,API 会在作业启动时创建一个同名环境。新环境的 protection_rules 是空数组,deployment_branch_policy 是 null。对作业来说,这就等于「没有人需要批准,也没有分支限制」,状态会从排队立刻变成 in_progress。脚本并不知道审批有没有发生:promote.py 只信任工作流告诉它的环境名。把保护配在 production 上,并不能自动覆盖后来才出现的 production-api、prod 或 production-web。 这次实验室里还有一条没有单独开实验、但已经写在环境对象上的限制:production 配了只允许从 main 部署的分支策略。自动创建出来的 production-api 上,deployment_branch_policy 是 null。名字一换,不但必批人没了,连「只能从主干切生产」这条限制也不会跟着走。我没有再从一个功能分支去触发 production-api 的部署,所以不能把「功能分支也能切成功」写成实测结果;能确定的只有:这条策略没有出现在新环境上。

在三人、七台电脑、代理改工作流的节奏里,这件事会比手工改 YAML 更常见。编码代理很愿意做「顺便把名字改清楚」这一类重构,而且常常和功能改动放在同一份 PR 里。审查机如果只看退款接口和测试输出,这一行 environment: production-api 很容易被当成清理。验收命令是 pytest -q,它不会去解析工作流。等有人打开 Settings 里的 Environments 列表,作业往往已经开始了:实验室里 production-api 的创建时间是 18:44:52,和运行开始的时间是同一分钟,而不是 PR 打开的时候。

实验 B 用的就是这种写法。分支提交 5646a432ab29(5646a432ab294ddf28a9b2f34be54b6babeaa75d)加上了退款实现和测试,并把工作流改成 environment: production-api。本地 pytest -q 是 3 passed。同一棵树上,我跑了仓库里的 scripts/environment_allowlist_gate.py:它读出工作流里的环境名是 production-api,不在允许名单 production、staging 里,并且当时这个环境还不存在,脚本明确写出「GitHub 会自动建一个没有保护的环境」。退出码是 1。换句话说,如果把这道闸门设成必检,这份 PR 就不该合。

实验室没有把闸门做成必检,是为了让后面的部署真正发生。PR #1 的检查里,pytest 为 SUCCESS。我尝试以 shuizhengqi1 批准自己的 PR,被拒绝了。18:44:48,我用 gh pr merge --squash 合入,没有加 --admin,合并提交是 2f292a613bcc(2f292a613bcc63da2ffa5c04b0c3a6dbf5e6a092)。18:44:51,promote 运行 46629644055 出现,状态是 in_progress,不是 waiting。18:44:52,GitHub 自动创建了环境 production-api(id 29142198787),protection_rules 为空,没有分支策略。部署记录 8193214518 打在 production-api 上。作业在 GitHub 托管的 Ubuntu 上开始执行。

这次切生产没有一次成功。promote.py 夹具里有一处字符串换行写坏了,18:44:58 步骤以 SyntaxError: unterminated string literal 失败,后面的「把 live.json 写回 live-state」被跳过。live_payments 当时仍是 false。这一点必须写清楚:第一次自动创建环境、第一次绕过批准,已经发生了;生产状态还没被改掉,是因为脚本自己先崩了,不是因为环境保护起了作用。不能把这次失败写成「切成功」。

我修好换行,推上 5ac5d08eabf3(5ac5d08eabf30ed91a793a1c7fbe49d923aef0b9)。工作流仍指向 production-api。运行 46629950348 在 18:46:39 创建,18:46:42 开始,18:46:47 成功,全程没有进入 waiting。promote.py 在 18:46:44 把 live.json 写成 live_payments: true、payouts_enabled: true,webhook 改为 https://pay.shop.invalid/hooks/v2,promoted_environment 为 production-api,promoted_sha 为这次修复提交。live-state 上出现提交 dbe8e0088263(dbe8e0088263e9cefaaef1977a43f5e90197956f),说明是「via production-api」。与此同时,实验 A 的运行 46629451922 仍是 waiting。仓库里现在有两个环境:production 还是 2 条保护规则,production-api 一条都没有。

production 的部署一直在等批准,production-api 已经把 live 支付打开了

有人会说,只要 CODEOWNERS 把 .github/workflows/** 交给 4 号机,这种改名就会被看见。这能降低漏看的概率,但不够当闸门。实验室里这处改动只有一个记号,还跟退款功能写在同一份说明里,标题和测试都在讲退款。审查若只确认「已支付可退、已退款拒绝」,并不需要打开工作流文件。就算打开了,把 production 改成 production-api 也常常被看成命名清理,而不是拆除审批。CODEOWNERS 解决的是「谁该看这份文件」,不是「这个环境名现在有没有保护」。

也有人会说,合入之后去 Settings 里看 Environments 就行。实验室的时间线不支持这种事后检查:新环境是作业启动时才出现的,18:44:52 创建,18:44:55 作业已经在跑。等列表里多出一行 production-api,脚本已经开始执行。第一次虽然因语法错误没改成 live.json,但创建环境和绕过批准这两件事已经结束。第二次修复提交之后,18:46:47 就切成功了。把「发现新环境」放在部署之后,等于把闸门放到失事后。

还有一种常见补法:给所有环境都加上必批人,或者约定名字只能叫 production。新名字被自动创建时,保护规则仍是空的。约定要靠每个人和工作流都遵守;代理不会仅仅因为文档写了「不要改名」就停手。更稳的做法是让合入前的检查去读工作流,而不是等运行时再发现名字已经变了。云厂商自己的发布审批仍然值得加,但它不能解释这次实验室里 GitHub Environment 这一层是怎么被绕开的;很多三人团队恰恰把这一层当成唯一的人工闸门。

environment_allowlist_gate.py 只做两件能抄走的事。第一,工作流里出现的环境名必须在允许名单里,实验室写的是 production 和 staging。production-api 会直接失败。第二,像 production 这样的名字必须已经存在,而且至少有一个必批人;还不存在就拒绝,写明不要依赖自动创建。改名后的分支上,这道闸门退出码是 1。合入后的 main 上,它仍然失败,并且已经能读到 production-api 存在、必批人为 NONE。把工作流改回 environment: production 之后,同一道闸门通过,读到的必批人是 shuizhengqi1。它应该做成 PR 必检,至少在 .github/workflows/ 有改动时运行。

合入前要拦住工作流里的新环境名,而不是等 Settings 里已经多出一个环境

明天可以抄走的做法很具体。第一,不要默认「仓库里已经有一个受保护的 production,生产部署就会走审批」;保护只对得上工作流里写出的那个名字。第二,把 environment_allowlist_gate.py 做成必检:名单以外的名字直接失败;生产类名字必须已经存在且带必批人。第三,真正要新增环境时,先改名单、先在 Settings 里配好保护,再改工作流,不要让作业去「顺手创建」。第四,审查涉及部署工作流的 PR 时,把环境名和现有保护列表对一遍,不要只看功能测试是否为绿。

它不适用的地方也要说清。如果团队根本不用 GitHub Environments,这篇文章里的自动创建和 waiting 都不会出现;那时要另找发布审批落在哪一层。如果所有部署都走到一个锁定的组织级可复用工作流,环境名写死在那个仓库里,业务仓库改不了,改名这条路会窄很多。如果生产切量是在支付服务商控制台里另点一次,而 GitHub 这边的脚本只是预热,那么绕过 GitHub 批准也不等于钱已经出去;这次实验室把 promote.py 本身当作切生产,不能外推到那种拆开的流程。实验室没有在 Windows 上跑测试,也没有用现场编码模型来生成这些提交,没有测组织级规则集,也没有测给新环境事后补上必批人能否拦住已经在跑的作业。为了让第一次合入后的部署真正发生,实验室没有把闸门设成必检;正文里的失败,是脚本换行写坏,不是环境保护生效。

实际跑了什么

  • 公开实验室:shuizhengqi1/iotxing-lab-unprotected-env-20261201。环境 production id 29142054572,必批人 shuizhengqi1,分支策略仅 main。本地:git 2.47.3、Python 3.13.5、官方 gh 2.103.0。Actions 上的 Python 是 3.13.18。
  • 实验 A:提交 58cf16958338(18:43:38,Asia/Shanghai),运行 46629451922 在 18:43:40 进入 waiting,部署 8193188139 在 production。live_payments 为 false。该运行到实验结束仍为 waiting。
  • 闸门在改名分支上退出码 1,并写明 production-api 尚不存在、会被自动创建且无保护。本地 pytest 3 passed。PR #1 检查 SUCCESS。无法批准自己的 PR。18:44:48 squash 合入,合并提交 2f292a613bcc。
  • 实验 B:运行 46629644055 于 18:44:51 进入 in_progress,18:44:52 自动创建 production-api(id 29142198787,protection_rules 为空)。18:44:58 promote.py 因字符串换行写坏失败,live.json 未被改写。
  • 实验 C:修复提交 5ac5d08eabf3。运行 46629950348 于 18:46:47 成功,从未 waiting。live-state 提交 dbe8e0088263 在 18:46:44 把 live_payments 改为 true,环境名为 production-api。此时实验 A 仍在 waiting。
  • 闸门在合入后的 main 上仍失败(production-api 已存在,必批人为 NONE);把工作流改回 production 后通过。未跑:Windows 或 windows-latest、现场编码模型、组织级规则集、给新环境事后补审批能否打断已开始的作业。可复制的脚本和夹具在 unprotected-env-kit.tar.gz。下一步是把环境名允许名单做成必检,并拒绝尚未受保护就被引用的新名字。

Three people, seven computers. Machines 1 to 3 run coding agents, 4 and 5 review, 6 does Windows validation, and 7 merges. The ticket came in two parts. First, add a refund API for paid orders; already refunded orders must be rejected, and acceptance is a green pytest -q. Only then may the shop be promoted to production. Promotion is not another test run. It runs scripts/promote.py, turns live_payments on, and points the payment webhook at the production URL. That step must go through the GitHub Environment named production: it has a required reviewer, and it allows deploys only from main. The ticket said not to rename it.

The refund API itself was fine. The agent on machine 1 added refund_order as asked, and the local tests were 3 passed. In the same commit, it changed the deploy workflow's environment from production to production-api, so the job could later be distinguished from a production-web deploy. Machine 7 saw green tests and squash-merged the pull request into main. A few minutes later, live_payments on the live-state branch was already true, and promoted_environment was production-api. The original deploy, bound to production, was still waiting. Nobody had approved it.

This post checks one thing: environment protection is bound to the environment name. If a workflow starts using a name that does not yet exist in the repository, GitHub auto-creates an environment with that name, with no required reviewers and no branch policy. The job does not enter waiting, and the promote script runs. This is not the same as the docs-only label that skipped a required test so an export script still merged, and it is not the same as a timeout fix whose pull request also deleted the nightly reconciliation job. Today is only about this: you thought you had required approval for production deploys, but what you actually protected was the run whose name happened to be production.

I created a public lab repository, shuizhengqi1/iotxing-lab-unprotected-env-20261201. Shop data is fake (zhou@shop.invalid, card last four 1111, webhooks on shop.invalid and pay.shop.invalid). The fixture commits were written by hand, not generated by a live coding model. The lab repo did not require a pull-request approval before merge, and I could not approve my own pull request (GitHub returned Review Can not approve your own pull request). The question today is not whether a PR review was clicked; it is whether the environment approval stopped promote.py. --admin was not used. There was no Windows run.

The seed commit, c75ee696e2dc (c75ee696e2dcba7fa86b4edbe8f325675fc28460), had only order fixtures and the promote script. live_payments in state/live.json was false, and the webhook still pointed at staging. I first created the GitHub Environment production (id 29142054572) at 2026-12-01 18:43:27 (Asia/Shanghai): required reviewer shuizhengqi1, prevent_self_review false, and a branch policy that allowed only main (policy id 69022152). There were two protection rules, of types required_reviewers and branch_policy. Only then did I push the deploy workflow to main, with the job set to environment: production.

Experiment A's commit was 58cf16958338 (58cf16958338289f5175ea6b47fe35fd39f04023) at 18:43:38, described as promoting to production only after environment approval. Run 46629451922 appeared at 18:43:40 in waiting, and the job was waiting as well. Deployment 8193188139 was recorded on production. live.json on live-state still had live_payments false and an empty promoted_at. I did not approve that run. After the later renamed deploys had finished, it was still waiting.

The workflow used production-api; the approval still sat on production

The mechanism is direct. In GitHub Actions, the string after environment: is the lookup key for environment protection. Only a matching name loads required reviewers, wait timers, and branch policies. If the name does not match, the API creates an environment with that name when the job starts. The new environment has an empty protection_rules array and a deployment_branch_policy of null. For the job, that means no approval and no branch restriction, so the status goes from queued to in_progress. The script cannot see whether an approval happened: promote.py trusts the environment name the workflow gave it. Protection attached to production does not automatically cover a later production-api, prod, or production-web. There is one more restriction that was already on the environment object, though I did not run a separate experiment for it: production had a branch policy that allowed deploys only from main. On the auto-created production-api, deployment_branch_policy was null. After the rename, the required reviewer was gone, and the "main only" rule did not follow either. I did not trigger a production-api deploy from a feature branch, so I cannot report that a feature branch would have promoted successfully; what is certain is that the branch policy was not present on the new environment.

On a three-person, seven-machine team where agents edit workflows, this shows up more often than a handwritten YAML typo. Coding agents are happy to "clarify" a name, and they often put that refactor in the same pull request as a feature. If the review machines look only at the refund API and the test output, a single line environment: production-api is easy to read as cleanup. The acceptance command is pytest -q, which does not parse the workflow. By the time someone opens the Environments list in Settings, the job has often already started. In the lab, production-api was created at 18:44:52, in the same minute the run started, not when the pull request was opened.

Experiment B used that pattern. Branch commit 5646a432ab29 (5646a432ab294ddf28a9b2f34be54b6babeaa75d) added the refund implementation and tests, and changed the workflow to environment: production-api. Local pytest -q was 3 passed. On the same tree I ran scripts/environment_allowlist_gate.py. It read the environment name production-api, which is not in the allowlist production, staging, and it recorded that the environment did not exist yet, so GitHub would auto-create it with no protection. The exit code was 1. If this gate had been a required check, the pull request should not have merged.

The lab left the gate off so the later deploy could actually run. On pull request #1, the pytest check was SUCCESS. An attempt to approve the pull request as shuizhengqi1 was rejected. At 18:44:48 I squash-merged with gh pr merge --squash, without --admin. The merge commit was 2f292a613bcc (2f292a613bcc63da2ffa5c04b0c3a6dbf5e6a092). At 18:44:51, promote run 46629644055 appeared in in_progress, not waiting. At 18:44:52 GitHub auto-created production-api (id 29142198787), with empty protection_rules and no branch policy. Deployment 8193214518 was recorded on production-api. The job started on a GitHub-hosted Ubuntu runner.

That cutover did not succeed on the first try. A broken newline in the promote.py fixture caused a SyntaxError: unterminated string literal at 18:44:58, and the step that would have written live.json back to live-state was skipped. live_payments was still false. This has to be said plainly: the environment had already been auto-created, and approval had already been skipped. Production state was unchanged because the script crashed, not because environment protection worked. That failure is not a successful cutover.

I fixed the newline and pushed 5ac5d08eabf3 (5ac5d08eabf30ed91a793a1c7fbe49d923aef0b9). The workflow still pointed at production-api. Run 46629950348 was created at 18:46:39, started at 18:46:42, and succeeded at 18:46:47, never entering waiting. At 18:46:44 promote.py wrote live_payments: true, payouts_enabled: true, webhook https://pay.shop.invalid/hooks/v2, promoted_environment production-api, and promoted_sha equal to the fix commit. Commit dbe8e0088263 (dbe8e0088263e9cefaaef1977a43f5e90197956f) appeared on live-state, with the message that promotion was via production-api. Meanwhile, experiment A's run 46629451922 was still waiting. The repository now had two environments: production still had two protection rules, and production-api had none.

The production deploy stayed waiting for approval; production-api already turned live payments on

A reasonable objection is that CODEOWNERS on .github/workflows/** would have put machine 4 on this change. That lowers the chance of a missed edit, but it is not a gate. In the lab the rename was a single token, described in the same pull request as the refund feature, with a title and tests about refunds. A review that only confirms "paid orders can be refunded, already refunded orders are rejected" never has to open the workflow file. Even if it does, changing production to production-api often looks like naming cleanup, not the removal of an approval. CODEOWNERS answers who should see the file, not whether that environment name currently has protection.

Another objection is that someone can check the Environments list in Settings after the merge. The lab timeline does not support that as a control: the new environment appears when the job starts. It was created at 18:44:52, and the job was running at 18:44:55. By the time production-api shows up in the list, the script has already started. The first run did not change live.json because of the syntax error, but creating the environment and skipping approval were already done. After the fix, the cutover succeeded at 18:46:47. Discovering the new environment after the deploy puts the gate after the failure.

A third common fix is to attach required reviewers to every environment, or to agree that the name may only be production. A newly auto-created name still starts with empty protection rules. A naming convention depends on every person and every workflow honoring it; an agent will not stop just because a document says "do not rename this." The more stable approach is to have a merge-time check read the workflow, rather than discover the new name at runtime. A cloud vendor's own release approval is still worth adding, but it does not explain how the GitHub Environment layer was skipped in this lab, and many three-person teams treat that layer as their only human gate.

environment_allowlist_gate.py does two copyable things. First, every environment name in a workflow must be in the allowlist, which in the lab is production and staging. production-api fails immediately. Second, a production-like name must already exist and have at least one required reviewer; if it does not exist, the check refuses and says not to rely on auto-create. On the renamed branch the gate exited 1. On main after the merge it still failed, and by then it could also see that production-api existed with required reviewers NONE. After the workflow was changed back to environment: production, the same gate passed and read reviewer shuizhengqi1. It should be a required pull-request check, at least when .github/workflows/ changes.

Block a new environment name in the workflow before merge; do not wait until Settings already shows a new environment

The takeaways for tomorrow are specific. First, do not assume that a protected environment named production means every production deploy will wait for approval; protection only matches the name written in the workflow. Second, make environment_allowlist_gate.py a required check: names outside the allowlist fail, and a production-like name must already exist with required reviewers. Third, when you genuinely need a new environment, change the allowlist and attach protection in Settings before changing the workflow; do not let the job create it. Fourth, when a pull request touches a deploy workflow, compare the environment name against the existing protection list; do not stop at green feature tests.

The limits should also be stated. If the team does not use GitHub Environments, the auto-create and waiting behavior in this post will not appear, and the approval has to be found on another layer. If every deploy goes through a locked org-level reusable workflow that hardcodes environment: production, and application repositories cannot change that name, this particular rename path is much narrower. If production cutover is a separate click on a payment provider's console, and the GitHub script is only a warmup, then skipping the GitHub approval is not the same as money moving; this lab treated promote.py itself as the cutover and should not be stretched to a split process. The lab did not run tests on Windows, did not use a live coding model to write the commits, did not test an organization ruleset, and did not test whether adding reviewers to a new environment after the fact can stop a job that has already started. To let the post-merge deploy actually run, the lab did not make the gate a required check; the failure in the first renamed run was a broken newline in the script, not environment protection taking effect.

What was actually run

  • Public lab: shuizhengqi1/iotxing-lab-unprotected-env-20261201. Environment production id 29142054572, required reviewer shuizhengqi1, branch policy main only. Local: git 2.47.3, Python 3.13.5, official gh 2.103.0. Python on Actions was 3.13.18.
  • Experiment A: commit 58cf16958338 (18:43:38, Asia/Shanghai). Run 46629451922 entered waiting at 18:43:40. Deployment 8193188139 was on production. live_payments was false. That run was still waiting at the end of the lab.
  • The gate exited 1 on the rename branch and noted that production-api did not exist yet and would be auto-created without protection. Local pytest: 3 passed. Pull request #1 checks were SUCCESS. Approving my own pull request was impossible. Squash merge at 18:44:48, merge commit 2f292a613bcc.
  • Experiment B: run 46629644055 went to in_progress at 18:44:51. At 18:44:52 GitHub auto-created production-api (id 29142198787, empty protection_rules). At 18:44:58 promote.py failed on a broken newline, and live.json was not changed.
  • Experiment C: fix commit 5ac5d08eabf3. Run 46629950348 succeeded at 18:46:47 and never waited. Commit dbe8e0088263 on live-state set live_payments to true at 18:46:44, with environment name production-api. Experiment A was still waiting.
  • The gate still failed on main after the merge (production-api existed, required reviewers NONE) and passed after the workflow was restored to production. Not run: Windows or windows-latest, a live coding model, an organization ruleset, or adding reviewers to a new environment after a job had already started. Reproducible scripts and fixtures are in unprotected-env-kit.tar.gz. The next step is to make the environment-name allowlist a required check, and to reject a new name that is referenced before it is protected.