Skip to content

Commit

Permalink
Update heroku-nodejs guide
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Jul 6, 2023
1 parent ce0c016 commit fca736f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 67 deletions.
7 changes: 4 additions & 3 deletions _includes/landing/hero.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<div class="container px-lg-5">
<div class="row text-center">
<div class="col">
<h1 class="display-1 fw-bold mt-5 lh-1">Simplify Your Secrets</h1>
<h1 class="display-2 fw-extrabold mt-5 lh-1">Secrets manager for .env files</h1>

<div class="row">
<div class="col-lg-8 offset-lg-2">
<p class="h2 fw-light mt-1">Manage your secrets using <span class="fw-bold">dotenv-vault</span>'s all-in-one toolkit. Say goodbye to scattered secrets across multiple platforms and tools.</p>
<div class="col-lg-10 offset-lg-1">
<p class="h4 fw-light mt-2">Sync your <span class="fw-light">.env</span> files with a single command, deploy them with an encrypted <span class="fw-semibold">.env.vault</span> file, and say goodbye to scattered secrets across multiple platforms and tools.</p>
<!--<p class="h2 fw-light mt-1">Manage your secrets using <span class="fw-bold">dotenv-vault</span>'s all-in-one toolkit. Say goodbye to scattered secrets across multiple platforms and tools.</p>-->
<!--<p class="h2 fw-light mt-3">Add syncing and encryption to your .env files and say goodbye to scattered secrets across multiple platforms and tools.</p>-->
<!---<p class="h2 fw-light mt-3">Combat scattered secrets with a single source of truth. <span class="fw-bold">dotenv-vault</span> builds on top of the proven <span class="fw-bold">dotenv</span> standard, adding syncing and encryption to .env files.</p>-->
</div>
Expand Down
Binary file added assets/img/docs/edit-production-value.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
layout: docs
section: Overview
title: "Introduction"
description: In these pages you'll find tutorials that will teach you how to use dotenv-vault, and reference documentation for all the moving parts.
description: The secrets manager for .env files – from the same people that pioneered dotenv.
---

**dotenv-vault** is a secrets manager built on top of dotenv. It lets you sync and deploy your .env files across machines, teams, and environments – simply and securely.
Sync your `.env` files with a single command, deploy them with an encrypted `.env.vault` file, and say goodbye to scattered secrets across multiple platforms and tools.

If you already use .env files you can benefit from dotenv-vault in just a few seconds of your time. Try it right now.
If you already use .env files you can benefit from dotenv-vault in just a few seconds of your time. It works with a single command: **npx dotenv-vault@latest push**.

<div class="row row-cols-2 g-2">
<div class="col-lg-12">
Expand Down
153 changes: 94 additions & 59 deletions docs/integrations/heroku/nodejs.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
---
layout: docs
title: "Heroku with Node.js - Integrations"
section: "Integration Guides"
title: "Heroku with NodeJS"
description: Deploy a NodeJS app to Heroku. Use an encrypted .env.vault file to secure and deploy your secrets to Heroku.
---

{% include icons/heroku.html width="50" color="#430098" %}
{% include icons/nodejs.html width="50" color="#339933" %}
<div class="alert alert-info">ⓘ This guide assumes you are already familiar with <a href="https://github.com/motdotla/dotenv">dotenv</a> and have <a href="/docs/tutorials/sync">synced your secrets</a> with <a href="https://github.com/dotenv-org/dotenv-vault">dotenv-vault</a>.</div>

# __Heroku with Node.js__

Learn how to make Heroku, Node.js, and Dotenv Vault work together in a simple web app. This tutorial assumes you are already familiar with `.env` files and know [how to sync them](/docs/tutorials/sync).

