netlify-plugin-env v0.0.7
netlify-plugin-env
This plugin swaps out ENV vars on Netlify at build time. Here's how it works:
Say you have an ENV in your API code called DATABASE_URL
. If you use this plugin, you'll be able to override that value based on a Context or Branch name.
For example:
- A
staging
branch, would automatically setDATABASE_URL
to the value ofSTAGING_DATABASE_URL
if it exists. - A
production
context would automatically setDATABASE_URL
to the value ofPRODUCTION_DATABASE_URL
if it exists. - A
deploy-preview
context (used for Pull Requests) would automatically setDATABASE_URL
to the value ofDEPLOY_PREVIEW_DATABASE_URL
if it exists.
This allows you to have per-environment or per-context environment variables, without exposing those variables in your netlify.toml
config.
If you'd rather use a suffix rather than the default prefix configuration, pass suffix to the inputs below.
For the examples above, it would use the values DATABASE_URL_STAGING
, DATABASE_URL_PRODUCTION
, and DATABASE_URL_DEPLOY_PREVIEW
respectively.
Usage
Add the plugin
Add a [[plugins]]
entry to your netlify.toml
file:
[[plugins]]
package = 'netlify-plugin-env'
[plugins.inputs]
mode = 'prefix'
name | description | default |
---|---|---|
mode | The way to append the context or branch name (prefix or suffix ) | prefix |
Update your build command
Update your build command to "source" the .env
file that gets created for you. Note: your .env
should always be in .gitignore
! This script writes a temporary .env file for you at build time, so updated values can be used during the build process.
You can do this in netlify.toml
:
[build]
command = ". ./.env && yarn build"
Or through the Netlify UI if you don't have a build
section defined in netlify.toml
: