Skip to content

feat(ops): an environment option on a scheduled Op, so a deployment-protection reviewer can gate the generated apply (#2257) - #2264

Merged
lex00 merged 3 commits into
mainfrom
feat/2257-op-environment
Sep 8, 2026
Merged

lex00 merged 3 commits into
mainfrom
feat/2257-op-environment

Conversation

@lex00

@lex00 lex00 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Closes #2257.

generateOpsPipeline emitted no environment: on any job, so a GitHub environment with required reviewers bound nothing a chant-generated pipeline ran. INTENTIUS/choudoufu#807 (c) had to say in its README that its production environment gates no job today. This is the key, per forge.

Where the option lives, and why

On ScheduledOpSpec, beside #2242's setup and permissions — not on OpConfig beside #2120's schedule.

#2120 moved the cadence onto the Op because a cadence is runtime-neutral data with three independent readers: the CI generators render it as a cron, chant operator reads it as a tick, a fountain Steward turns it into a Schedule. An environment has one reader, the CI generator, and it is not runtime-neutral: it names an object that exists on GitHub and GitLab and does not exist on Forgejo at all.

It is also a property of the job rather than of the Op, which is exactly the reason #2242 gave for setup being per Op: in plan-on-pr the pull-request plan touches no environment and only the push apply belongs behind the reviewer, over the same root and the same code. An Op generated into two environments would need two values, so a value pinned to the Op would have to be overridden per generate call anyway.

environment?: { name: string; url?: string }

Per forge

github emits environment: on that Op's job, always as a mapping rather than the environment: name string shorthand, so adding a url later is a new key rather than a reshaped value. It is emitted on the Op's own job and never on the <op>-gate-notice job beside it: that job exists to say a chant gate is pending, and behind the same reviewer it would only become readable after somebody had already acted on what it reports.

permissionsFor gains nothing. Environment protection is repository configuration — the reviewers, the wait timer and the branch restriction live on the environment object in Settings, not in the workflow and not on GITHUB_TOKEN — so the option costs no scope at all. A test asserts the permission set is identical with and without it, and that it does not reach for deployments.

assertEnvironment refuses only what could never bind: a blank name, an empty url, and a url that is neither absolute nor a ${{ }} expression (GitHub renders it as the deployment's own link, so a relative path is a dead link rather than an error anywhere). Nothing checks that the environment exists or is protected, and that is deliberate: GitHub creates an unprotected environment on first use rather than failing, so whether reviewers exist is not answerable from workflow content.

forgejo drops the key. Forgejo Actions has no environments — no protection rules, no required reviewers, no wait timers — so the key would read as a deployment gate and hold nothing back. Unlike permissions:, which the runner reads and ignores, this names an object that does not exist on the instance, and the whole point of the option is a human holding an apply, so a silent drop is the worst outcome available. The generated file therefore gains a header comment naming the environment that was asked for, stating that nothing on the forge enforces it, and pointing at chant's own gate, which needs nothing from the forge:

# chant dropped `environment: production` from this workflow: Forgejo Actions has no
# environments, so there is no protection rule, required reviewer or wait timer for the key to
# name. This job is not held back by anything on the forge. What still stops the apply is chant's
# own gate: the run records a pending fact on the chant/lifecycle branch and ends, and `chant
# approve <op> <gate>` is what lets the next run through.

The drop is scoped to the Op generator rather than added to the dialect's DROPPED_KEYS. Adding it there would also have dropped the environment: the DeployEnvironment composite puts on component-pipeline jobs, changing output for an existing surface that is not this issue. That gap is real (a DeployEnvironment targeted at forgejo still emits a key the runner ignores) and worth its own issue; it is not touched here.

gitlab maps it onto its own environment:, which exists there with the same two fields and a protected-environment approval rule behind it. This is the one of the three per-Op options GitLab can honour rather than refuse. What it cannot declare from the file is the protection itself — an environment's approval rules are project settings, exactly as GitHub's required reviewers are repository settings — so the generated header names the environment to protect, the same way it already names the Pipeline Schedule to create:

#   actions-audit: cron "0 6 * * *", CHANT_SCHEDULED_OP="actions-audit", finding-mode issue — needs a GITLAB_TOKEN CI/CD variable (masked, scope: api)
#     deploys to environment "production" — protect it under Settings > CI/CD > Protected environments to require an approval before the job runs

How the two gates compose

Documented on the ops guide as its own section, and on the terraform ops page and the plan-on-pr README in worked form.

The environment reviewer stops the job before it starts. Nothing is checked out, the apply role is never assumed, and the run sits in the forge's queue until somebody with access to that environment releases it.

chant's own gate (#2119) stops the apply inside a run that already started. The Op plans, reaches its Gate phase, finds no resolution on the gate ledger, records a pending fact on the chant/lifecycle branch and ends. chant approve <op> <gate> --approver you records the answer as another fact on that branch, and the next run reads it and applies. Because it is a fact rather than a held connection, the decision survives the run that asked for it, and the approver is reading a plan that already exists.

The reviewer answers whether the job may run at all, cheaply and before any cost is incurred. chant's gate answers whether this exact plan should be applied, which nothing can answer before the job has run far enough to produce it. Either alone is coherent — the reviewer alone pairs with gate: "never" on the root; chant's gate alone is what a Forgejo estate gets — and both, or neither, is the user's choice per environment.

plan-on-pr

The apply spec now sets environment: { name: "production" }; the plan spec sets none, because it deploys nothing and a reviewer prompt on every pull request is the fastest way to teach people to click through one. examples.test.ts asserts the key on the apply job, its absence on the plan workflow and on the gate-notice job, and that the apply's permission set is unchanged.

Additive, proven against bytes

Each of the three generators pins the exact document a spec with no environment emits, as a literal produced by the generator at the commit this branch started from, not by this one. Those three tests passed before the change and pass after it, so they are a byte comparison against the old output rather than a tautology. The github suite additionally asserts that adding the option changes exactly the environment block:

expect(after.replace("    environment:\n      name: production\n", "")).toBe(before);

Red first

The 15 new discriminating assertions were run against the parent commit's generators with the new test files in place. All 15 failed; the three byte-identical goldens passed, which is what makes them meaningful.

 FAIL  lexicons/github/.../generate-op-pipeline.test.ts > a deployment environment on the Op's job (#2257) > emits environment: on the Op's job, as a mapping rather than the string shorthand
 FAIL  lexicons/github/.../generate-op-pipeline.test.ts > ... > carries the url when the spec sets one
 FAIL  lexicons/github/.../generate-op-pipeline.test.ts > ... > adding the environment changes exactly the environment block and nothing else
 FAIL  lexicons/github/.../generate-op-pipeline.test.ts > ... > applies to any trigger, so a pull_request plan can name a review environment too
 FAIL  lexicons/github/.../generate-op-pipeline.test.ts > ... > refuses a blank environment name, which resolves to nothing
 FAIL  lexicons/github/.../generate-op-pipeline.test.ts > ... > refuses a url that is neither absolute nor an expression, which renders as a dead link
 FAIL  lexicons/github/.../generate-op-pipeline.test.ts > ... > refuses an empty url rather than emitting one
 FAIL  lexicons/forgejo/.../generate-op-pipeline.test.ts > a dropped deployment environment (#2257) > drops the key, and github with the same spec keeps it
 FAIL  lexicons/forgejo/.../generate-op-pipeline.test.ts > ... > says in the generated header which environment did not survive, and what still gates
 FAIL  lexicons/forgejo/.../generate-op-pipeline.test.ts > ... > the header only appears for the Op that asked for an environment
 FAIL  lexicons/forgejo/.../generate-op-pipeline.test.ts > ... > refuses the same malformed environment github refuses, through the shared builder
 FAIL  lexicons/gitlab/.../generate-op-pipeline.test.ts > a deployment environment (#2257) > maps it onto GitLab's own environment: key, with the url when there is one
 FAIL  lexicons/gitlab/.../generate-op-pipeline.test.ts > ... > names the environment to protect in the header, beside the schedule to create
 FAIL  lexicons/gitlab/.../generate-op-pipeline.test.ts > ... > refuses a blank name and a url that is neither absolute nor a variable expression
 FAIL  lexicons/terraform/examples/examples.test.ts > plan-on-pr ... > puts the apply job behind a GitHub environment, and the plan job behind none

 Test Files  4 failed (4)
      Tests  15 failed | 108 passed (123)

Gates

Run in this worktree, on the rebased branch:

$ node scripts/check-docs-diataxis.mjs
docs: 20 tutorial, 52 how-to, 58 reference, 34 explanation
diataxis=0

$ node scripts/lint-docs-sentences.mjs
Docs prose lint: 4087 gated finding(s), no regressions — 3 BELOW the baseline of 4090
prose=0

$ npx tsc --noEmit -p packages/core/tsconfig.json
coretsc=0

$ npx tsx scripts/typecheck.ts
Repo-wide typecheck: 32 known failure(s), no regressions. (32 baselined)
typecheck=0

$ npm run build -w @intentius/chant-lexicon-<name>
build:github=0
build:forgejo=0
build:gitlab=0
build:terraform=0

$ just lint
✖ 2 problems (0 errors, 2 warnings)     # both pre-existing, in packages/core/src/meta/publish-metadata.test.ts
lint=0

$ npx vitest run lexicons/{github,forgejo,gitlab}/src/components/ lexicons/terraform/examples/examples.test.ts packages/core/src/op/
 Test Files  48 passed (48)
      Tests  774 passed (774)
vitest=0

Not run here, by instruction: anything needing a container, an emulator, a live forge or a long acceptance suite. Specifically, just ci in full, just check-lexicons, just docs-build/docs-check-links (needs lychee), and the terraform lexicon's acceptance blocks (src/op/activities/choudoufu.acceptance.test.ts, composites/terraform-adopt-op.acceptance.test.ts), which gate on a terraform binary and the pinned floci emulator. Nothing in this change touches an activity, an executor or the emulator surface, so those blocks exercise no new code; running just ci on the merge result is the verification someone should do before merging.

Downstream

INTENTIUS/choudoufu#807's TestPipelineGovernanceEnvironmentGapIsStillReal is designed to go red the day a regenerated live-apply.yml carries this key. Once choudoufu repins chant and regenerates, that test should fail, which is the signal to rewrite the README section saying the reviewer gates no job today. That is tracked there as item (f) of #807.

Notes

  • Regenerating the terraform docs twin (npm run docs -w @intentius/chant-lexicon-terraform) also corrected a stale lexicon version line in lexicons/terraform/docs/src/content/docs/index.mdx (0.57.0 → 0.59.0) that predates this branch.
  • The forgejo component path still emits a job-level environment: from the DeployEnvironment composite that the Forgejo runner ignores. Out of scope here and left untouched on purpose; worth its own issue.

…generated job's deployment gate (#2257)

`generateOpsPipeline` emitted no `environment:` on any job, so a GitHub
environment with required reviewers bound nothing a chant-generated pipeline
ran. The option lands on `ScheduledOpSpec` beside #2242's `setup` and
`permissions`, not on `OpConfig` beside #2120's `schedule`: a cadence is
runtime-neutral data three readers render, while which forge environment a job
deploys to has exactly one reader, is a property of that job rather than of the
Op (the pull-request plan touches no environment; only the push apply belongs
behind the reviewer), and would have to be overridden per generate call anyway
for an Op generated into more than one environment.

github emits `environment: { name, url? }` on the Op's own job and never on the
gate-notice job beside it, which exists to say a chant gate is pending and would
be useless held behind the same reviewer. `permissionsFor` gains nothing:
environment protection is repository configuration, not a token scope.
`assertEnvironment` refuses only what could never bind — a blank name, and a
`url` that is neither absolute nor an expression the forge resolves — because
GitHub creates an unprotected environment on first use rather than failing, so
whether reviewers exist is not answerable from workflow content.

forgejo drops the key: Forgejo Actions has no environments, no protection rules
and no required reviewers. Dropping it silently would be the worst available
outcome when the whole point of the option is a human holding an apply, so the
generated file gains a header comment naming the environment that was asked for,
saying nothing on the forge enforces it, and pointing at chant's own gate. The
drop is scoped to the Op generator rather than added to the dialect's
`DROPPED_KEYS`, so every other document the dialect transforms is untouched.

gitlab maps it to its own `environment:`, which exists there with the same two
fields and a protected-environment approval rule behind it. As with the
schedule, the approval rule is a project setting this file cannot declare, so
the generated header names the environment to protect.

Additive throughout: a spec without `environment` emits the same bytes it did
before.
…aim to bytes (#2257)

Fifteen new assertions across the three generators and the plan-on-pr example,
all of them red against the parent commit: the key on github's job and its
absence on the notice job beside it, the url form, forgejo's drop plus the
header that says which environment did not survive, gitlab's own environment:
mapping and its protected-environments header line, and the three refusals.

Each generator additionally pins the exact document a spec with no environment
emits, as a literal produced by the generator at the parent commit rather than
by this one. That is what makes "additive" falsifiable: those three passed
before the change and pass after it, so they are a byte comparison against the
old output and not a tautology.
The ops guide gains a "Two gates, and how they compose" section: the reviewer
stops the job before it starts, so nothing is checked out and no role is
assumed; chant's gate stops the apply inside a run that already started, on a
plan that already exists, and is cleared by a commit rather than a button.
Either alone is coherent, both is the usual production arrangement, and the
choice is per environment. The per-Op options paragraph now covers all three
and says what each provider does with the new one.

The terraform ops page and the plan-on-pr README carry the worked version: the
apply spec names `production`, the emitted YAML shows the key, and both say
that the environment costs no token scope and that the pending-gate notice job
stays outside it. The lexicon-authoring page tells a generator author that this
is the option where a silent drop is worst.

Regenerating the terraform docs twin also picked up a stale lexicon version
line (0.57.0 to 0.59.0) that predates this branch.
@lex00
lex00 merged commit 8244ba0 into main Sep 8, 2026
6 checks passed
@lex00
lex00 deleted the feat/2257-op-environment branch September 8, 2026 05:29
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.

ops(github): an environment option on a scheduled Op, so a deployment-protection reviewer can gate the generated apply job

1 participant