You can find a complete [example repo here](https://github.com/dotenv-org/integration-example-heroku-nodejs).
You can find a complete [example here](https://github.com/dotenv-org/examples/tree/master/heroku-nodejs).

## Initial setup
Create a `Procfile` in the `root` folder to set your Heroku project settings. Add the `web` key, followed by the start command of your Node.js application as value.

##### Yaml
Add a `Procfile` to run your NodeJS app.

##### Procfile
```yaml
// Procfile
web: npm run build
web: node index.js
```
[Example](https://github.com/dotenv-org/integration-example-heroku-nodejs/blob/master/Procfile)
[Example](https://github.com/dotenv-org/examples/blob/master/heroku-nodejs/Procfile)
Create an `index.js` file, if you haven't done so already. Process the environment variables in it and proceed with a standard Node.js `http-server` setup. Reference the module, indicate the port, and add some dynamic HTML with an environment variable to confirm it works beyond local.
Your `index.js` file should look something like this.

##### Node.js
##### index.js
```js
// index.js
const PORT = process.env.PORT || 5000
const PORT = process.env.PORT || 3000
const http = require('http')
const server = http.createServer((req, res) => {
res.statusCode = 200;
Expand All @@ -39,9 +36,9 @@ server.listen(PORT, () => {
console.log(`Server running on port:${PORT}/`);
});
```
[Example](https://github.com/dotenv-org/integration-example-heroku-nodejs/blob/master/index.js)
[Example](https://github.com/dotenv-org/examples/blob/master/heroku-nodejs/index.js)

Remember to set an event listener running on the same port so your app knows when to serve its visitors. Commit that to code and push it to Heroku.
Commit that to code and push it to Heroku.

##### CLI

Expand All @@ -50,88 +47,126 @@ heroku create
git push heroku
```

Once it is deployed, your app will say `'Hello undefined'` as it doesn't have a way to access the environment variable from the HTML yet. That is why the next step for you to take is to connect them dynamically.
Once deployed, your app will say `'Hello undefined'` as it doesn't have a way to access the environment variable yet. Let's do that next.

## Package installation
Start by installing the [`dotenv`](https://github.com/motdotla/dotenv) package with `npm`.
## Install dotenv

Install [`dotenv`](https://github.com/motdotla/dotenv).

##### CLI
```shell
npm install dotenv --save
```

Reference the Vault package as early as possible in your `index.js` code to prevent possible conflicts.
Create a `.env` file in the root of your project.

##### .env
```shell
# .env
HELLO="World"
```

As early as possible in your application, import and configure dotenv.

##### Node.js
##### index.js
```js
// index.js
require('dotenv').config()
console.log(process.env) // for debugging purposes. remove when ready.
```
[Example](https://github.com/dotenv-org/integration-example-heroku-nodejs/blob/master/index.js)

## Vault setup
Open your Vault project and insert the `HELLO` secret with value of your choice under `development` for local testing. For this tutorial it is `"user, your local test worked perfectly"` to complete the static text in the HTML. Once you are ready and confirmed you're logged in, sync your Dotenv Vault locally with `npx dotenv-vault pull`. Then, run locally for testing.
console.log(process.env) // remove this after you've confirmed it is working

#### Shell
```shell
# .env
HELLO="user, your local test worked perfectly."
const PORT = process.env.PORT || 3000
const http = require('http')
...
```
[Example](https://github.com/dotenv-org/examples/blob/master/heroku-nodejs/index.js#L2)

Try running it locally.

#### Node.js
```Java
##### CLI
```shell
node index.js
{
HELLO: 'user, your local test worked perfectly.'
...
HELLO: 'World'
}
Running on port 5000
Server running on port:3000/
```

If you've set everything correctly, you will be faced with the message `"Hello user, your local test worked perfectly"` at [http://localhost:5000](http://localhost:5000).
Perfect. `process.env` now has the keys and values you defined in your `.env` file.

That covers local development. Let's solve for Heroku production next.

## Build the Vault
Now that the local test is completed successfully, it is time for you to set a production value for when you deploy. Following the previous fashion, it is set to `HELLO="user, your production test worked perfectly."` Run `npx dotenv-vault open production` so you can start editing production values with the Vault interface.
## Build .env.vault

#### CLI
```Java
npx dotenv-vault open production
Push your latest `.env` file changes and edit your production secrets. [Learn more](/docs/tutorials/sync)

##### CLI
```shell
npx dotenv-vault@latest push
npx dotenv-vault@latest open production
```

{% include helpers/screenshot.html url="/assets/img/cloudinary/dotenv_vault_environment_variable_interface_production_nh0bop.png" %}
Use the UI to configure those secrets per environment.

##### UI
{% include helpers/screenshot.html url="/assets/img/docs/edit-production-value.gif" %}

Then build your encrypted `.env.vault` file.

##### CLI
```shell
npx dotenv-vault@latest build
```

When you are done tinkering, pull the latest Vault version and build your encrypted local `.env.vault` file by running `npx dotenv-vault build`. Commit your `.env.vault` file to code without stress knowing it is both safe and necessary to do so, unlike `.env` files.
Its contents should look something like this.

#### CLI
##### .env.vault
```shell
npx dotenv-vault build
#/-------------------.env.vault---------------------/
#/ cloud-agnostic vaulting standard /
#/ [how it works](https://dotenv.org/env-vault) /
#/--------------------------------------------------/

# development
DOTENV_VAULT_DEVELOPMENT="/HqNgQWsf6Oh6XB9pI/CGkdgCe6d4/vWZHgP50RRoDTzkzPQk/xOaQs="
DOTENV_VAULT_DEVELOPMENT_VERSION=2

# production
DOTENV_VAULT_PRODUCTION="x26PuIKQ/xZ5eKrYomKngM+dO/9v1vxhwslE/zjHdg3l+H6q6PheB5GVDVIbZg=="
DOTENV_VAULT_PRODUCTION_VERSION=2
```

## Set deployment
## Set DOTENV_KEY

There is one last step to complete before you are ready - you must set the decryption `DOTENV_KEY` on Heroku. To do that, first fetch your Vault production key by running `npx dotenv-vault keys production`.
Fetch your production `DOTENV_KEY`.

#### CLI
##### CLI
```shell
npx dotenv-vault keys production
remote: Listing .env.vault decryption keys... done
npx dotenv-vault@latest keys production
# outputs: dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=production
```

dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=production
Set `DOTENV_KEY` on Heroku using the CLI.

##### CLI
```shell
heroku config:set DOTENV_KEY=dotenv://:key_1234…@dotenv.org/vault/.env.vault?environment=production
```

Copy over the key and jump to your Heroku project. Hop on to the Settings panel and then set `DOTENV_KEY` as key and your decryption key as value `dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=production` as value.
Or use Heroku's UI.

##### UI
{% include helpers/screenshot.html url="/assets/img/cloudinary/dotenv_vault_heroku_environment_variable_settings_sk6fkj.png" %}

## Commit and push

That's it!

Commit those changes safely to code and deploy to Heroku.
Commit those changes safely to code and deploy.

When the build runs, it will recognize the `DOTENV_KEY`, decrypt the .env.vault file, and load the `production` environment variables to `ENV`. If a `DOTENV_KEY` is not set when developing on local machine, for example, it will fall back to standard Dotenv functionality.
That's it! On deploy, your `.env.vault` file will be decrypted and its production secrets injected as environment variables – just in time.

You'll know things worked correctly when you see `'Loading .env from encrypted .env.vault'` in your Heroku logs.
You'll know things worked correctly when you see `'Loading env from encrypted .env.vault'` in your logs.

{% include helpers/screenshot.html url="/assets/img/cloudinary/dotenv_vault_heroku_logs_encrypted_loading_env_vault_qbwich.png" %}

Additional Note: If a `DOTENV_KEY` is not set (for example when developing on your local machine) it will fall back to standard [dotenv](https://github.com/motdotla/dotenv) functionality.

2 changes: 1 addition & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: docs
section: Overview
title: "Quickstart ⚡️"
description: The complete quickstart guide to manage your secrets with dotenv-vault. Learn and understand how to use dotenv-vault with your application – in about 5 minutes.
description: Manage your secrets with dotenv-vault. Learn and understand how to use dotenv-vault with your application – in about 5 minutes.
---

In this guide, we'll deploy an application with secrets to Heroku. We'll create the app, load and sync its secrets, and deploy it using the .env.vault file. A quick skim over this tutorial, and you'll understand all the foundational concepts of using dotenv-vault.
Expand Down
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: default
title: "Simplify Your Secrets"
title: "The secrets manager for .env files"
redirect_from:
- /why
---
Expand Down

0 comments on commit fca736f

Please sign in to comment.