Skip to content

Environment variables

Ahoy can load variables from .env files before it runs a command, so settings and secrets stay out of your .ahoy.yml.

Create a .env file next to your .ahoy.yml:

Terminal window
DB_USER=root
DB_PASS=secret
DB_NAME=myapp

Point to it in your config:

ahoyapi: v2
env: .env
commands:
db:
usage: Connect to the database
cmd: mysql -u$DB_USER -p$DB_PASS $DB_NAME

The path is relative to .ahoy.yml, not to the directory you’re in.

An env at the top of the file applies to every command. A command can have its own env as well, which is loaded after the top-level one:

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.sh

Either env can be a list. Files load in order, and 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 staging

Files that might not exist v3.0.1+

Section titled “Files that might not exist ”

If an env file is missing, Ahoy still runs the command. From v3.0.1 it also prints a warning, so a typo in a filename doesn’t leave you wondering why a variable is empty:

[warn] environment file '.env.local' not found, continuing without it. Add 'optional: true' to that entry if it is meant to be absent.

Some files are meant to be missing, such as a personal .env.local that only some people have. Mark those optional: true and Ahoy won’t warn about them:

env:
- .env.base # Warns if missing
- path: .env.local
optional: true # Quiet if missing

This works like optional on imports. The warning goes to stderr, so it won’t end up in piped output. ahoy config validate lists a missing required file with ❌ and a missing optional one with ➖.

If the same variable is set in more than one place, the later one wins:

  1. Your shell’s environment
  2. Top-level env files, in order
  3. The command’s own env files, in order

Ahoy adds two variables to every command:

Variable Value
AHOY_COMMAND_NAME The name of the command being run
AHOY_CMD Path to the ahoy binary

Use them in scripts that call other Ahoy commands or need to know their own name.

Add your real .env files to .gitignore, and commit a .env.example with placeholder values instead:

.gitignore
.env
.env.local
.env.secrets
Terminal window
# .env.example - safe to commit
DB_USER=your_username
DB_PASS=your_password
DB_NAME=your_database

The Environment reference has the full file format.