Skip to main content

Any Variables (#1415)

caution

All experimental features are subject to breaking changes and/or removal at any time. We strongly recommend that you do not use these features in a production environment. They are intended for testing and feedback only.

Currently, Task only supports string variables. This experiment allows you to specify and use the following variable types:

  • string
  • bool
  • int
  • float
  • array
  • map

This allows you to have a lot more flexibility in how you use variables in Task's templating engine. There are two active proposals for this experiment. Click on the tabs below to switch between them.

warning

This experiment proposal breaks the following functionality:

  • Dynamically defined variables (using the sh keyword)
info

To enable this experiment, set the environment variable: TASK_X_ANY_VARIABLES=1. Check out our guide to enabling experiments for more information.

Maps​

This proposal removes support for the sh keyword in favour of a new syntax for dynamically defined variables, This allows you to define a map directly as you would for any other type:

version: 3

tasks:
foo:
vars:
FOO: {a: 1, b: 2, c: 3} # <-- Directly defined map on the `FOO` key
cmds:
- 'echo {{.FOO.a}}'

Migration​

Taskfiles with dynamically defined variables via the sh subkey will no longer work with this experiment enabled. In order to keep using dynamically defined variables, you will need to migrate your Taskfile to use the new syntax.

Previously, you might have defined a dynamic variable like this:

version: 3

tasks:
foo:
vars:
CALCULATED_VAR:
sh: 'echo hello'
cmds:
- 'echo {{.CALCULATED_VAR}}'

With this experiment enabled, you will need to remove the sh subkey and define your command as a string that begins with a $. This will instruct Task to interpret the string as a command instead of a literal value and the variable will be populated with the output of the command. For example:

version: 3

tasks:
foo:
vars:
CALCULATED_VAR: '$echo hello'
cmds:
- 'echo {{.CALCULATED_VAR}}'

If your current Taskfile contains a string variable that begins with a $, you will now need to escape the $ with a backslash (\) to stop Task from executing it as a command.


Common to both proposals​

Both proposals add support for all other variable types by directly defining them in the Taskfile. For example:

Evaluating booleans​

version: 3

tasks:
foo:
vars:
BOOL: false
cmds:
- '{{if .BOOL}}echo foo{{end}}'

Arithmetic​

version: 3

tasks:
foo:
vars:
INT: 10
FLOAT: 3.14159
cmds:
- 'echo {{add .INT .FLOAT}}'

Ranging​

version: 3

tasks:
foo:
vars:
ARRAY: [1, 2, 3]
cmds:
- 'echo {{range .ARRAY}}{{.}}{{end}}'

There are many more templating functions which can be used with the new types of variables. For a full list, see the slim-sprig documentation.

Looping over variables​

Previously, you would have to use a delimiter separated string to loop over an arbitrary list of items in a variable and split them by using the split subkey to specify the delimiter:

version: 3

tasks:
foo:
vars:
LIST: 'foo,bar,baz'
cmds:
- for:
var: LIST
split: ','
cmd: echo {{.ITEM}}

Both of these proposals add support for looping over "collection-type" variables using the for keyword, so now you are able to loop over a map/array variable directly:

version: 3

tasks:
foo:
vars:
LIST: [foo, bar, baz]
cmds:
- for:
var: LIST
cmd: echo {{.ITEM}}

When looping over a map we also make an additional {{.KEY}} variable availabe that holds the string value of the map key. Remember that maps are unordered, so the order in which the items are looped over is random.