Actions
Component Actions offer several exec entrypoints that allow a component to perform additional logic at key stages of its lifecycle. These actions are executed within a shell with the same context as the Zarf binary. For a detailed overview of the execution sequence of component actions, please refer to the Zarf package create lifecycle documentation and package deploy lifecycle documentation. Additionally, you can experiment with the component actions example located in the Component Actions example.
The component.actions
field includes the following optional keys, also known as action sets
:
onCreate
- Runs duringzarf package create
.onDeploy
- Runs duringzarf package deploy
.onRemove
- Runs duringzarf package remove
.
These action sets
contain optional action lists
. The onSuccess
and onFailure
action lists are conditional and rely on the success or failure of previous actions within the same component, as well as the component”s lifecycle stages.
before
- sequential list of actions that will run before this component is processed forcreate
,deploy
, orremove
.after
- sequential list of actions that will run after this component is successfully processed forcreate
,deploy
, orremove
.onSuccess
- sequential list of actions that will run after ALLafter
actions have successfully completed.onFailure
- sequential list of actions that will run after ANY error during the above actions or component operations.
In addition to action lists
, action sets
can also specify a defaults
section that will be applied to all actions in the set. The defaults
section contains all of the same elements as an action configuration, with the exception of the action specific keys like cmd
, description
or wait
, which are not allowed in the defaults
section.
An action list
contains an ordered set of action configurations
that specify what a particular action will do. In Zarf there are two action types (cmd
and wait
), the configuration of which is described below.
Between all action configurations, there are a few common keys that are common to all of them which are described below:
description
- a description of the action that will replace the default text displayed to the user when the action is running. For example:description: "File to be created"
would displayWaiting for "File to be created"
instead ofWaiting for "touch test-create-before.txt"
.maxTotalSeconds
- the maximum total time to allow the command to run (default:0
- no limit for command actions,300
- 5 minutes for wait actions).
A cmd
action executes arbitrary commands or scripts within a shell wrapper. You can use the cmd
key to define the command(s) to run. This can also be a multi-line script. You cannot use cmd
and wait
in the same action.
Within each of the action
lists (before
, after
, onSuccess
, and onFailure
), the following action configurations are available:
cmd
- (required if not a wait action) the command to run.dir
- the directory to run the command in, defaults to the current working directory.mute
- whether to mute the realtime output of the command, output is always shown at the end on failure (default:false
).maxRetries
- the maximum number of times to retry the command if it fails (default:0
- no retries).env
- an array of environment variables to set for the command in the form ofname=value
.setVariables
- set the standard output of the command to a list of variables that can be used in other actions or components (onDeploy only).shell
- set a preferred shell for the command to run in for a particular operating system (default issh
for macOS/Linux andpowershell
for Windows).
The wait
action temporarily halts the component stage it is initiated in, either until the specified condition is satisfied or until the maxTotalSeconds time limit is exceeded (which, by default, is set to 5 minutes). To define wait
parameters, execute the wait
key; it is essential to note that you cannot use cmd
and wait
in the same action. Essentially, a wait
action is yaml sugar for a call to ./zarf tools wait-for
.
Within each of the action
lists (before
, after
, onSuccess
, and onFailure
), the following action configurations are available:
wait
- (required if not a cmd action) the wait parameters.cluster
- perform a wait operation on a Kubernetes resource (kubectl wait).kind
- the kind of resource to wait for (required).name
- the name of the resource to wait for (required), can be a name or label selector.namespace
- the namespace of the resource to wait for.condition
- the condition to wait for (default:exists
).
network
- perform a wait operation on a network resource (curl).protocol
- the protocol to use (i.e.http
,https
,tcp
).address
- the address/port to wait for (required).code
- the HTTP status code to wait for if usinghttp
orhttps
, orsuccess
to check for any 2xx response code (default:success
).
Below are some examples of putting together simple actions at various points in the Zarf lifecycle:
Below is a simple example of an onCreate
action set that declares defaults
as well as before
and after
action lists:
Below is an example of an onDeploy
action set that demonstrates how you can use onFailure
actions to perform cleanup tasks or user messaging when an action of component lifecycle step fails:
Below are examples of waiting for resources to exist or be available within an action using wait
actions:
As you may have noticed mentioned in the before
action list of the above Simple onCreate
example, Zarf provides some helpful transformations that help enhance cross-platform compatibility and allow you to better orchestrate Zarf and its components.
Below are the transformations that Zarf will make on an action before it is ran:
- Replace
./zarf
with the path to the currently running Zarf executable.- This allows you to run Zarf in Zarf and is designed to help you use
zarf tools
commands in the air gap.
- This allows you to run Zarf in Zarf and is designed to help you use
- Replace common Unix commands and shell syntax with
powershell
/pwsh
alternatives on Windows.- This allows commands like
touch
to work on Windows and while not perfect enhances cross-platform capabilities.
- This allows commands like
- Add
env
entries for all previously declared Zarfvariables
.- This allows you to use variables in actions and when combined with
setVariables
allows you to chainvariables
from an action for use in later actions or templates.
- This allows you to use variables in actions and when combined with
Within onDeploy
action lists, you can use the setVariables
action configuration to set a list of variables that can be used in other actions or components during zarf package deploy
. The variable value will be assigned in two environment variables: ZARF_VAR_{NAME}
and TF_VAR_{name}
. These values will be accessible in subsequent actions and can be used for templating in files
or manifests
in other components as ###ZARF_VAR_{NAME}###
. This feature allows package authors to define dynamic runtime variables for consumption by other components or actions.
Below is an example of an onRemove
action set that demonstrates how you can use ./zarf
to use Zarf commands like zarf tools kubectl
to perform actions on systems that might not have the pre-requisite software (like kubectl
) installed onto them:
Below are a few more use cases from other examples
and packages
for how actions can be used:
The below example shows the kiwix-serve
component from the data injections example which downloads a .zim
file with an onCreate.before
action for inclusion into the Zarf package.
The below example includes the eksctl
binary and eks.yaml
file in one component, setting it up in an onDeploy.after
action and then uses the eksctl
binary in a second component to create an EKS cluster in an onDeploy.before
action.
The below example shows using a wait
command to wait for a GitOps deployment to happen after Zarf configures the initial GitRepository
manifest. By default Zarf will only track the resources it directly deploys, but adding a wait
action allows you to control the lifecycle more directly.