Giter Site home page Giter Site logo

netlify-plugin-contextual-env's Introduction

netlify-plugin-contextual-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 set DATABASE_URL to the value of STAGING_DATABASE_URL if it exists.
  • A production context would automatically set DATABASE_URL to the value of PRODUCTION_DATABASE_URL if it exists.
  • A deploy-preview context (used for Pull Requests) would automatically set DATABASE_URL to the value of DEPLOY_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.

image

Usage

Add the plugin

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

[[plugins]]
package = 'netlify-plugin-contextual-env'
  [plugins.inputs]
  mode = 'prefix'
name description default
mode The way to append the context or branch name (prefix or suffix) prefix

netlify-plugin-contextual-env's People

Contributors

bencao avatar cball avatar dependabot[bot] avatar ehmicky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

netlify-plugin-contextual-env's Issues

Different env var value for different deploy previews

Hi!,
I'm just starting to use netlify and found your plugin while trying to setup different env vars for different environments, great work btw ๐Ÿ‘

One thing I wanted to do is having an env var, which we are using to set the backend url, that would be used only by one deploy preview, so then I could point different deploy previews to different backends (because the backend is deployed from a pull request too).

What I got from reading this plugin's code and trying out some things is that the DEPLOY_PREVIEW prefix sets the env var for all deploy previews, right?

I thought that using the branch prefix would make it possible and then ran into the problem that the BRANCH env var on deploy previews is a reference to the PR, not to the branch itself, so in my case it looks something like pull/2/head and / isn't an allowed character in netlify's UI to set nev vars.

Is there any other way I'm missing? (without using branch deploys instead of deploy previews)

I guess one way to support this would be to have the name of the branch transformed to replace / by _ the same way -s are being replaced before replacing the env vars. What do you think?

Fail to publish AWS Lambda

Your environment variables exceed the 4KB limit imposed by AWS Lambda. Please consider reducing them.
When i started to use this plugin i got this error, it could be interesting to remove the old var from them env after the transfer

Folder-style branches?

We currently have a concept of namespaces in our branch names such as members/ec, but I can't seem to get the env variable since it the replace in the plugin only replaces -, but I'm wondering if we'd be open to extending it to allow for replacing / into _? Thoughts?

NPM Example error

The NPM page for this shows the wrong example code. It has netlify-plugin-env instead of netlify-plugin-contextual-env (which found in this repo). This causes errors when building the site.

image

console.log exposes secrets

In the build log, the plugin logs the replaced variables. It should not display the values here, as these are sometimes secrets.

Environment variables do not currently work

This Build plugin does not completely work due to a missing feature in Netlify Build tracked here: process.env is not currently shared between Build plugins and build.command.

However the .env file logic is not impacted by this.

Question: envs used in functions runtime vs build

I have a question about

https://github.com/cball/netlify-plugin-contextual-env/blob/master/src/index.js#L18

I was using your plugin as a guide to do something similar for a env that isn't used as part of the build runtime but rather when a function executes.

process.env[inputs.netlify_env] = "some value"

While I know that the env (it is DATABASE_URL) is set during build seen in the preBuild plugin:

2:22:10 PM: DATABASE_URL ends in tdjhw

I'm not seeing it that a function can find it when executes:

error: Environment variable not found: DATABASE_URL.

Is that expected, or might it be how that function fetches the env (note: it's Prisma when creating the client).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.