Skip to content
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

Configure context-based env vars #33

Merged
merged 2 commits into from
Jan 23, 2022
Merged

Conversation

kachkaev
Copy link
Collaborator

@kachkaev kachkaev commented Jan 23, 2022

Netlify UI does not support context-based environment variables. For example, we cannot configure XYZ=42 for preview releases (pull requests) and XYZ=4242 for production (main branch).

As a workaround, we read Netlify’s CONTEXT env variable and then map PROD_XYZ / XYZ_PROD or PR_XYZ / XYZ_PR into XYZ.

next.config.js runs before the rest of the app code, so our workaround applies everywhere.

Prior art

@netlify
Copy link

netlify bot commented Jan 23, 2022

✔️ Deploy Preview for dtp-stat-on-nextjs ready!

🔨 Explore the source changes: ef0dc0a

🔍 Inspect the deploy log: https://app.netlify.com/sites/dtp-stat-on-nextjs/deploys/61eddd085c736a000875e8c9

😎 Browse the preview: https://deploy-preview-33--dtp-stat-on-nextjs.netlify.app/

@kachkaev kachkaev linked an issue Jan 23, 2022 that may be closed by this pull request
@kachkaev kachkaev marked this pull request as ready for review January 23, 2022 22:34
kirjs
kirjs previously approved these changes Jan 23, 2022
next.config.mjs Outdated
*/

const suffixOrPrefix =
process.env.CONTEXT === "production"
Copy link

Choose a reason for hiding this comment

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

nit: consider using a hashmap to avoid double ternaries

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if this improves a lot in our specific scenario, but yeah, why not? 🙂 Thanks for your feedback!


if (suffixOrPrefix) {
for (const key in process.env) {
if (key.startsWith(`${suffixOrPrefix}_`)) {
Copy link

Choose a reason for hiding this comment

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

Curious why do we need two ways of doing the same thing?
Can we just agree on prefix?
That would simplify the code, and make the naming more predicatble

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I agree with you that it’s best to support just one option. I’m still not sure which one will work best, so I’d like us to have some initial freedom. Netlify orders env vars alphabetically in their UI and I’m not sure whether prefix or suffix will be easier to use. My intuition says suffix, but netlify-plugin-contextual-env uses prefix.

Once we’ve experimented and established the preferred, we can simplify this code a little.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm good with suffix, let's find agreement on this.

@kachkaev kachkaev merged commit 6c03a1e into main Jan 23, 2022
@kachkaev kachkaev deleted the configure-context-based-env-vars branch January 23, 2022 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Настроить разные переменные для веток
3 participants