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

feat(core): specify a custom .env file for workspace:run-commands #2505

Merged
merged 2 commits into from
Feb 19, 2020

Conversation

rarmatei
Copy link
Collaborator

Expected Behavior (This is the new behavior we can expect after the PR is merged)

By default @nrwl/workspace:run-commands looks for a .env file at the root level, and loads all the variables in there before running the command.

Sometimes, it might be useful to specify different dotenv depending on what environment you're targetting.

With this PR, you can specify a envFile option to run-commands which loads that instead of the root .env:

       "sentry-push-sourcemaps-prod": {
          "builder": "@nrwl/workspace:run-commands",
          "options": {
            "commands": [
              {
                "envFile": "apps/my-app/environments/prod.env",
                "command": "nx run my-app --target=sentry-push-sourcemaps"
              }
            ]
          }
        },

Issue

Closes #2199

@rarmatei rarmatei requested a review from jaysoo February 19, 2020 11:57
@rarmatei rarmatei self-assigned this Feb 19, 2020
} else {
try {
require('dotenv').config();
} catch (e) {}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

since it's a "hidden" default we don't throw if the user doesn't have a root .env file
we do throw however, if they specify explicitly and it doesn't exist

Copy link
Member

Choose a reason for hiding this comment

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

It makes sense. 👍

@jaysoo jaysoo merged commit eee8c18 into nrwl:master Feb 19, 2020
@nelsieborja
Copy link

Would it be possible to add the same .env customization for next builders?

try {
require('dotenv').config();
} catch (e) {}

Some of our old codes are using process.env and we cannot change them to use:
import { environment } from '../environments/environment';

@asciidiego
Copy link

In case somebody wonders why the code from the original question does not work, it's because the envFile option should go, well... inside the options key. See example below:

       "sentry-push-sourcemaps-prod": {
          "builder": "@nrwl/workspace:run-commands",
          "options": {
            "envFile": "apps/my-app/environments/prod.env", // This SHOULD go here
            "commands": [
              {
                "envFile": "apps/my-app/environments/prod.env", // NOT here
                "command": "nx run my-app --target=sentry-push-sourcemaps"
              }
            ]
          }
        },

Took me 20 minutes and some source code browsing to figure it out.

@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature request] Next.js: load environment variables from a .env file in the project folder
4 participants