Dotenv Vault
Dotenv Vault allows us to sync secrets between developer's machine and to deployments by storing an encrypted version of the secrets in the repo. This guide will walk you through setting up Dotenv Vault in the repo and deployments.
Prerequisites
Make sure you have access to the Labrys Dotenv account, ask one of the senior devs if you don't and we can invite you.
Set up in Repo
This repo is configured to use a single .env
file at the root of the repo. There should already be a .env.example
and .env.vault
file. This .env.vault
file is linked with the template repo deployment, so we want to remove it and set up a new one.
- Delete the existing
.env.vault
- Create a new
.env
file based on.env.example
- Run
npx dotenv-vault@latest new
and pressy
to open the browser - Give the project a name (match the Github repo name)
- Run
npx dotenv-vault@latest login
and pressy
to log in
Now if you make changes to your .env file you can push with:
npx dotenv-vault@latest push
and can pull with:
npx dotenv-vault@latest push
Managing Multiple Environments
By default the above commands will pull the local
environment. See the Dotenv Docs about managing multiple environments. Create a production
and development
environment before moving on to the next sections.
Deployment on Vercel
Following the Dotenv Docs about deploying to Vercel has caused issues in the past. Try using the following instead.
The Next.JS app is automatically configured to use the DOTENV_KEY
provided by Vercel's environment variables, all you need to do is set it.
Setting the DOTENV_KEY
s
- Run
npx dotenv-vault@latest keys
to get your keys - Open
Settings
->Environment Variables
in your Vercel project - Create a new environment variable with
DOTENV_KEY
as the key and the development key from the earlier command as the value. Set theEnvironments
dropdown to just selectPreview
then hit save - Do the same for the production
DOTENV_KEY
but set theEnvironments
dropdown to justProduction
Manually Setting the Build Command
If you need to manually set the build command you can use:
cd ../../ && npx dotenv-vault@latest decrypt $DOTENV_KEY > .env && cd apps/nextjs && cp ../../.env .env && turbo run build
This command performs the following:
- Move to the root of the repo (where the
.env.vault
file is) - Decrypt the
.env.vault
file into a.env
file - Move back to the
nextjs
directory - Copy the
.env
file from the root to thenextjs
directory (this allows us to use Next's build in.env
file support) - Run the build
This may need to be modified for your specific use case, e.g. if your .env.vault
file is already in the nextjs
directory then you wont need to move around and copy the file, just use the decrypt command:
npx dotenv-vault@latest decrypt $DOTENV_KEY > .env && turbo run build