Overview
Tasks represent individual units of work in your build pipeline:- Building and compiling code
- Running tests and linters
- Type checking
- Starting development servers
- Deploying applications
Task IDs
A task identifier (or name) is a unique resource for locating a task within a project. The ID is explicitly configured as a key within thetasks setting, and can be written in camel/kebab/snake case.
ID Requirements
IDs support:- Alphabetic unicode characters
- Numbers
0-9 - Underscores
_, hyphens-, forward slashes/, dots. - Must start with a character
moon.yml
Task Types
Tasks are grouped into 1 of the following types based on their configured parameters:Build Tasks
Task generates one or many artifacts, and is derived from theoutputs setting.
moon.yml
Build tasks are cached by default. Moon uses the outputs to determine if the task needs to re-run.
Test Tasks
Task asserts code is correct and behaves as expected. This includes linting, typechecking, unit tests, and any other form of testing. This is the default type.moon.yml
Run Tasks
Task runs a one-off, long-running, or never-ending process, and is derived from theoptions.persistent setting.
moon.yml
Task Modes
Alongside types, tasks can also be grouped into special modes that provide unique handling within the action graph and pipelines.Local Server
Tasks either run locally, in CI (continuous integration pipelines), or both. For tasks that should only be ran locally, for example, development servers, we provide a mechanism for marking a task as local only server. When enabled:- Caching is turned off
- The task will not run in CI
- Terminal output is not captured
- The task is marked as persistent
moon.yml
Internal Only
Internal tasks are tasks that are not meant to be ran explicitly by the user (via the command line), but are used internally as dependencies of other tasks. Additionally, internal tasks are not displayed in a project’s tasks list, but can be inspected withmoon task.
moon.yml
Internal tasks help keep your task lists clean while enabling complex build workflows.
Interactive
Tasks that need to interact with the user via terminal prompts are known as interactive tasks. Because interactive tasks require stdin, and it’s not possible to have multiple parallel running tasks interact with stdin, we isolate interactive tasks from other tasks in the action graph. This ensures that only 1 interactive task is ran at a time.moon.yml
Persistent
Tasks that never complete, like servers and watchers, are known as persistent tasks. Persistent tasks are typically problematic when it comes to dependency graphs, because if they run in the middle of the graph, subsequent tasks will never run because the persistent task never completes! However in moon, this is a non-issue, as we collect all persistent tasks within the action graph and run them last as a batch. This is perfect for a few reasons:- All persistent tasks are ran in parallel, so they don’t block each other
- Running both the backend API and frontend webapp in parallel is a breeze
- Dependencies of persistent tasks are guaranteed to have ran and completed
moon.yml
apps/api/moon.yml
apps/web/moon.yml
Task Configuration
Tasks can be configured per project throughmoon.yml, or for many projects through .moon/tasks/**/*.
Commands vs Scripts
A task is either a command or script, but not both. So what’s the difference exactly? Command: A single binary execution with optional arguments, configured with thecommand and args settings (which both support a string or array).
Script: One or many binary executions, with support for pipes and redirects, and configured with the script setting (which is only a string).
A command also supports merging during task inheritance, while a script does not and will always replace values.
| Feature | Command | Script |
|---|---|---|
| Configured as | string, array | string |
| Inheritance merging | ✅ via mergeArgs option | ⚠️ always replaces |
| Additional args | ✅ via args setting | ❌ |
| Passthrough args (from CLI) | ✅ | ❌ |
Multiple commands (with && or ;) | ❌ | ✅ |
| Pipes, redirects, etc | ❌ | ✅ |
| Always ran in a shell | ❌ | ✅ |
| Custom platform/toolchain | ✅ | ✅ |
| Token functions and variables | ✅ | ✅ |
moon.yml
moon.yml
Inputs and Outputs
Define inputs to determine when a task should re-run, and outputs for caching:moon.yml
Moon uses inputs to generate a hash for cache invalidation. If inputs haven’t changed and a cache exists, the task is skipped.
File Groups
Use file groups to reuse input patterns:.moon/tasks/node.yml
Environment Variables
Define environment variables for tasks:moon.yml
Task Dependencies
Define dependencies between tasks:moon.yml
~:task- Task in the same project^:task- Task in all project dependenciesproject:task- Specific project and task
Options
Control task behavior with options:moon.yml
Real-World Examples
Development Server
moon.yml
Build Pipeline
moon.yml
Conditional Tasks
moon.yml
Task Inheritance
View the official documentation on task inheritance.Best Practices
Define explicit inputs
Define explicit inputs
Always specify task inputs to enable accurate caching. Avoid using
**/* (the default) which includes everything.Use deps for task ordering
Use deps for task ordering
Instead of chaining commands with
&&, use the deps field to express task dependencies. This enables better parallelization and caching.Prefer commands over scripts
Prefer commands over scripts
Commands are more portable, support better merging, and allow passthrough args. Use scripts only when you need shell features like pipes.
Mark persistent tasks correctly
Mark persistent tasks correctly
Always mark dev servers and watchers as persistent to ensure they run last and don’t block other tasks.
Related Concepts
- Task Inheritance - Inherit tasks across projects
- Targets - Project:task identifiers
- Cache - Task output caching
- Affected - Smart task execution