Purple white and red flowers.
Our garden is growing. We've raised a Series A funding round.
Read more here

Introducing Garden Custom Commands

Jon Edvald
Jon Edvald
March 19, 2022

We’ve added Custom Commands to Garden, to give our users more control and to simplify day-to-day usage of Garden projects. You can now define your own commands in your projects, and run them in the same way that you run the default Garden commands.

You can think of them as a more powerful version of npm package scripts or Makefile targets, with the full capability of Garden's templating engine, and seamless integration with the Garden CLI.

A quick example

Imagine a group of developers. For their day-to-day work, they routinely update a git submodule, and then run a garden <span class="p-color-bg">deploy --dev</span> command with some extra arguments. Sometimes they forget to update the submodule first, or they spend time looking in their history for the correct deploy arguments.

With custom commands, you can define a project level .yaml file with <span class="p-color-bg">kind: Command</span>. These commands can be run just like any other Garden commands (in this example, <span class="p-color-bg">garden api-dev</span>).

Each custom command can include an <span class="p-color-bg">exec</span> block where you can run any system commands and a <span class="p-color-bg">gardenCommand</span> block where you can chain other Garden commands.

You can find detailed documentation on how to use custom commands in our documentation.

Some magic: Syntactic sugar for custom commands

One of our favorite parts about using custom commands is the syntax we have to easily use groups of shell arguments. Specifically, you can use:

  • ${args.$all} - all arguments and options passed to <span class="p-color-bg">garden</span>.
  • ${args.$rest} - all arguments and options that are not explicitly defined in the custom command.
  • ${args["--"]} - to split off a specific group of commands. So for <span class="p-color-bg">garden my-command -- foo --bar</span> you would get an array containing <span class="p-color-bg">foo</span> and <span class="p-color-bg">--bar</span>.

Basically, you get all the power of the Garden templating you’re used to, wrapped up in the simplicity of short and intuitive commands.

You can also go beyond passing any and all arguments to the wrapped scripts and commands. Custom commands allow you to explicitly define your own arguments and/or custom option flags. You can easily template them into your wrapped scripts or Garden commands. These are validated when running the command, and also appear in help texts for the command, ultimately making your team's day-to-day even easier.

Once again, you can find more detailed examples in the docs.

What can you use custom commands for?

Pretty much anything! Internally we have found them useful in all kinds of situations, as they are super simple to run, but also very flexible and powerful to build. Here are some cases where you might find custom commands most useful:

  • Short codes: Instead of having to remember or document a long Garden command that your entire team uses, you can add a custom command and your entire team can have access to it by running a one-word, memorable command. Internally, we tend to run some commands in running containers when developing. For example we use <span class="p-color-bg">garden exec</span> to run a shell in the API service by calling <span class="p-color-bg">garden exec api sh</span>. Maybe not everyone in the team even knows that this is possible, but by wrapping it in a custom command with a friendlier name, you can allow your whole team to use more powerful tools. And beyond running a shell, you might have other commands that you frequently run in deployed containers, such as tests and other scripts. You can now easily codify those as custom commands.
  • Custom scripts: Many teams have shell scripts that wrap Garden commands. This works fine, but requires context switching between running a local script and running <span class="p-color-bg">garden</span>. Now you can wrap your scripts in custom commands for better consistency.
  • Recommended settings: Instead of recommending to your team which arguments and flags to run Garden with, you can wrap all your recommended settings into a custom command. And your team isn’t limited by this, as you can use the ${args.$all} shortcut as in the example above to allow them to add their own options to the preset defaults.
  • Tests, database migrations, and more: If you have tasks you regularly run, such as database migrations or tests, you can wrap these in custom commands but keep all of the power of Garden templating and arg parsing to ensure that developers are not limited by the defaults.

Because custom commands are treated like standard Garden commands, you can see them listed with a simple <span class="p-color-bg">garden --help</span>. They will appear after the built-in commands. You can also run <span class="p-color-bg">garden <my-custom-command> --help</span> to see how an individual command is used.

Feedback or requests?

Let us know what you are using custom commands for, and if you run into any limitations! We do still consider the API experimental so it's certainly open for improvement, and we are actively looking for feedback to improve them. But don't hesitate to adopt custom commands—we promise any improvements will be backwards compatible! By all means tell us what you think by opening a GitHub issue, or a discussion in our community forum.

previous arrow
newt arrow