Skip to content
Latchkey

Task: Variables, env, and Templating

Task variables are declared under vars and interpolated with Go template syntax like {{.NAME}}.

Task uses Go templates for interpolation. Variables can be defined globally or per task, set from shell output, or pulled from the environment.

What it does

The vars map defines variables referenced as {{.NAME}} in cmds. The env map sets environment variables for a task. A var can take its value from sh: to capture command output. The built-in {{.CLI_ARGS}} holds everything passed after a -- separator on the command line. Task can also load a .env file via the dotenv key.

Common usage

Taskfile.yml
# Taskfile.yml
version: '3'

vars:
  BINARY: app
  COMMIT:
    sh: git rev-parse --short HEAD

env:
  CGO_ENABLED: '0'

dotenv: ['.env']

tasks:
  build:
    cmds:
      - go build -ldflags "-X main.commit={{.COMMIT}}" -o {{.BINARY}} ./...

  test:
    cmds:
      - go test {{.CLI_ARGS}} ./...
# task test -- -run TestFoo -v

Syntax

Key / formWhat it does
vars:Map of variables for templating
{{.NAME}}Interpolate a variable (Go template)
sh: <command>Set a var from command output
env:Environment variables for the task
dotenv: ['.env']Load variables from a .env file
{{.CLI_ARGS}}Arguments passed after -- on the command line

In CI

Use {{.CLI_ARGS}} to forward matrix or filter flags from a pipeline step into a task without editing the Taskfile. Override a variable per run with task build BINARY=server, which CI uses to inject build numbers or targets.

Common errors in CI

A blank value where you expected a variable usually means a typo in {{.NAME}} (Go templates do not error on an undefined key by default, they render empty). A sh: var fails the task if its command exits non-zero. "template: :1: function ... not defined" means an unsupported template function. Forgetting the -- separator means flags are parsed by task itself, not passed through as CLI_ARGS.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →