Environment variables
Ahoy can load variables from .env files before it runs a command, so settings and secrets stay out of your .ahoy.yml.
A simple example v2.3.0+
Section titled “A simple example ”Create a .env file next to your .ahoy.yml:
DB_USER=rootDB_PASS=secretDB_NAME=myappPoint 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_NAMEThe path is relative to .ahoy.yml, not to the directory you’re in.
For every command, or just one
Section titled “For every command, or just one”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.shMultiple files v2.5.0+
Section titled “Multiple files ”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 stagingFiles 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 missingThis 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 ➖.
Which value wins
Section titled “Which value wins”If the same variable is set in more than one place, the later one wins:
- Your shell’s environment
- Top-level env files, in order
- The command’s own env files, in order
Variables Ahoy sets v3
Section titled “Variables Ahoy sets ”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.
Keep secrets out of git
Section titled “Keep secrets out of git”Add your real .env files to .gitignore, and commit a .env.example with placeholder values instead:
.env.env.local.env.secrets# .env.example - safe to commitDB_USER=your_usernameDB_PASS=your_passwordDB_NAME=your_databaseThe Environment reference has the full file format.