Environment Variables
Ahoy can load environment variables from files before running your commands, keeping configuration and secrets out of your YAML.
A simple example v2.3.0+
Section titled “A simple example ”Create a .env file alongside your .ahoy.yml:
DB_USER=rootDB_PASS=secretDB_NAME=myappReference it in your config:
ahoyapi: v2
env: .env
commands: db: usage: Connect to the database cmd: mysql -u$DB_USER -p$DB_PASS $DB_NAMEThe path is relative to the .ahoy.yml file, not your working directory.
Global and per-command files
Section titled “Global and per-command files”The top-level env applies to every command. Individual commands can also specify their own env, which layers on top:
ahoyapi: v2
env: .env # Loaded for every command
commands: deploy: usage: Deploy the application env: .env.deploy # Also loaded for this command only cmd: ./deploy.shMultiple files v2.5.0+
Section titled “Multiple files ”Both global and per-command env fields accept an array. Files are loaded in order - later files override earlier ones:
ahoyapi: v2
env: - .env.base - .env.local # Overrides .env.base
commands: deploy: env: - .env.deploy - .env.staging cmd: ./deploy.sh stagingA file like .env.local is often absent, for example when you start a project
from a checked-out repo with an .ahoy.yml file in it. From v3.0.1 Ahoy
reports this, rather than passing over it in silence, so please start marking such entries
as optional - see below.
Files that may be absent v3.0.1+
Section titled “Files that may be absent ”A missing env file never stops a command - it is skipped, the variables it would have set stay empty, and the command runs. What changed in v3.0.1 is that Ahoy now shows a warning:
[warn] environment file '.env.local' not found, continuing without it. Add 'optional: true' to that entry if it is meant to be absent.Every entry is required by default, so a typo in a filename surfaces instead of
leaving you to work out why a variable is empty. For the files that are meant
to be missing - a personal .env.local that only some developers have - write
the entry as a mapping and mark it optional: true:
env: - .env.base # Required: warns when absent - path: .env.local optional: true # Silent when absentThis is the same optional you may already use on
imports: simply meaning that the file is not expected to
always present.
The warning goes to stderr, so it never contaminates a command’s stdout when
you pipe it. ahoy config validate distinguishes the two as well, listing a
missing required file with ❌ and a missing optional one with ➖ ... - missing, marked optional.
Precedence
Section titled “Precedence”From lowest to highest:
- Your shell’s existing environment
- Global env files (in order)
- Command-specific env files (in order)
Runtime variables v3
Section titled “Runtime variables ”Ahoy automatically injects two variables into every command:
| Variable | Value |
|---|---|
AHOY_COMMAND_NAME |
The name of the command being run |
AHOY_CMD |
Path to the ahoy binary |
These are useful for scripts that call other ahoy commands, or need to know their own name.
Keep secrets out of version control
Section titled “Keep secrets out of version control”Add your real .env files to .gitignore and commit a .env.example with placeholder values:
.env.env.local.env.secrets# .env.example - safe to commitDB_USER=your_usernameDB_PASS=your_passwordDB_NAME=your_databaseFor the complete format specification and precedence details, see the Environment Reference.