There are two sub-questions:
Should I put secret environment variables in environment.ts file?
The
process
variable shim is gone. If I use it, tsc will throw an error:Cannot find name 'process'
.
Here is my thing:
About Q1: I don't think put secret environment variables in environment.ts file is correct. Because these files will be a push to source code management, like GitHub, gitlab, bitbucket. It's not safe. So I think secret environment variables should be passed through process.env
, like process.env.ACCESS_TOKEN
, or, if use docker-compose, should put the secret environment variables in .env
file and add this file to .gitignore
file.
About Q2: If I use Heroku to set up my environment variables, it depends on process
variable. Now, angular6 get rid of the shim of process
, How can I work with Heroku? Also, using docker-compose pass environment variables through .env
file depends on process
too.
And if use .env
file for docker-compose, there is a new question come out: How to pass variables in .env file to angular6 environment.ts file
update 1:
Here is a case:
First, there is no back-end
I use github api or other open api, and there is environment variable named access_token
,
If I put this in environment.ts
file and push my front-end source code to Github, Github will detect the secret information and give me an warning, they say, You should not put github access token in your source code and push it to repo, so they will revoke my access token.
So I want to use process.env.ACCESS_TOKEN
, but the process
variable shim is gone in Angular6
, how can I solve this? Should I add environment.ts
file to .gitignore
file or what?
update 2
Here is another case:
continue with update 1. Now, I add docker-compose and Dockerfile to run my front-end app in docker container. Here is the workflow:
Write Dockerfile, run
npm run build
command and copy./build
directory to nginx static file directory of docker container. the./build
directory containsindex.html
,bundle.js
file and so on.put
access_token
and other secret environment variables into.env
file.run
docker-compose up
to run my app in docker container.
I think this workflow is solid. No need back-end service, the secret environment variables in .env
and .gitignore
contains .env
file, so it will not be pushed to Github repo.
But, the key point is process
shim is gone. I can't get environment variables through process
.
update 3
I think my question focus on front-end app development phase. I continue use above case to explain.
For production ready, the workflow is:
Make a back-end service for github oauth, when the oauth workflow is done.Back-end service send
access_token
to front-end.front-end login successfully, get the
access_token
from back-end service and store it in localStorage or cookie. Don't need getaccess_token
fromprocess.env
But for development phase, Front-end and back-end development is separated for general situation. So, Front-end should not depend on back-end service.
And I don't want to build the whole big system for the beginning.
So I think the question is:
Where to store secret environment variables and how to get within Angular6
front-end application code? There are several situation need consider:
- Work with PaaS Heroku config vars
- dockerlize(docker-compose, Dockerfile),
.env
file. - Without back-end service.
- add the environment variables file to
.gitignore
, don't push to SCM(Github, gitlab and so on)