task
task copied to clipboard
passing environment variables to task via command directly on windows
- 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
varslike current
Or any other ideas? Open for discussion.
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.
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
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?
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.
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
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. 🙂
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 That's great. 🙂
Is this working like this? I tried it with both code above, and the results are the same for Version 3 now.