Skip to content

Set CONTEXT when running Netlify Dev #1685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2021

Conversation

jon-sully
Copy link
Contributor

@jon-sully jon-sully commented Dec 29, 2020

- Summary

In the last few months the addition of Build Plugins (particularly https://github.com/bencao/netlify-plugin-inline-functions-env) combined with the deploy context env var have made it possible for Functions to feel environment-agnostic a la:

const contextualEnvVar = (v) => {
  const currentContext = process.env.CONTEXT
  const formattedContext = currentContext.replace('-', '_').toUpperCase()
  return process.env[`${formattedContext}_${v}`]
}

exports.handler = async _ => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      status: contextualEnvVar('API_KEY')
    })
  }
}

I'll be writing up a post on this soon, but the premise is that I can set PRODUCTION_API_KEY, DEPLOY_PREVIEW_API_KEY, and DEV_API_KEY all in the Netlify Admin UI then my function will correctly get the key value for each environment using contextualEnvVar('API_KEY') as shown above.

The ultimate result is that my Function will run with the correct ENV vars for each environment it's in, I can (securely) set ENV var values via the Netlify Admin UI for all four environments (dev, deploy-preview, branch-preview, and production), and I can easily override the dev key locally by just adding the key to my local .env file (adding DEV_API_KEY in this case).

This is really neat. Using Stripe as the canonical example, this structure would let me have my Stripe Prod and Testing keys configured safely in the Netlify Admin UI and feel good knowing that my local dev, deploy-previews, and production environments would be using the correct keys all the time.

This PR just adds CONTEXT = dev to the process thread when running netlify dev locally. Currently no CONTEXT is set and I think this just adds parity to Netlify's other environments where CONTEXT is set.

- Description for the changelog

Add CONTEXT=dev to environment variables when running netlify dev

- What else

I currently have this working great on a micro testing site, Tenv (test env)! Here are a couple of cURLs to show:

$ curl https://deploy-preview-2--tenv.netlify.app/.netlify/functions/output-env-var

$ curl https://tenv.netlify.app/.netlify/functions/output-env-var

The site can be inspected as needed too. tenv.netlify.app

I believe this addition / workflow would also satisfy a few different community requests.

- A picture of a cute animal (not mandatory but encouraged)

image

@jon-sully jon-sully requested a review from a team as a code owner December 29, 2020 10:43
@erezrokah erezrokah added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Jan 4, 2021
@jon-sully
Copy link
Contributor Author

I'm not sure why some of the CI builds failed 😕 A couple of the Windows builds on the first pass and now one of the ubuntu builds

@erezrokah
Copy link
Contributor

Thanks @jon-sully, the CI failure seems unrelated. This is also related to #473 (comment).
Please bare with us while we discuss this internally to figure out what are the best built in env variables the CLI should inject.

@erezrokah erezrokah force-pushed the add-context-env-var branch from 1b3de0b to ed468c4 Compare January 12, 2021 10:25
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jon-sully this is good to go and sorry for the delay in approving it.

@erezrokah erezrokah merged commit b7bc393 into netlify:master Jan 12, 2021
@jon-sully
Copy link
Contributor Author

Yay! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants