Variables
Defining Variables
Section titled “Defining Variables”name = "Jake";version = "1.0.0";build_dir = "dist";Using Variables
Section titled “Using Variables”Use {{variable}} syntax in commands:
greeting = "Hello"target = "World"
task greet: echo "{{greeting}}, {{target}}!"Output: Hello, World!
Variable Scope
Section titled “Variable Scope”Variables are global and available to all recipes:
project = "myapp"
task build: echo "Building {{project}}"
task test: echo "Testing {{project}}"Variable Precedence
Section titled “Variable Precedence”When the same variable name is defined in multiple places, the following precedence applies (highest to lowest):
- Recipe parameters (passed via CLI:
jake build env=prod) - Environment variables (loaded via
@dotenvor from shell) - Jakefile variables (defined with
=assignment)
This means @dotenv should appear before variable assignments to allow .env files to override defaults:
@dotenv # Load .env firstPORT = "3000" # Default value (overridden by .env if PORT is set there)
task serve: echo "Running on port {{PORT}}"With .env containing PORT=8080, the task outputs: Running on port 8080
Without .env or if PORT is not set, it uses the default: Running on port 3000
Environment Variables
Section titled “Environment Variables”Loading .env Files
Section titled “Loading .env Files”@dotenv # Load .env@dotenv ".env.local" # Load specific fileFiles are loaded in order; later files override earlier ones.
Exporting Variables
Section titled “Exporting Variables”@export NODE_ENV=production@export DEBUG=falseExported variables are passed to all subprocess commands.
Using in Commands
Section titled “Using in Commands”Use $VAR or ${VAR} syntax:
task show: echo "Node: $NODE_ENV" echo "Debug: ${DEBUG}".env File Format
Section titled “.env File Format”# Database settingsDATABASE_URL=postgres://localhost/myappDB_POOL_SIZE=10
# API Keys (use quotes for special chars)API_KEY="abc123!@#"
# Empty valuesEMPTY_VAR=
# Escape sequencesMULTILINE=line1\nline2WINDOWS_PATH=C:\\Users\\NameSupported escapes: \n, \t, \r, \\, \", \', \$
Requiring Variables
Section titled “Requiring Variables”Validate required environment variables:
@require API_KEY DATABASE_URL
task deploy: echo "Deploying with $API_KEY"Jake exits with an error if any required variable is missing.