Skip to content

Use environment variables in the "generates" section of a Taskfile #1545

Answered by pd93
drorata asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @drorata. The problem you're having is because environment variables are only available inside shell environments (i.e. the commands in your task). If you want to use variables in other parts of your task, you need to use the templating engine:

version: "3"

env:
  OUTPUT_FILENAME: output.txt

tasks:
  default:
    cmds:
      - rm -f $OUTPUT_FILENAME
      - cp input.txt $OUTPUT_FILENAME
      - echo "That was it" >> $OUTPUT_FILENAME
    sources:
      - input.txt
    generates:
      - "{{.OUTPUT_FILENAME}}" # <-- here we use templating syntax to output the variable

Sidenote:

The above solution works because all env variables are currently made available to the templating engine as …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by drorata
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants