|
| 1 | +--- |
| 2 | +title: New `if:` Control and Variable Prompt |
| 3 | +description: Introduction of the `if:` control and required variable prompts. |
| 4 | +author: vmaerten |
| 5 | +date: 2026-01-24 |
| 6 | +outline: deep |
| 7 | +--- |
| 8 | + |
| 9 | +# New `if:` control and interactivity support |
| 10 | + |
| 11 | +<AuthorCard :author="$frontmatter.author" /> |
| 12 | + |
| 13 | +The [v3.47.0][release] release is here, and it brings two exciting new features |
| 14 | +to Task. Let's take a closer look at them! |
| 15 | + |
| 16 | +## The New `if:` Control |
| 17 | + |
| 18 | +This first feature is simply the second most upvoted issue of all time (!) with |
| 19 | +58 :thumbsup:s (!!) at the time of writing. |
| 20 | + |
| 21 | +It introduces the `if:` control, which allow you to conditionally skip the |
| 22 | +execution of certain tasks and proceeding. `if:` can be set on a task-level or |
| 23 | +command-level, and can be either a Bash command or a Go template expression. |
| 24 | + |
| 25 | +Let me show a couple of examples. |
| 26 | + |
| 27 | +Task-level with Bash expression: |
| 28 | + |
| 29 | +```yaml |
| 30 | +version: '3' |
| 31 | + |
| 32 | +tasks: |
| 33 | + deploy: |
| 34 | + if: '[ "$CI" = "true" ]' |
| 35 | + cmds: |
| 36 | + - echo "Deploying..." |
| 37 | + - ./deploy.sh |
| 38 | +``` |
| 39 | +
|
| 40 | +Command-level with Go template expression: |
| 41 | +
|
| 42 | +```yaml |
| 43 | +version: '3' |
| 44 | + |
| 45 | +tasks: |
| 46 | + conditional: |
| 47 | + vars: |
| 48 | + ENABLE_FEATURE: "true" |
| 49 | + cmds: |
| 50 | + - cmd: echo "Feature is enabled" |
| 51 | + if: '{{eq .ENABLE_FEATURE "true"}}' |
| 52 | + - cmd: echo "Feature is disabled" |
| 53 | + if: '{{ne .ENABLE_FEATURE "true"}}' |
| 54 | +``` |
| 55 | +
|
| 56 | +For more details, please check out the [documentation][if-docs]. |
| 57 | +The [examples][if-examples] from the test suite may be useful too. |
| 58 | +
|
| 59 | +::: info |
| 60 | +
|
| 61 | +We had similar functionality before, but nothing that perfectly fits this use |
| 62 | +case. There were [`sources:`][sources] and [`status:`][status], but those were |
| 63 | +meant to check if a task was up-to-date, and [`preconditions:`][preconditions], |
| 64 | +but this would halt the execution of the task instead of skipping it. |
| 65 | + |
| 66 | +::: |
| 67 | + |
| 68 | +## Prompt for Required Variables |
| 69 | + |
| 70 | +For backward-compatibility reasons, this feature is disabled by default. |
| 71 | +To enable it, either pass `--interactive` flag or add `interactive: true` to |
| 72 | +your `.taskrc.yml`. |
| 73 | + |
| 74 | +Once you do that, Task will basically starting prompting you in runtime for any |
| 75 | +required variables. In the example below, `NAME` will be prompted at runtime: |
| 76 | + |
| 77 | +```yaml |
| 78 | +version: '3' |
| 79 | +
|
| 80 | +tasks: |
| 81 | + # Simple text input prompt |
| 82 | + greet: |
| 83 | + desc: Greet someone by name |
| 84 | + requires: |
| 85 | + vars: |
| 86 | + - NAME |
| 87 | + cmds: |
| 88 | + - echo "Hello, {{.NAME}}!" |
| 89 | +``` |
| 90 | + |
| 91 | +If a given variable has an enum, Task will actually show a selection menu so you |
| 92 | +can choose the right option instead of typing: |
| 93 | + |
| 94 | +```yaml |
| 95 | +version: '3' |
| 96 | +
|
| 97 | +tasks: |
| 98 | + # Enum selection (dropdown menu) |
| 99 | + deploy: |
| 100 | + desc: Deploy to an environment |
| 101 | + requires: |
| 102 | + vars: |
| 103 | + - name: ENVIRONMENT |
| 104 | + enum: [dev, staging, prod] |
| 105 | + cmds: |
| 106 | + - echo "Deploying to {{.ENVIRONMENT}}..." |
| 107 | +``` |
| 108 | + |
| 109 | +Once again, check out the [documentation][prompt-docs] for more details, and the |
| 110 | +[prompt examples][prompt-examples] from the test suite. |
| 111 | + |
| 112 | +## Feedback |
| 113 | + |
| 114 | +Let's us know if you have any feedback! You can find us on our |
| 115 | +[Discord server][discord]. |
| 116 | + |
| 117 | +[release]: https://github.com/go-task/task/releases/tag/v3.47.0 |
| 118 | +[vmaerten]: https://github.com/vmaerten |
| 119 | +[sources]: https://taskfile.dev/docs/guide#by-fingerprinting-locally-generated-files-and-their-sources |
| 120 | +[status]: https://taskfile.dev/docs/guide#using-programmatic-checks-to-indicate-a-task-is-up-to-date |
| 121 | +[preconditions]: https://taskfile.dev/docs/guide#using-programmatic-checks-to-cancel-the-execution-of-a-task-and-its-dependencies |
| 122 | +[if-docs]: https://taskfile.dev/docs/guide#conditional-execution-with-if |
| 123 | +[if-examples]: https://github.com/go-task/task/blob/main/testdata/if/Taskfile.yml |
| 124 | +[prompt-docs]: https://taskfile.dev/docs/guide#prompting-for-missing-variables-interactively |
| 125 | +[prompt-examples]: https://github.com/go-task/task/blob/main/testdata/interactive_vars/Taskfile.yml |
| 126 | +[discord]: https://discord.com/invite/6TY36E39UK |
0 commit comments