docs(if): add documentation for conditional execution

This commit is contained in:
Valentin Maerten
2025-12-08 22:29:13 +01:00
parent f6fd264b63
commit d778576389
2 changed files with 135 additions and 0 deletions

View File

@@ -1007,6 +1007,99 @@ tasks:
- echo "I will not run"
```
### Conditional execution with `if`
The `if` attribute allows you to conditionally skip tasks or commands based on a
shell command's exit code. Unlike `preconditions` which fail and stop execution,
`if` simply skips the task or command when the condition is not met and continues
with the rest of the Taskfile.
#### Task-level `if`
When `if` is set on a task, the entire task is skipped if the condition fails:
```yaml
version: '3'
tasks:
deploy:
if: '[ "$CI" = "true" ]'
cmds:
- echo "Deploying..."
- ./deploy.sh
```
#### Command-level `if`
When `if` is set on a command, only that specific command is skipped:
```yaml
version: '3'
tasks:
build:
cmds:
- cmd: echo "Building for production"
if: '[ "$ENV" = "production" ]'
- cmd: echo "Building for development"
if: '[ "$ENV" = "development" ]'
- go build ./...
```
#### Using templates in `if` conditions
You can use Go template expressions in `if` conditions. Template expressions like
`{{eq .VAR "value"}}` evaluate to `true` or `false`, which are valid shell
commands (`true` exits with 0, `false` exits with 1):
```yaml
version: '3'
tasks:
conditional:
vars:
ENABLE_FEATURE: "true"
cmds:
- cmd: echo "Feature is enabled"
if: '{{eq .ENABLE_FEATURE "true"}}'
- cmd: echo "Feature is disabled"
if: '{{ne .ENABLE_FEATURE "true"}}'
```
#### Using `if` with `for` loops
When used inside a `for` loop, the `if` condition is evaluated for each iteration:
```yaml
version: '3'
tasks:
process-items:
cmds:
- for: ['a', 'b', 'c']
cmd: echo "processing {{.ITEM}}"
if: '[ "{{.ITEM}}" != "b" ]'
```
This will output:
```
processing a
processing c
```
#### `if` vs `preconditions`
| Aspect | `if` | `preconditions` |
|--------|------|-----------------|
| On failure | Skips (continues) | Fails (stops) |
| Message | Only in verbose mode | Always shown |
| Use case | "Run if possible" | "Must be true" |
Use `if` when you want optional conditional execution that shouldn't stop the
workflow. Use `preconditions` when the condition must be met for the task to
make sense.
### Limiting when tasks run
If a task executed by multiple `cmds` or multiple `deps` you can control when it

View File

@@ -614,6 +614,27 @@ tasks:
- ./deploy.sh
```
#### `if`
- **Type**: `string`
- **Description**: Shell command to conditionally execute the task. If the
command exits with a non-zero code, the task is skipped (not failed).
```yaml
tasks:
# Task only runs in CI environment
deploy:
if: '[ "$CI" = "true" ]'
cmds:
- ./deploy.sh
# Using Go template expressions
build-prod:
if: '{{eq .ENV "production"}}'
cmds:
- go build -ldflags="-s -w" ./...
```
### `dir`
- **Type**: `string`
@@ -810,6 +831,27 @@ tasks:
SERVICE: '{{.ITEM}}'
```
### Conditional Commands
Use `if` to conditionally execute a command. If the shell command exits with a
non-zero code, the command is skipped.
```yaml
tasks:
build:
cmds:
# Only run in production
- cmd: echo "Optimizing for production"
if: '[ "$ENV" = "production" ]'
# Using Go templates
- cmd: echo "Feature enabled"
if: '{{eq .ENABLE_FEATURE "true"}}'
# Inside for loops (evaluated per iteration)
- for: [a, b, c]
cmd: echo "processing {{.ITEM}}"
if: '[ "{{.ITEM}}" != "b" ]'
```
## Shell Options
### Set Options