Skip to content

Conversation

@vmaerten
Copy link
Member

@vmaerten vmaerten commented Dec 10, 2025

Summary

  • Add if attribute for conditional execution of tasks and commands
  • When the shell command in if exits with non-zero, the task/command is skipped (not failed)
  • Supports Go template expressions (e.g., {{eq .VAR "value"}}) which evaluate to true/false
  • Works with for loops, evaluating the condition for each iteration

fixes #608

Turns out it was not that hard and it can solve most of problems described in the issue

Example

tasks:
  deploy:
    if: '[ "$CI" = "true" ]'
    cmds:
      - cmd: echo "Production build"
        if: '{{eq .ENV "production"}}'
      - ./deploy.sh

Add `if` field to tasks and commands that allows conditional execution
based on a shell command exit code. When the condition returns non-zero,
the task/command is silently skipped (with verbose logging).

- Task-level: skip entire task if condition fails
- Command-level: skip individual command if condition fails
- Supports template variables in conditions
- Works with for loops (condition evaluated per iteration)
- Works with deferred commands
Add comprehensive test cases for:
- Task calls in cmds with if conditions
- Go template functions (eq, ne, and, or)
- Direct template variable evaluation
- CLI variable overrides
- Task-level if with templates
Add comprehensive golden fixture tests for if feature:
- Command-level if (true/false)
- Task-level if (true/false)
- Task calls with if conditions
- Go template functions (eq, ne, and, or)
- Direct template variable evaluation
- CLI variable overrides
- For loops with if conditions
@vmaerten vmaerten changed the title feat(if): add conditional execution for tasks and commands feat: add conditional execution for tasks and commands Dec 11, 2025
@vmaerten vmaerten marked this pull request as ready for review December 11, 2025 20:58
if err := execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: t.If,
Dir: t.Dir,
Env: env.Get(t),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes people ask to have Posix and Bash opt available on these commands. I don't know if that is a good or a bad idea, never the less please see #2538 where the idea is in draft form.

@vmaerten vmaerten self-assigned this Dec 12, 2025
@vmaerten vmaerten added the area: exec Changes related to the execution of commands. label Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: exec Changes related to the execution of commands.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add if: keyword to control when a task or command is ran?

3 participants