ctx
The ctx value describes the situation a script runs in: its raw arguments, its source file, the parsed inputs of a
custom command, the component in scope, and the lifecycle hook that triggered it. Every attribute is read-only.
Usage
ctx.args # list of script arguments
ctx.script # source file, or None
ctx.flags # dictionary of parsed flags
ctx.arguments # dictionary of parsed named arguments
ctx.component # component handle, or None
ctx.hook # hook details, or None
ctx.operation # parent operation details, or None
Attributes
ctx.args- The command-line arguments passed to a standalone script.
ctx.script- The path and directory of the script file, when the script was read from a file.
ctx.flags- The parsed flags of a custom command, or the flags a workflow supplies.
ctx.arguments- The parsed named arguments of a custom command.
ctx.component- The component a custom command or hook is scoped to.
ctx.hook- The name and event of the lifecycle hook that runs the script.
ctx.operation- The parent operation, such as
terraform apply, and its outcome.
What each entry point provides
The attributes always exist. What they hold depends on how the script started:
| Entry point | ctx.args | ctx.script | ctx.flags | ctx.arguments | ctx.component | ctx.hook, ctx.operation |
|---|---|---|---|---|---|---|
| Standalone script | Arguments after the script name or stdin marker (-) | The script file, or None for stdin | Empty | Empty | None | None |
| Custom command | Empty | Set when the script comes from a local !include | Parsed flags | Parsed arguments | The command's component when it declares component:, otherwise None | None |
| Workflow step | Empty | Set when the script comes from a local !include | Workflow flags, as strings | Empty | None | None |
| Lifecycle hook | Empty | Set when the script comes from a local !include | Empty | Empty | The hook's component, or None for an aggregate hook | The hook and the parent operation |
Steps inside parallel and matrix groups inherit the context of the step that contains them. A standalone program that
declares its interface with cli.command receives its parsed inputs as the arguments
of its entry function instead of through ctx.flags and ctx.arguments.
Examples
Branch on the entry point
if ctx.hook != None:
print("running as the {} hook".format(ctx.hook.event))
elif len(ctx.args) > 0:
print("standalone script with arguments:", ctx.args)
else:
print("workflow or custom command step")
Read inputs in a custom command
replicas = int(ctx.flags["replicas"])
service = ctx.arguments["service"]
print("{}: {} replicas".format(service, replicas))
Related
envholds the step's declared environment inputs.cli.commanddeclares a standalone program's interface.- Atmos Automation Language and the script step