Skip to content

Option to assign env from vars #1863

@onedr0p

Description

@onedr0p

Hi 👋🏼

Sometimes my taskfiles and scripts they call require both vars and env to be set to the same values. You can imagine a cmd using a var {{.VAR}} and the script it calls (or envsubst) also using the same var but as env $VAR.

It would be great if there was a way to easily have env inherit vars set on a task.

Current issue

version: '3'

tasks:
  test:
    cmds:
      - echo "using vars {{.FOO}}"
      - echo "using env $FOO and $FIZZ"
      - cat test.txt | envsubst # test.txt has content '$FOO:$FIZZ'
    vars: &vars
      FOO: bar
    env:
      <<: *vars
      FIZZ: buzz

Output with various TASK_X_MAP_VARIABLES

TASK_X_MAP_VARIABLES=0 task test
err:  maps cannot be assigned to variables
file: /tmp/Taskfile.yaml:8:11
   6 |       - echo "using vars {{.FOO}}"
   7 |       - echo "using env $FOO and $FIZZ"
>  8 |     vars: &vars
     |           ^
   9 |       FOO: bar
  10 |     env:
TASK_X_MAP_VARIABLES=1 task test
task: [test] echo "using vars bar"
using vars bar
task: [test] echo "using env $FOO and $FIZZ"
using env  and buzz
task: [test] cat test.txt | envsubst
:buzz

Purposed solution

Even if the above worked, it would still be great to have the below to avoid the anchor/alias jank.

version: '3'

tasks:
  test:
    cmds:
      - echo "using vars {{.FOO}}"
      - echo "using env $FOO and $FIZZ"
      - cat test.txt | envsubst # test.txt has content '$FOO:$FIZZ'
    vars:
      FOO: bar
    envFromVars: true # assign all vars to env
    env:
      FIZZ: buzz # and also allow adding other env

Expected Output

task test
task: [test] echo "using vars bar"
using vars bar
task: [test] echo "using env $FOO and $FIZZ"
using env bar and buzz
task: [test] cat test.txt | envsubst
bar:buzz

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions