Custom Plan and Apply Steps
Custom steps add your own commands to a Stategraph Orchestration workflow, around the engine steps init, plan, and apply. A custom step runs a script, sets an environment variable, or authenticates to a cloud provider. Use custom steps to:
- Run checks before an apply.
- Run security and compliance scans.
- Notify monitoring or messaging systems.
Configuring custom steps
Add the steps to the plan and apply lists of a workflow in .stategraph/config.yml:
workflows:
- tag_query: "dir:prod"
plan:
- type: init
- type: run
cmd: ['${TERRATEAM_ROOT}/scripts/pre-plan.sh']
- type: plan
- type: run
cmd: ['${TERRATEAM_ROOT}/scripts/post-plan.sh']
apply:
- type: init
- type: run
cmd: ['${TERRATEAM_ROOT}/scripts/pre-apply.sh']
- type: apply
- type: run
cmd: ['${TERRATEAM_ROOT}/scripts/post-apply.sh']
What is TERRATEAM_ROOT?
TERRATEAM_ROOT is the absolute path of the repository checkout on the runner. See Runner environment variables for the full list.
- Steps run in the order that you list them. Put an
envoroidcstep before the steps that need it. - A workflow that sets
plansteps must include aplanstep. A workflow that setsapplysteps must include anapplystep. Arunstep that callsterraform plandoes not count. - A failed custom step stops the workflow by default, and Orchestration posts the error on the pull request. To continue after a failure, set
ignore_errors: trueon the step.
Step types
Workflows accept the step types init, plan, apply, run, env, oidc, checkov, conftest, opa, and gates, in both the plan and the apply list. The workflows reference lists every key of every type.
Run
The run step runs a command or script in the directory of the operation. cmd is the command, as a list of strings.
- type: run
cmd: ['${TERRATEAM_ROOT}/scripts/my-script.sh']
Other options include run_on (run only on success, failure, or always), ignore_errors, capture_output, visible_on, and format. See the run step options.
Env
The env step sets an environment variable for the steps that follow it. name is the variable, and the output of cmd is the value.
- type: env
name: MY_VAR
cmd: ['echo', 'my-value']
sensitive: true keeps the value out of the step's log and masks it in the output that Orchestration posts. To export several variables from one script, use method: source. See the env step options.
OIDC
The oidc step authenticates to AWS, GCP, or Azure with OpenID Connect.
AWS
- type: oidc
provider: aws
role_arn: arn:aws:iam::123456789012:role/terraform-role
region: us-west-2
GCP
- type: oidc
provider: gcp
project_id: your-project-id
workload_identity_provider: projects/123456/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: my-service-account@my-project.iam.gserviceaccount.com
See the OIDC step options and the cloud provider guides.
Example workflow
With the configuration in Configuring custom steps:
- Open a pull request that changes Terraform files in the
proddirectory. - Orchestration runs the plan steps:
init,pre-plan.sh,plan, andpost-plan.sh. - Review the plan with your team.
- After approval, comment
stategraph apply, or merge ifwhen_modified.autoapplyistrue. Orchestration runs the apply steps:init,pre-apply.sh,apply, andpost-apply.sh. - Orchestration posts the apply results on the pull request.
Best practices
- Scripts can read sensitive data. Keep secrets out of logs and captured output.
- Limit custom steps to the directories that need them with dirs and tag queries.
- Use hooks for commands that run once per operation, not once per directory.
- Pass configuration through environment variables, not hard-coded values.
- Handle errors and write clear logs in each script.
- Document the tools that each script needs.
- Keep each script to one job.
- Keep the scripts in the repository of the infrastructure code.