task icon indicating copy to clipboard operation
task copied to clipboard

passing environment variables to task via command directly on windows

Open LittleC opened this issue 6 years ago • 9 comments
trafficstars

  • Task version: master(installed via go)
  • OS: windows
  • Example Taskfile:
version: '2'
env:
  CONFIG: Debug
print:
    cmds:
      - echo $CONFIG

For Mac, I could pass environment variables like this CONFIG=Debug task print, but for windows, I have to do $env:CONFIG='Debug'; task print; Remove-Item Env:\CONFIG in powershell, because task print CONFIG=Release will just parse that as vars rather than env.

I'm wondering if it is possible to provide a way to pass env variable directly in one command like task print $CONFIG=Debug VAR=test, for which

  • variable name start with a $ will be recognized as env
  • otherwise, treated as vars like current

Or any other ideas? Open for discussion.

LittleC avatar May 08 '19 06:05 LittleC

You can set the env in your Taskfile from variables (or environment when no variables are set) using:

version: '2'
env:
  CONFIG: '{{.CONFIG | default "Debug"}}'
tasks:
  print:
    cmds:
      - echo $CONFIG

Then you should be able to run task print CONFIG=Release.

smyrman avatar May 08 '19 10:05 smyrman

OK, that makes sense. thanks a lot!

didn't know could write in this way CONFIG: '{{.CONFIG | default "Debug"}}'

I'll give a try! thanks again

LittleC avatar May 10 '19 09:05 LittleC

I have tried above approach, and generally it would work well. But for task in dependencies, global env would not be shared.

For example,

# https://taskfile.org

version: '2'

env:
  CONFIG: '{{.CONFIG | default "Debug"}}'

tasks:
  foo:
    deps: [bar]
    cmds:
      - echo $CONFIG Configuration for task foo
      - task: bar
        vars:
          CONFIG: "{{.CONFIG}}"
    silent: true

  bar:
    cmds:
      - echo $CONFIG Configuration for task bar
    silent: true

And when running task foo CONFIG=Release, the output would be

Debug Configuration for task bar
Release Configuration for task foo
Release Configuration for task bar

Where first time of executing bar is under Debug configuration.

@smyrman do you have any means to work around this?

LittleC avatar May 10 '19 09:05 LittleC

IMHO, this may be caused by env is evaluated per time task is called? As far as that one is defined as global env, my expectation is evaluating once and shared within all tasks.

LittleC avatar May 10 '19 09:05 LittleC

This looks like a bug to me. @andreynering?

From what I can tell, the variable is passed on to the specific task when passed on the CLI, and not to the global vars or env. In addition, I am surprised that the env is re-evaluated for each task -- is this intentional?

I tried a few different variants, this works, but defeats the purpose of a global env:

# https://taskfile.org

version: '2'

env:
  CONFIG: '{{.CONFIG | default "Debug"}}'

tasks:
  foo:
    deps:
    - task: bar
      vars:
        CONFIG: '{{.CONFIG}}'
    cmds:
    - echo $CONFIG Configuration for task foo
    - task: bar
      vars:
        CONFIG: '{{.CONFIG}}'
    silent: true

  bar:
    cmds:
    - echo $CONFIG Configuration for task bar
    silent: true
$ task foo CONFIG=Release
Release Configuration for task bar
Release Configuration for task foo
Release Configuration for task bar

This does not:

# https://taskfile.org

version: '2'

vars:
  CONFIG: '{{.CONFIG | default "Debug"}}'

tasks:
  foo:
    env:
      CONFIG: '{{.CONFIG}}'
    deps:
    - task: bar
    cmds:
    - echo $CONFIG Configuration for task foo
    - task: bar
    silent: true

  bar:
    env:
      CONFIG: '{{.CONFIG}}'
    cmds:
    - echo $CONFIG Configuration for task bar
    silent: true
ask foo CONFIG=Release
Debug Configuration for task bar
Release Configuration for task foo
Debug Configuration for task bar

smyrman avatar May 10 '19 13:05 smyrman

Hi @LittleC and @smyrman,

Couple of things to discuss here.

First: yes, it's by design that all variables are evaluated on each task run, even global ones. Perhaps that can change in the future, but it's how it works today.

Second: I just pushed an improvement that will allow you to set global variables through the CLI (#192, f0768b3af191efa2a9ef32fba53262eaa914d02f). I think that's enough to allow your use case for now.

Third: the idea of allow setting an environment variable through the CLI by prepending $ is good. 🙂

andreynering avatar May 11 '19 14:05 andreynering

Hi @andreynering @smyrman

Thank you guys for sharing those information.

First: yes, it's by design that all variables are evaluated on each task run, even global ones. Perhaps that can change in the future, but it's how it works today.

I see this is the design right now.

Second: I just pushed an improvement that will allow you to set global variables through the CLI (#192, f0768b3). I think that's enough to allow your use case for now.

So I will use the way provided by @smyrman to accomplish what i want, and also I'll try the global variable feature :P

Third: the idea of allow setting an environment variable through the CLI by prepending $ is good. 🙂

If i have spare time I would like to submit a PR. About the syntax, I'm wondering same as passing variables to CLI, task $ENV1=FOO doSth1 $ENV2=BAR doSth2 $ENV3=FOOBAR will set preceding global env ENV1 as FOO, while the following will be used for task ahead only. Is that okay?

LittleC avatar May 13 '19 05:05 LittleC

@LittleC That's great. 🙂

andreynering avatar May 18 '19 11:05 andreynering

Is this working like this? I tried it with both code above, and the results are the same for Version 3 now.

bayeslearnerold avatar Dec 25 '23 05:12 bayeslearnerold