In Taskfile how to use a dotenv file produced by one task in another task

274 Views Asked by At

In Taskfile I would like to use a dotenv file produced by one task in another task. Consider the following example where make-env task produces a .env file and my-task task depends on make-env and loads .env.

---
version: 3

tasks:
  make-env:
    cmds:
      - echo "MY_VAR=MY_VALUE" >> ./.env
  my-task:
    deps:
      - make-env
    dotenv:
      - ./.env
    cmds:
      - echo "MY_VAR = $MY_VAR"

I would expect to see MY_VAR = MY_VALUE after running the task my-task command, but it is MY_VAR = instead. It seems like it loads the dotenv file first (but does not fail because the file does not exist yet?) and then executes all dependencies.

A workaround that I considered is to run make-env task as a step in the my-task command and source the dotenv file, but I would really like to leverage Taskfile's deps and dotenv constructs.

Is there a way to ensure that all dependencies have run before the dotnev files are loaded?

0

There are 0 best solutions below