# @netlify-tools/netlify-plugin-env

> A Netlify build plugin to set ENV variables based on the context

Latest version **0.1.3** (published 2020-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @netlify-tools/netlify-plugin-env
pnpm add @netlify-tools/netlify-plugin-env
yarn add @netlify-tools/netlify-plugin-env
bun add @netlify-tools/netlify-plugin-env
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2020-09-15 |
| First published | 2020-09-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 32.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jorge Suárez |
| Maintainers | jorgesuarezch |
| Keywords | netlify, netlify-plugin, env, netlify-deploy-context |

## Links

- npm: https://www.npmjs.com/package/@netlify-tools/netlify-plugin-env
- Repository: https://github.com/jorgesuarezch/netlify-tools
- Homepage: https://github.com/jorgesuarezch/netlify-tools#readme
- Issues: https://github.com/jorgesuarezch/netlify-tools/issues
- npm.io page: https://npm.io/package/@netlify-tools/netlify-plugin-env

## Dependencies (3)

- [chalk](https://npm.io/package/chalk.md) ^4.1.0
- [@netlify-tools/core](https://npm.io/package/@netlify-tools/core.md) ^0.1.2
- [webpack-node-externals](https://npm.io/package/webpack-node-externals.md) ^2.5.2

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 0.1.3 (latest) — 2020-09-15
- 0.1.2 — 2020-09-15
- 0.1.1 — 2020-09-15
- 0.1.0 — 2020-09-15

## README

# Netlify Plugin Env

[![Version](https://img.shields.io/npm/v/@netlify-tools/netlify-plugin-env.svg)](https://npmjs.org/package/@netlify-tools/netlify-plugin-env)
[![License](https://img.shields.io/npm/l/@netlify-tools/netlify-plugin-env.svg)](https://github.com/jorgesuarezch/netlify-tools/blob/master/packages/netlify-plugin-env/package.json)

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](https://docs.netlify.com/site-deploys/overview/#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`](/packages/cli/README.md) 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:

```bash
AUTH_TOKEN=supersecret
AUTH_TOKEN_STAGING=secret@
```

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

```js
AUTH_SECRET === AUTH_TOKEN_STAGING === secret@
```

The next table show a few examples

| Deploy Context | Branch Deploy? | Variable                            | Notes                                                                                                                                        |
| -------------- | -------------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| production     | ---            | AUTH_TOKEN`_PRODUCTION`             | predefined deploy context                                                                                                                    |
| deploy-preview | ---            | AUTH_TOKEN`_DEPLOY_PREVIEW`         | predefined deploy context                                                                                                                    |
| branch-deploy  | ---            | AUTH_TOKEN`_BRANCH_DEPLOY`          | predefined deploy context                                                                                                                    |
| ---            | staging        | AUTH_TOKEN`_STAGING`                |                                                                                                                                              |
| ---            | feature/login  | AUTH_TOKEN`_FEATURE_LOGIN`          |                                                                                                                                              |
| deploy-preview | staging        | AUTH_TOKEN`_DEPLOY_PREVIEW_STAGING` | allow 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

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

Install it via yarn

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

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

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

---
_Source: https://npm.io/package/@netlify-tools/netlify-plugin-env · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
