website/docs/concepts/task.mdx
import VersionLabel from '@site/src/components/Docs/VersionLabel';
Tasks are commands that are ran in the context of a project. Underneath the hood, a task is simply a binary or system command that is ran as a child process.
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 the tasks setting, and can be
written in camel/kebab/snake case. IDs support alphabetic unicode characters, 0-9, _, -, /,
., and must start with a character.
A task ID can be paired with a scope to create a target.
Tasks are grouped into 1 of the following types based on their configured parameters.
outputs setting.options.persistent setting.Alongside types, tasks can also grouped into a special mode that provides unique handling within the action graph and pipelines.
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, and the task is marked as persistent.
To mark a task as local only, enable the preset setting.
tasks:
dev:
command: 'start-dev-server'
preset: 'server'
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 with moon task.
To mark a task as internal, enable the options.internal setting.
tasks:
prepare:
command: 'intermediate-step'
options:
internal: true
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.
To mark a task as interactive, enable the options.interactive
setting.
tasks:
init:
command: 'init-app'
options:
interactive: true
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:
To mark a task as persistent, enable the options.persistent
setting.
tasks:
dev:
command: 'start-dev-server'
options:
persistent: true
# OR
preset: 'server'
Tasks can be configured per project through moon.*, or for many projects
through .moon/tasks/**/*.
A task is either a command or script, but not both. So what's the difference exactly? In the context
of a moon task, a command is a single binary execution with optional arguments, configured with the
command and args settings (which both
support a string or array). While a script is 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. Refer to the table below for more differences between the 2.
| 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 | ✅ | ✅ |
View the official documentation on task inheritance.