mirror of
https://github.com/go-task/task.git
synced 2026-06-23 20:55:52 +00:00
feat: add conditional execution for tasks and commands (#2564)
This commit is contained in:
@@ -616,6 +616,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`
|
||||
@@ -812,6 +833,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
|
||||
|
||||
Reference in New Issue
Block a user