Set CONTEXT when running Netlify Dev #1685
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
- 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:
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
, andDEV_API_KEY
all in the Netlify Admin UI then my function will correctly get the key value for each environment usingcontextualEnvVar('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 (addingDEV_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 noCONTEXT
is set and I think this just adds parity to Netlify's other environments whereCONTEXT
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
cURL
s 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)