Jehad Nasser
Jehad Nasser

Reputation: 3565

How to create a conditional task in Taskfile?

I have three tasks created with TASKFILE, main-task and its two preconditions(or deps) e.g A and B. I need the main-task to run task A, if task A works fine and did the job then ignore the second task B, otherwise fallback to task B and execute it (that's why I added ignore_error: true). How to put this logic inside main-task using taskfile syntax? thanks

Example:

---
version: 3

tasks:
  A:
    cmds:
      - cmd: exit 1
        ignore_error: true
  
  B:
    cmds:
      - exit 1

  main-task:
    deps: # Run A only, But if it fails then Run B
    cmds:
      - task: # or here: Run A only, But if it fails then Run B

Upvotes: 2

Views: 5612

Answers (2)

Maksim G
Maksim G

Reputation: 11

You can try to use template engine and some kind of if statement like this:

- cmd: sh -c '{{if eq .CLI_ARGS "my_flag"}}echo "executing additional task" && <shell task command>{{end}}'

https://taskfile.dev/usage/#gos-template-engine

https://go-task.github.io/slim-sprig/

Upvotes: 1

Andrey Nering
Andrey Nering

Reputation: 157

Unfortunately, there isn't a way to run a task only if another one failed. You can run it only if succeeded by calling it after the previous one, or always by using defer.

Upvotes: 2

Related Questions