Environment Variables¶
Environment variable in Taskfile¶
There are two ways to declare environment variables in Taskfile:
- Using a
.envfile via thedotnetoption in the Taskfile. - Using
envoption in the Taskfile.
If you want to override the value of an environment variable, you have to reference it using the $ENV_NAME format in the Taskfile, not {{.ENV_NAME}}. You can refer below for more details.
export STAGE=devwill override the value ofSTAGEdefined in.envfile orenvoption in the Taskfile.
Using .env file¶
Info
You can specify dotnet option in global or task level.
Assume you have the following content:
Taskfile.yml
version: '3'
dotenv: # global/system environment variables for all tasks
- '.env'
env:
MYENV: test
tasks:
hello:
dotenv: ['{{ .MYENV }}/.env', '{{ .HOME }}/.env']
cmds:
- 'echo "Endpoint is: $ENDPOINT, Stage is: $STAGE"'
.HOMEis a magic variable that refers to the home directory of the user.export STAGE=dev, if set explicitly in the environment, it will override the value ofSTAGEdefined in.envfile.- but if you set
export MYENV=prod, it will not override the value ofMYENVdefined in Taskfile.
demo and output
ubuntu@touted-mite:~$ task hello
task: [hello] echo "Endpoint is: $ENDPOINT, Stage is: $STAGE"
Endpoint is: testing.com, Stage is: test
# Override the value of STAGE
ubuntu@touted-mite:~$ export STAGE=dev
ubuntu@touted-mite:~$ task hello
task: [hello] echo "Endpoint is: $ENDPOINT, Stage is: $STAGE"
Endpoint is: testing.com, Stage is: dev
# Try to override MYENV but it will not override
ubuntu@touted-mite:~$ export MYENV=dev
ubuntu@touted-mite:~$ task hello
task: [hello] echo "Endpoint is: $ENDPOINT, Stage is: $STAGE"
Endpoint is: testing.com, Stage is: dev
Using env option¶
Taskfile.yml
version: '3'
env: # global/system environment variables for all tasks
HOME: /root
tasks:
msg:
env:
MESSAGE: Hello
cmds:
- 'echo "Message is: $MESSAGE"'
- 'echo "Home directory is $HOME"'