0.1.3 • Published 4 years ago

@netlify-tools/netlify-plugin-env v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Netlify Plugin Env

Version License

This plugin helps to have your env variables separated by deploy context in a seamless way.

Why

It is pretty common to have multiple environments to test apps rigorously before promoting changes to production. This way, the app can be delivered with more confidence.

Netlify provides a great mechanism called deploy contexts. This mechanism allow to configure as many custom deploy contexts as you want, all you need is to add a new branch to your configuration. For instance, a branch called staging will match a deploy context called staging

That's great, isn't it? However there is a little limitation for configuring environment variables per deploy context. The only way to override an env variable for a context is through netlify.toml, which means code needs to be changed and sensitive information could be exposed.

So, this plugin aims to fix that.

Note @netlify-tools/cli can help with setting env variables by context

How it works

In order for this plugin to work properly env variables names need to have the format: [VARIABLE]_[DEPLOY-CONTEXT], so the plugin will check the process.env and will set for [VARIABLE] the value of [VARIABLE]_[DEPLOY-CONTEXT] if the current deploy context and [DEPLOY-CONTEXT] matches.

For instance:

AUTH_TOKEN=supersecret
AUTH_TOKEN_STAGING=secret@

the plugin will override the AUTH_TOKEN value with the one for AUTH_TOKEN_STAGING.

AUTH_SECRET === AUTH_TOKEN_STAGING === secret@

The next table show a few examples

Deploy ContextBranch Deploy?VariableNotes
production---AUTH_TOKEN_PRODUCTIONpredefined deploy context
deploy-preview---AUTH_TOKEN_DEPLOY_PREVIEWpredefined deploy context
branch-deploy---AUTH_TOKEN_BRANCH_DEPLOYpredefined deploy context
---stagingAUTH_TOKEN_STAGING
---feature/loginAUTH_TOKEN_FEATURE_LOGIN
deploy-previewstagingAUTH_TOKEN_DEPLOY_PREVIEW_STAGINGallow combination between predefined deploy context and branch deploys. This is great for preview having different configuration per preview

Usage

Add the plugin

Install it via npm

npm install --save-dev @netlify-tools/netlify-plugin-env

Install it via yarn

yarn add --dev @netlify-tools/netlify-plugin-env

Add a [[plugins]] entry to your netlify.toml file:

[[plugins]]
package = '@netlify-tools/netlify-plugin-env'