There are two sub-questions:
Should I put secret environment variables in the
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 the 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 and another open API, and there is an environment variable named access_token
,
If I put this in the environment.ts
file and push my front-end source code to Github, Github will detect the secret information and give me a warning, they say:
You should not put the 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 the .gitignore
file or what?
update 2
Here is another case
Continue with update 1. Now, I add docker-compose.yaml
and Dockerfile
to run my front-end app in the docker
container.
Here is the workflow:
Write
Dockerfile
, runnpm run build
command and copy./build
directory tonginx
static file directory ofdocker
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 adocker
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 to 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 are separated for the general situation. So, Front-end should not depend on the 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? Several situations need to be considered:
- Work with PaaS Heroku config vars
- Dockerized(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)