Add TF_TARGETS variable to scope Terraform CI runs to specific targets
What
Adds an optional TF_TARGETS pipeline variable to .gitlab-ci-trg-terraform.yml, so a web-triggered run can be limited to a subset of resources instead of always covering the whole configuration.
How
- New
TF_TARGETSvariable, prefilled in the web pipeline form. Accepts resource addresses separated by space, comma or newline, e.g.module.aks module.network. Empty means the whole configuration, so existing usage is unchanged. -
before_scriptnow also generatestargets.shnext toinit.sh. It expandsTF_TARGETSinto-target=flags, exports them asTF_TARGET_ARGSand logs what the run is limited to. The script is sourced (. ./targets.sh), not executed. - Flags are applied in
plan,destroy-plananddestroy.destroydoes not read a plan cache, so without the flags it would tear down the entire state. -
applyis intentionally left untouched: the targets are already recorded inplan.cache, and passing-targetalongside a saved plan is rejected by Terraform.
Notes
${TF_TARGET_ARGS} is expanded unquoted on purpose, otherwise several targets would be passed as a single argument. As a side effect, addresses containing spaces (indexes such as module.x["a b"]) cannot be passed this way; those still need TF_CLI_ARGS_plan.
Verification
YAML parses cleanly. Not executed against a real state yet, so the first targeted run should be a plan to confirm the flags land as expected.