Run hooks around a stage
Optional: hand this page to your coding agentThe steps work by hand too.Show the whole prompt
Read https://intentius.io/terragucci/guides/run-steps/.
Propose the ci.hooks block for chant.workspace.json that runs my repo's checks before each plan, with onFailure: "approve" on any check that should hold a wave for a person rather than fail it.
Add it to chant.workspace.json, raise minReader to 0.124.41 if it is lower, and open a pull request. Tell me that the hooks take effect once it merges, and why.
Never apply, approve (a pull request review or `terragucci approve`), override a policy denial (`terragucci override`), use `--mode apply`, or merge; never touch `.chant/allowed_signers` or `chant/lifecycle`.Result
Section titled “Result”On every forge, your commands run inside the job that already runs each root’s init, plan and apply, by the rules of chant’s stage hooks. The plan note and the report list every hook that ran:
**Hooks (2):**
| Root | When | Hook | Result |
|----------|-------------|--------|--------------------------------|
| envs/app | before-plan | tfvars | passed |
| envs/app | after-plan | verify | asks for an approval, exit 1 |Prerequisites
Section titled “Prerequisites”| You need | Why |
|---|---|
| The pipeline | the stages run the hooks |
chant.workspace.json with minReader 0.124.41 or newer |
chant reads ci.hooks from it |
| Whatever your commands call, in the job’s image | the hooks run in the job’s container; your own image can carry it |
-
List the hooks under
ci.hooksinchant.workspace.json:{"ci": {"hooks": {"before": {"plan": [{ "name": "tfvars", "command": ["sh", "-c", "./scripts/write-tfvars.sh > hook.auto.tfvars"], "perRun": true }]},"after": {"plan": [{ "name": "verify", "command": ["sh", "-c", "cosign verify-blob --key cosign.pub --signature module.sig module.tgz"], "perRun": true, "targets": ["prod/*"], "onFailure": "approve" }]}}}}Key Meaning commandthe argv to run in the root’s directory; ["sh", "-c", "..."]runs a shell lineperRuntrue: terragucci runs each hook once for each root it matches, and refuses a hook without itnamewhat the log, the note and the report call it; the command when unset targetsthe roots it runs for, by name or pattern ( *within one/segment,**across them); every root when unsetonFailure"fail", the default, or"approve"{target}in the command names the root, and{plan}the saved plan once there is one. -
Merge it. Later changes pick the hooks up once this one merges (why).
Hook timing
Section titled “Hook timing”| Moment | Runs |
|---|---|
before.observe |
before the root’s init |
before.plan |
after init, before the plan |
after.plan |
after the plan, with the saved plan |
before.apply |
once the gate lets the wave through, before the root’s apply |
after.apply |
after the root applied |
| Job | Runs the hooks of |
|---|---|
plan, and a /terragucci plan re-plan |
before.observe, before.plan and after.plan, for each root the change reaches |
apply-wave-<k>, and apply-comment with apply.when: pull-request |
every moment, for each root of the wave |
| drift | before.observe, before.plan and after.plan around the refresh-only plan |
| check | none; check.pre_commit runs the repo’s pre-commit hooks there, see Run your pre-commit hooks in CI |
Roots that plan side by side run their hooks side by side.
Each hook gets the job’s environment minus its forge tokens (as the binary does), chant’s CHANT_HOOK (the moment), CHANT_TARGET and CHANT_RUNS, plus these:
| Variable | Value |
|---|---|
TG_STAGE |
tf-plan, tf-apply or tf-drift |
TG_ROOT |
the root’s path from the repo |
TG_REPO |
the checkout’s path |
TG_PLAN_FILE, CHANT_PLAN |
after a plan and before an apply: the saved plan, which tofu show -json "$TG_PLAN_FILE" reads |
A hook’s output goes to the job log with each line prefixed by the root, the moment and the hook’s name. The report keeps its name, moment, result, exit code and time, never its output.
In a Terragrunt repo
Section titled “In a Terragrunt repo”A wave of units plans with one run --all and applies with another, so each moment comes once per wave. A hook then runs in turn in each unit directory its targets match, and TG_ROOT names the unit:
| Moment | Runs |
|---|---|
before.observe, before.plan |
before the wave’s run --all plan; on a pull request’s plan a unit whose hook fails is refused and the rest plan |
after.plan |
after it, with TG_PLAN_FILE the unit’s saved plan; onFailure: "approve" holds the wave at its gate |
before.apply, after.apply |
around the run --all apply, for the units that change |
A provisional preview of dependents runs no hooks.
Under Terragrunt 1.2 a unit’s generated providers.tf and versions.tf are read-only links shared with other units. A hook that changes one should write a new file in its place rather than append to it (>>) or truncate it, or the change reaches every unit that shares the file. Setting mutable = true on the generate block, or TG_NO_CAS=true in the job’s env, gives each unit its own copy (Generated provider files).
Hook failures
Section titled “Hook failures”onFailure |
A non-zero exit |
|---|---|
"fail", the default |
fails the root, and its later hooks do not run. On a plan the root is refused; on an apply wave nothing in the wave applies. A failure after the apply fails the job, though the root has applied |
"approve" |
holds the root’s wave at its gate. The hooks after it still run. The wave waits for an approval of its set digest, whatever gate says, and the plan note shows the wave as one that waits |
chant refuses onFailure: "approve" after the apply, and refuses after.gate.
The wave waits through the same ledger as every other gated wave: terragucci approve of its digest on chant/lifecycle, under the approval mode at base. A wave that changes nothing applies nothing, so it never waits. With waves.jobs, a share job whose approve hook fails applies nothing unless the wave’s job decided under an approval.
Hooks are read at base
Section titled “Hooks are read at base”Each run reads ci.hooks from chant.workspace.json at its base, which the change cannot alter:
| Run | Reads the hooks at |
|---|---|
| a pull request’s plan | the target branch |
| an apply after a merge | the applied commit’s first parent, as the approval mode is read |
apply.when: pull-request |
the base the pull request applies onto |
| drift | the default branch, which it checks out |
A change therefore cannot add, edit or remove a hook that runs with its apply credentials, nor drop an approve hook to skip the gate. When the base has no declaration, no hook runs. An unreadable declaration at base fails the stage when the checkout names hooks; when it names none, no hooks run. The threat model has the rest.
Coming from steps:
Section titled “Coming from steps:”terragucci.yml takes no steps: key, and config check says where it went. Each step becomes one hook:
| A step’s | The hook’s |
|---|---|
run: <line> |
command: ["sh", "-c", "<line>"] and perRun: true |
before: init, before: drift |
before.observe |
after: init, before: plan |
before.plan |
after: plan, after: drift |
after.plan |
before: apply, after: apply |
before.apply, after.apply |
roots |
targets |
on_failure |
onFailure |
TG_STEP |
CHANT_HOOK, such as before-plan |
The drift job runs the plan-side moments, so a hook meant only for drift checks TG_STAGE. terragucci import spacelift and import env0 write a platform’s hooks into ci.hooks.
Run the jobs in your own image
Section titled “Run the jobs in your own image”Build your own image FROM terragucci’s when a hook needs a tool it lacks, and set image. terragucci’s images are Debian, so tools come from apt-get:
FROM ghcr.io/intentius/terragucci-tofu:<the tag init names>
RUN apt-get update && apt-get install -y --no-install-recommends jqimage: registry.example.com/infra/terragucci-tofu:1npx terragucci init then writes it into each job. Since it must keep terragucci and the binary, build it from the tag the pipeline’s header names and rebuild it on each terragucci upgrade. One image serves every job: the plan job plans every root a change reaches, so a root cannot have an image of its own.
terragucciHelp setting it up: Intentius
These docs count page views and clicks with PostHog. They set no cookies, store nothing in your browser, and send nothing when your browser asks not to be tracked.