Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing environment variables to task via command directly on windows #203

Open
LittleC opened this issue May 8, 2019 · 9 comments
Open

Comments

@LittleC
Copy link

LittleC commented May 8, 2019

  • 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.

@smyrman
Copy link
Contributor

smyrman commented May 8, 2019

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.

@LittleC
Copy link
Author

LittleC commented May 10, 2019

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 LittleC closed this as completed May 10, 2019
@LittleC LittleC reopened this May 10, 2019
@LittleC
Copy link
Author

LittleC commented May 10, 2019

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
Copy link
Author

LittleC commented May 10, 2019

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.

@smyrman
Copy link
Contributor

smyrman commented May 10, 2019

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

@andreynering
Copy link
Member

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, f0768b3). 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. 🙂

@LittleC
Copy link
Author

LittleC commented May 13, 2019

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?

@andreynering
Copy link
Member

@LittleC That's great. 🙂

@bayeslearner
Copy link

bayeslearner commented Dec 25, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants