Workflows
Workflows allow users to define simple, CI-like sequences of Garden commands and script steps, that can be run from a command line, in CI pipelines or directly triggered from PRs or branches using Garden Enterprise.
Custom shell scripts can be used for preparation ahead of running Garden commands, handling outputs from the commands, and more.
A sequence of commands executed in a workflow is also generally more efficent than scripting successive runs of Garden CLI commands, since state is cached between the commands, and there is no startup delay between the commands.
How it Works
Workflows are defined with a separate kind of configuration file, with a list of steps:
We suggest making a workflows.garden.yml
next to your project configuration in your project root. You can also place your workflow definitions in your project root project.garden.yml
/garden.yml
file (with a ---
separator after the project configuration).
Each step in your workflow can either trigger Garden commands, or run custom scripts. The steps are executed in succession. If a step fails, the remainder of the workflow is aborted.
You can run a workflow by running garden run workflow <name>
, or have it trigger automatically via Garden Enterprise.
Command steps
A simple command step looks like this:
You can also provide arguments to commands, and even template them:
Not all Garden commands can be run in workflows, and some option flags are not available. Please see the command reference to see which commands are supported in workflows.
The available keys for templating can be found in the template reference.
Script steps
A script step looks something like this:
Scripts can also be templated:
Environment variables
To explicitly provide environment variables to the steps of a workflow, you can use the workflow.envVars
field:
Workflow-level environment variables like this can be useful e.g. for providing templated values (such as secrets or project variables) to several script steps, or to initialize providers in the context of a CI system.
Note that workflow-level environment variables apply to all steps of a workflow (both command and script steps).
The skip
and when
options
skip
and when
optionsBy default, a workflow step is run if all previous steps have been run without errors. Sometimes, it can be useful to override this default behavior with the skip
and when
fields on workflow steps.
The skip
field is a boolean. If its value is true
, the step will be skipped, and the next step will be run as if the skipped step succeeded.
Note that skipped steps don't produce any outputs (see the step outputs section below for more). However, skipped steps are shown in the command log.
The when
field can be used with the following values:
onSuccess
(default): This step will be run if all preceding steps succeeded or were skipped.onError
: This step will be run if a preceding step failed, or if its preceding step haswhen: onError
. If the next step haswhen: onError
, it will also be run. Otherwise, all subsequent steps are ignored. See below for more.always
: The step will always be run, regardless of whether any previous steps have failed.never
: The step will always be ignored, even if all previous steps succeeded. Note: Ignored steps don't show up in the command logs.
The simplest usage pattern for onError
steps is to place them at the end of your workflow (which ensures that they're run if any step in your workflow fails):
A more advanced use case is to use onError
steps to set up "error handling checkpoints" in your workflow.
For example, if the first step (run task my-task
) fails in this workflow:
then the first two onError
steps will be run, and all other steps will be skipped (except for the last one, since it has when: always
). This can be useful for rollback operations that are relevant only at certain points in the workflow.
You can also template the values of skip
and when
for even more flexibility. For example:
Step outputs
Workflow steps can reference outputs from previous steps, using template strings. This is particularly useful when feeding command outputs to custom scripts, e.g. for custom publishing flows, handling artifacts and whatever else you can think of.
For example, to retrieve a module version after a build:
You can also set a name
on a step, to make it easier to reference:
The schema of command outputs can be found in the command reference. Every step also exports a log
key for the full command or script log.
Triggers
Garden Enterprise can monitor your project repository for updates, and trigger workflows automatically on e.g. PR and branch updates.
For example, here's how you'd trigger a workflow for PRs made from any feature/*
branch:
For a full description of how to configure triggers, check out the workflows reference.
Workflows and the Stack Graph
Unlike modules, workflows stand outside of the Stack Graph. They cannot currently depend on each other, and nothing in the Stack Graph can reference or otherwise depend on workflows.
Examples
Authenticate with Google Cloud before deploying a project
Here we use secrets (which are a Garden Enterprise feature) for the auth key, but you can replace those template keys with corresponding ${var.*}
or ${local.env.*}
keys as well.
Next Steps
Take a look at our Variables and Templating section for details on how to use templating in your configuration files.
Also check out Using the CLI for CLI usage examples, and some common day-to-day usage tips.
Last updated