feat: add conditional execution for tasks and commands (#2564)

This commit is contained in:
Valentin Maerten
2026-01-21 23:05:40 +01:00
committed by GitHub
parent da7eb0c855
commit 9bc1efbc47
28 changed files with 444 additions and 0 deletions

160
testdata/if/Taskfile.yml vendored Normal file
View File

@@ -0,0 +1,160 @@
version: '3'
vars:
SHOULD_RUN: "yes"
ENV: "prod"
FEATURE_ENABLED: "true"
FEATURE_DISABLED: "false"
tasks:
# Basic command-level if (condition met)
cmd-if-true:
cmds:
- cmd: echo "executed"
if: "true"
# Basic command-level if (condition not met)
cmd-if-false:
cmds:
- cmd: echo "should not appear"
if: "false"
- echo "this runs"
# Task-level if (condition met)
task-if-true:
if: "true"
cmds:
- echo "task executed"
# Task-level if (condition not met)
task-if-false:
if: "false"
cmds:
- echo "should not appear"
# With template variables
if-with-template:
cmds:
- cmd: echo "Running because SHOULD_RUN={{.SHOULD_RUN}}"
if: '[ "{{.SHOULD_RUN}}" = "yes" ]'
# If inside for loop
if-in-for-loop:
cmds:
- for: ["a", "b", "c"]
cmd: echo "processing {{.ITEM}}"
if: '[ "{{.ITEM}}" != "b" ]'
# If on task call
if-on-task-call:
cmds:
- task: subtask
if: "true"
subtask:
internal: true
cmds:
- echo "subtask ran"
# If combined with platforms (both must pass)
if-with-platforms:
cmds:
- cmd: echo "condition and platform met"
platforms: [linux, darwin, windows]
if: "true"
# Skip task call
skip-task-call:
cmds:
- task: subtask
if: "false"
- echo "after skipped task call"
# Task call in cmds with if condition met
task-call-if-true:
cmds:
- task: subtask
if: "true"
- echo "after task call"
# Task call in cmds with if condition not met
task-call-if-false:
cmds:
- task: subtask
if: "false"
- echo "continues after skipped task"
# Template eq - condition met
template-eq-true:
cmds:
- cmd: echo "env is prod"
if: '{{ eq .ENV "prod" }}'
# Template eq - condition not met
template-eq-false:
cmds:
- cmd: echo "should not appear"
if: '{{ eq .ENV "dev" }}'
- echo "this runs"
# Template ne (not equal)
template-ne:
cmds:
- cmd: echo "env is not dev"
if: '{{ ne .ENV "dev" }}'
# Template with boolean-like variable
template-bool-true:
cmds:
- cmd: echo "feature enabled"
if: '{{ eq .FEATURE_ENABLED "true" }}'
# Template with boolean-like variable (false)
template-bool-false:
cmds:
- cmd: echo "should not appear"
if: '{{ eq .FEATURE_DISABLED "true" }}'
- echo "feature was disabled"
# Direct true/false from template
template-direct-true:
cmds:
- cmd: echo "direct true works"
if: '{{ .FEATURE_ENABLED }}'
# Direct true/false from template (false case)
template-direct-false:
cmds:
- cmd: echo "should not appear"
if: '{{ .FEATURE_DISABLED }}'
- echo "direct false skipped correctly"
# Template with CLI variable override
template-cli-var:
cmds:
- cmd: echo "MY_VAR is yes"
if: '{{ eq .MY_VAR "yes" }}'
# Combined template conditions with and
template-and:
cmds:
- cmd: echo "both conditions met"
if: '{{ and (eq .ENV "prod") (eq .FEATURE_ENABLED "true") }}'
# Combined template conditions with or
template-or:
cmds:
- cmd: echo "at least one condition met"
if: '{{ or (eq .ENV "dev") (eq .ENV "prod") }}'
# Task-level if with template
task-level-template:
if: '{{ eq .ENV "prod" }}'
cmds:
- echo "task runs in prod"
# Task-level if with template (not met)
task-level-template-false:
if: '{{ eq .ENV "dev" }}'
cmds:
- echo "should not appear"

View File

@@ -0,0 +1 @@
this runs

View File

@@ -0,0 +1 @@
executed

View File

@@ -0,0 +1,7 @@
task: "if-in-for-loop" started
task: [if-in-for-loop] echo "processing a"
processing a
task: [if-in-for-loop] if condition not met - skipped
task: [if-in-for-loop] echo "processing c"
processing c
task: "if-in-for-loop" finished

View File

@@ -0,0 +1,5 @@
task: "task-call-if-false" started
task: [task-call-if-false] if condition not met - skipped
task: [task-call-if-false] echo "continues after skipped task"
continues after skipped task
task: "task-call-if-false" finished

View File

@@ -0,0 +1,2 @@
subtask ran
after task call

View File

@@ -0,0 +1 @@
task: if condition not met - skipped: "task-if-false"

View File

@@ -0,0 +1 @@
task executed

View File

@@ -0,0 +1 @@
task: if condition not met - skipped: "task-level-template-false"

View File

@@ -0,0 +1 @@
task runs in prod

View File

@@ -0,0 +1 @@
both conditions met

View File

@@ -0,0 +1 @@
feature was disabled

View File

@@ -0,0 +1 @@
feature enabled

View File

@@ -0,0 +1 @@
MY_VAR is yes

View File

@@ -0,0 +1 @@
direct false skipped correctly

View File

@@ -0,0 +1 @@
direct true works

View File

@@ -0,0 +1,5 @@
task: "template-eq-false" started
task: [template-eq-false] if condition not met - skipped
task: [template-eq-false] echo "this runs"
this runs
task: "template-eq-false" finished

View File

@@ -0,0 +1 @@
env is prod

View File

@@ -0,0 +1 @@
env is not dev

View File

@@ -0,0 +1 @@
at least one condition met