Hook and step model¶
Hooks let you run shell commands at fixed points around init, plan, and apply. The gates are implemented as hooks, and you can add your own (format, validate, a source scan, a notification). Hook variables use the engine's word project; in Terragrunt mode a project is exactly a unit.
The five seams¶
| Seam | Runs |
|---|---|
pre_init |
before terraform init |
post_init |
after init, before plan |
post_plan |
after plan, with the plan available |
pre_apply |
after plan, before apply |
post_apply |
after a successful apply |
Two stages¶
The plan run and the apply run each have their own hook
list, nested under plan: and apply: in the steps: block:
steps:
plan: # also the drift sweep
post_plan:
- name: Policy check (conftest)
run: bash "$TFPR_ENGINE_DIR/scripts/conftest-gate.sh"
- name: Security scan (avmsec)
run: bash "$TFPR_ENGINE_DIR/scripts/avm-policy-gate.sh" avmsec
on_failure: warn
- name: Reliability check (APRL)
run: bash "$TFPR_ENGINE_DIR/scripts/avm-policy-gate.sh" aprl
on_failure: warn
- name: Security scan (checkov)
run: bash "$TFPR_ENGINE_DIR/scripts/checkov-gate.sh"
on_failure: warn
- name: Cost estimate (infracost)
run: bash "$TFPR_ENGINE_DIR/scripts/infracost-gate.sh"
apply: # default: no hooks
pre_apply: []
post_apply: []
Firing order:
- plan and drift:
pre_init, init,post_init, plan,post_plan - apply:
pre_init, init,post_init, plan,post_plan,pre_apply, apply,post_apply
Apply re-plans before it applies, and the reviewed-plan guard refuses an apply
whose plan differs from the one reviewed, so the apply stage runs no hooks
unless its own list names them: the plan run already gated the same plan. The
apply report then carries a one-line note, "Plan checks not re-run: this is
the same plan that was reviewed at <commit>". tfpr apply --force
skips the guard and still does not fall back to the plan-stage hooks; force
means the operator has read the fresh plan. A repository that wants a gate at
apply time lists it under apply:.
The flat form, hook slots at the top level with no plan: or apply: key,
keeps working and means the plan stage only. A flat pre_apply or
post_apply is refused by discovery with the fix named, because an apply-time
hook that never runs would read as a gate that passed; a misspelt slot is
refused for the same reason.
Init, plan, the reviewed-plan guard, the unit lock and the ordered apply are
not steps and cannot be replaced from projects.yml. Hooks are the
customization surface; the guards are the product.
A hook is a named mapping¶
Every hook is a mapping with three keys:
| Key | Required | Meaning |
|---|---|---|
name |
yes | The hook's row title in the PR comment and its line in the check run. Unique within a stage. |
run |
yes | The shell command. It runs in the unit directory under bash -eo pipefail. |
on_failure |
no | block (the default) or warn. |
The status comes from the exit code, never from the text the hook prints:
| Exit | on_failure |
Status | Effect |
|---|---|---|---|
| 0 | any | pass | row collapsed |
| non-zero | block |
fail | row open; the unit fails, later hooks are not run, no apply instructions, the merge gate blocks |
| non-zero | warn |
warn | row open; the run goes on, merge and apply are not blocked |
0, results file says skipped |
any | skipped | row collapsed |
on_failure: warn is how a gate is made advisory. The hook still exits
non-zero, so the finding shows as an open row, but the unit carries on.
Upgrading from engine v6¶
A hook used to be a plain command string. Discovery now refuses that form and
prints the edit. Give each command a name, move it under run:, and replace
CHECKOV_SOFT_FAIL=1 with on_failure: warn:
# v6
post_plan:
- CHECKOV_SOFT_FAIL=1 bash "$TFPR_ENGINE_DIR/scripts/checkov-gate.sh"
# v7
post_plan:
- name: Security scan (checkov)
run: bash "$TFPR_ENGINE_DIR/scripts/checkov-gate.sh"
on_failure: warn
Use engine v7.0.2 or later for the migration. v7.0.0 also refused the old form
on the base branch's tree, so the pull request that migrates could not plan,
and before v7.0.2 a pull request that changes only projects.yml failed the
merge gate. From v7.0.2 it passes as "Configuration only: no unit selected".
Environment exposed to hooks¶
| Variable | Meaning |
|---|---|
TFPR_PLANFILE |
The binary plan file. Available from post_plan on. |
TFPR_PLANJSON |
The plan rendered as JSON (terraform show -json). Available from post_plan on. |
TFPR_OUT |
Append markdown here to surface it in the PR comment or drift issue. |
TFPR_ENGINE_DIR |
The engine runtime root, installed from the pinned release. Call bundled gates as bash "$TFPR_ENGINE_DIR/scripts/<gate>.sh". Defaults to the workspace in vendored mode. |
TFPR_BIN |
Path to the compiled tfpr binary, for hooks that invoke engine subcommands. |
TFPR_RESULTS |
Directory for the hook's results file, <gate>.json. |
TFPR_HOOK_NAME, TFPR_HOOK_SLOT |
The hook's name and the seam it runs in. |
PROJECT_NAME, PROJECT_DIR, PROJECT_LABEL |
The unit's name, directory, and full-path label. |
That table is close to the whole environment. Terraform,
Terragrunt and hooks run with an allow-list: PATH, HOME, TMPDIR, LANG,
USER, LC_*, GITHUB_WORKSPACE, the proxy and CA variables, ARM_*,
AZURE_*, BACKEND_*, TF_*, TG_*, TERRAGRUNT_*, MISE_*, ASDF_*,
the hook variables above, and the names listed under extra_env in
projects.yml. The GitHub OIDC request pair arrives as
ARM_OIDC_REQUEST_URL and ARM_OIDC_REQUEST_TOKEN. The workflow token, the
checks App token and INFRACOST_API_KEY are not in it, and discovery refuses
an extra_env entry that names a GH_*, ACTIONS_* or TFPR_* variable or
GITHUB_TOKEN. A hook that posted to GitHub with
GH_TOKEN belongs in a workflow step after the engine job; the cost gate
needs INFRACOST_API_KEY named under extra_env:
Hooks and extra_env are read from projects.yml on the default branch,
never from the pull request, so a pull request cannot add, change or drop a
hook for its own run. The pull request that adds a hook does not run it; the
next one does. Review extra_env like code: every name on it is readable by
pull request code in the plan job.
Each hook gets its own TFPR_OUT, and what it writes there is shown unchanged in
the hook's own row of the PR comment, titled with the hook's name. Rows follow
run order: pre_init and post_init hooks sit above the plan output and
post_plan hooks below it; on apply, every hook that ran before the apply sits
above the apply output and post_apply below. A row opens by itself when its
hook failed or warned, and then also shows the command and its exit code under
the hook's output. The title of a failed or warned row shows its status in colour before the verdict: warning in yellow, failed
in red (or the exit code when the hook wrote no verdict). A failed or warned hook that wrote nothing to TFPR_OUT
shows its own console output there instead, so a failure is never an empty
row. A hook that reports through TFPR_OUT, like the bundled gates, keeps its
full console output in the run log.
A hook can also write a results file, $TFPR_RESULTS/<gate>.json, with a
verdict field (the shape is in the engine README). Write it as one plain
line, lowercase-first and with no full stop, as the bundled gates do
(no policy violations, 2 passed, no priced resources). The verdict becomes the row's one-line summary (for example
Security scan (checkov) ยท 5 failed, 11 passed) and the hook's line in the
check run. A results file never changes the status: that always comes from the
exit code.
The gate hooks¶
All four ship in scripts/ and are meant to run as post_plan hooks.
The variables below reach a gate only when they are set in the hook's run
command or listed under extra_env. A variable set on the job is filtered out
like any other name outside the allow-list.
conftest-gate.sh¶
Tests TFPR_PLANJSON against a policy directory and appends a policy summary to
TFPR_OUT. Gating: exits non-zero on any deny.
| Variable | Default | Purpose |
|---|---|---|
CONFTEST_POLICY_DIR |
<repo>/policy |
Directory of .rego policies. |
CONFTEST_NAMESPACE |
main |
Rego namespace to test. |
CONFTEST_FAIL_ON_WARN |
unset | Set to 1 to also fail on warn. |
Passes with a note if no policies exist yet, so you can enable the hook before you write any.
avm-policy-gate.sh¶
Runs one bundle of Azure/policy-library-avm against TFPR_PLANJSON: avmsec
or aprl, given as the first argument. It downloads the pinned release,
checks its SHA256, adds the default exemption file for avmsec (high severity
only) and every .rego file under avm-policy-exceptions/<bundle>/, then hands
the bundle to conftest-gate.sh with every namespace. Gating: exits non-zero on
any deny, so run it with on_failure: warn to report without blocking. A
failed download or checksum is an error row. See
the AVM policy library.
| Variable | Default | Purpose |
|---|---|---|
AVM_POLICY_EXCEPTIONS_DIR |
<repo>/avm-policy-exceptions |
Folder of exemption files, one subfolder per bundle. |
AVM_POLICY_ALL_SEVERITIES |
unset | Set to 1 to run every avmsec rule. |
AVM_POLICY_LIBRARY_DIR |
unset | A local copy of the library (its root, holding policy/), used instead of the download. |
AVM_POLICY_LIBRARY_REF, AVM_POLICY_LIBRARY_SHA256 |
v1.0.0 and its hash |
Another release and its tarball hash, always together. |
checkov-gate.sh¶
Scans TFPR_PLANJSON (plan scanning, which also sidesteps Terragrunt) and appends
the failed checks to TFPR_OUT as a list grouped by resource. Gating: exits with
checkov's status. Set on_failure: warn on the hook to report without blocking.
| Variable | Default | Purpose |
|---|---|---|
CHECKOV_SOFT_FAIL |
unset | Set to 1 to make checkov itself exit zero. Prefer on_failure: warn, which keeps the finding visible as an open warning row. |
CHECKOV_HARD_FAIL_ON |
unset | Check IDs that must gate. |
CHECKOV_REPO_ROOT |
$PWD |
Enables inline checkov:skip comment support. |
CHECKOV_ARGS |
unset | Extra CLI arguments. |
checkov downloads its guideline links from api0.prismacloud.io on every run.
Nothing is sent. To stop the call, for example on a runner without that egress,
set the argument in the hook command:
- name: Security scan (checkov)
run: CHECKOV_ARGS=--skip-download bash "$TFPR_ENGINE_DIR/scripts/checkov-gate.sh"
on_failure: warn
Findings then carry no link, and inline skips that use Prisma BC_* IDs stop
working. CKV_* IDs still work.
infracost-gate.sh¶
Runs infracost breakdown against TFPR_PLANJSON and appends the monthly cost to
TFPR_OUT as a list per priced resource. Report-only: always exits zero.
| Variable | Default | Purpose |
|---|---|---|
INFRACOST_API_KEY |
unset | Hosted pricing API key. Without it the hook notes it and skips. |
INFRACOST_BREAKDOWN_ARGS |
unset | Extra arguments for infracost breakdown. |
Writing your own hook¶
A hook is any shell command. For example, run terraform fmt -check before init and
surface the result in the comment: