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
You can specify dotnet option in global or task level.
- Taskfile.yml
- .env
- .env
Assume you have the following content:
STAGE=testTaskfile.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.
- but if you set
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: devUsing 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"'Override dotenv variable using env option
.env
ENDPOINT=testing.comTaskfile.yml
version: '3'
tasks:
hello:
dotenv:
- '.env'
env:
ENDPOINT: dev.com # Override the value of ENDPOINT
cmds:
- 'echo "Endpoint is: $ENDPOINT"'demo and output
ubuntu@touted-mite:~$ task hello
task: [hello] echo "Endpoint is: $ENDPOINT"
Endpoint is: dev.comLast updated on