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

dotenv file path not using var from command line #658

Open
mkyc opened this issue Jan 18, 2022 · 1 comment
Open

dotenv file path not using var from command line #658

mkyc opened this issue Jan 18, 2022 · 1 comment

Comments

@mkyc
Copy link

mkyc commented Jan 18, 2022

  • Task version: v3.10.0 (h1:vOAyD9Etsz9ibedBGf1Mu0DpD6V0T1u3VG6Nwh89lDY=)
  • Operating System: macOS Monterey 12.1

Taskfile.yml:

version: '3'

vars:
  ACCOUNT: test

dotenv: [ ".env.{{.ACCOUNT}}" ]

tasks:
  print:
    cmds:
      - echo $TEST_VAR

.env.test:

TEST_VAR=lilili

.env.other:

TEST_VAR=lalala
~ task print                                
task: [print] echo $TEST_VAR
lilili
~task print ACCOUNT=other
task: [print] echo $TEST_VAR
lilili

should be lalala.

I guess dotenv is evaluated before command line args.

@tylermmorton tylermmorton added the type: bug Something not working as intended. label Jan 22, 2022
@pd93 pd93 removed the v4 label Oct 15, 2022
@matthijsdewit111
Copy link

matthijsdewit111 commented Jul 2, 2024

Hope this will be fixed soon, but right now I found 2 possible workarounds.

The first one is that you can define the dotenv: part on task level, and this works as expected. The drawback is however that you need to duplicate it throughout the taskfile in each task that needs then environment variables. For example:

version: '3'

vars:
  ACCOUNT: test

tasks:
  print:
    dotenv: [ ".env.{{.ACCOUNT}}" ]
    cmds:
      - echo $TEST_VAR

The other method is the use the strange fact that using variables seems to work ok with in the env: and sh: part. You can read and parse the .env file manually here. Which is very ugly/verbose, but that you only need to do it once in the taskfile. For example:

version: '3'

vars:
  ACCOUNT: test

env:
  TEST_VAR:
    sh: cat .env.{{.ACCOUNT}} | grep -i "TEST_VAR=" | sed -e "s/TEST_VAR=//"

tasks:
  print:
    cmds:
      - echo $TEST_VAR

(This will also print a warning: cat: .env.: No such file or directory, but it still works. Pretty sure the command gets evaluated twice, once on general setup, and once again for the setup of the task.)

@pd93 pd93 removed the type: bug Something not working as intended. label Dec 16, 2024
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

5 participants