How to set and use environment variables in Node.js service
Environment variables help you run your application in different environments. They allow you to isolate all specific environment aspects from your application code and keep your app encapsulated. You can create several projects in Zerops that represent different environments (development, stage, production) or even each developer can have a project with its own environment.
In Zerops you do not have to create a .env
file manually. Zerops handles the environment variables for you.
Types of env variables in Zerops​
There are 3 different sets of env variables in Zerops:
Type | Environment | Defined in |
---|---|---|
basic | build | zerops.yaml |
basic | runtime | zerops.yaml |
secret | runtime | Zerops GUI |
Use the secret env variables for all sensitive data you don't want to store in your application code. Secret env variables are also useful if you need for testing where you need to change the value of some env variables frequently. Secret variables are managed in Zerops GUI and you don't have to redeploy your application.
The basic build and runtime env variables are listed in your zerops.yaml and deployed together with your application code. When you need to update a value of an existing env variable or change the set of build or runtime env variables, update your zerops.yaml and redeploy your application to Zerops.
You can reference another variable of the same service or even a variable of another service within the same project.
Set secret env variables in Zerops GUI​
Use secret variables to store passwords, tokens and other sensitive information that shouldn't be part of your repository and listed in zerops.yaml.
You can set env variables when you create a new Node.js service or you can set them later.
To configure env variables for an existing service, go to the service detail and choose Environment variables in the left menu. Scroll to the Secret variables section and click on the Add secret variable button and set variable key and value.
You can edit or delete env variables that you've created by clicking on the menu on the right side of each row.
The changes you've made to environment variables will be automatically applied to all containers of your project's services.
You need to restart the runtime service after you update environment variables. The Node.js process running in the container receives the list env variables only when it starts. Update of the env variables while the Node.js process is running does not affect your application.