Nx: [Feature Request] Using environment variables with accessToken (distributed caching)

Created on 19 Apr 2020  路  4Comments  路  Source: nrwl/nx

I'm trying to use cloud distributed caching in my project. It works fine, but there is an issue with accessToken which is placed inside nx.json. As it is a JSON, I can't set dynamic key using ENV - one for CI and one (read-only) for local use. Unfortunately I checked the DC and CLI docs and there is no alternative way (parameter) to set accessToken.

I know I can do a workaround - using fileReplacements in workspace.json/angular.json. But having a copy of this file and specifying --configuration flag just for one token is not the best approach.

Expected Behavior

To be able to use ENV to set accessToken dynamically.

Current Behavior

No such possibility.

Context

Angular CLI: 9.1.0
Node: 12.14.1
OS: win32 x64

Angular: 9.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.0
@angular-devkit/build-angular     0.901.0
@angular-devkit/build-optimizer   0.901.0
@angular-devkit/build-webpack     0.901.0
@angular-devkit/core              9.1.0
@angular-devkit/schematics        9.1.0
@ngtools/webpack                  9.1.0
@schematics/angular               9.1.0
@schematics/update                0.901.0
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0

Other

Maybe I'm missing something, because why would you give a possibility of creating multiple access tokens, but no possibility to use more than one?

misc question / discussion

Most helpful comment

@FrozenPandaz Thanks for pointing a solution for that. But how can I use dotenv with that? I have multiple projects and I would prefer setting .env file locally instead of real user/system environments. I have found some alternative, but I'm already using dotenv with some worker scritps inside project, just not sure how to inject the variables inside nx CLI script.

All 4 comments

You could update the nx.json via a script https://medium.com/@ferie/how-to-pass-environment-variables-at-building-time-in-an-angular-application-using-env-files-4ae1a80383c

The environment variable to set the Nx Cloud access token is NX_CLOUD_AUTH_TOKEN=(your-token-here).

If you sign into https://nx.app and go to your workspace, you can click the Workspace Options button on the top right:

image

There, there is documentation for different ways to setup your access token:

image

@FrozenPandaz Thanks for pointing a solution for that. But how can I use dotenv with that? I have multiple projects and I would prefer setting .env file locally instead of real user/system environments. I have found some alternative, but I'm already using dotenv with some worker scritps inside project, just not sure how to inject the variables inside nx CLI script.

Was this page helpful?
0 / 5 - 0 ratings