Skip to content

Commit ca347a1

Browse files
committed
Heroku instructions #211
1 parent 870233c commit ca347a1

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ We've used this `builderbook` project to build [saas](https://github.com/async-l
2323
- [Add a new book](#add-a-new-book)
2424
- [Add your own styles](#add-your-own-styles)
2525
- [Deploy](#deploy)
26+
- [Deploy to Heroku](#deploy-to-heroku)
2627
- [Scaling](#scaling)
2728
- [Screenshots](#screenshots)
2829
- [Built with](#built-with)
@@ -236,6 +237,9 @@ We also specified styles for all content inside a `<body>` element:
236237

237238

238239
## Deploy
240+
241+
IMPORTANT: Now v1 is depreciated for new users. See the next section about deploying to Heroku.
242+
239243
- Install now: `npm install -g now`.
240244
- Point your domain to Zeit world nameservers: [three steps](https://zeit.co/world#get-started).
241245
- Create `now.json` file. Make sure to add actual values for `GA_TRACKING_ID`, `StripePublishableKey` (production-level) and `alias`. Read more about how to [configure now](https://zeit.co/docs/features/configuration).
@@ -263,6 +267,120 @@ We also specified styles for all content inside a `<body>` element:
263267
- Now outputs your deployment's URL, for example: `builderbook-zomcvzgtvc.now.sh`.
264268
- Point successful deployment to your domain with `now alias` or `now ln NOW_URL mydomain.com` (`NOW_URL` is URL of your deployment).
265269

270+
## Deploy to Heroku
271+
272+
In this section we will learn how to deploy our app to [Heroku cloud](https://www.heroku.com/home). We will deploy our React-Next-Express app to lightweight Heroku container called [dyno](https://www.heroku.com/dynos).
273+
274+
Instructions are for app located at `/book/8-end`.
275+
Adjust route if you are deploying app from the root of this public repo.
276+
277+
We will discuss the following topics in this section:
278+
1. installing Heroku on Linux-based OS
279+
2. creating app on Heroku dashboard
280+
3. preparing app for deployment
281+
4. configuring env variables
282+
5. deploying app
283+
6. checking logs
284+
7. adding custom domain
285+
286+
Let's go step by step.
287+
288+
1. Install Heroku CLI (command-line interface) on your OS. Follow the [official guide](https://devcenter.heroku.com/articles/heroku-cli). In this book we provide instructions for Linux-based systems, in particular, a Ubuntu OS. For Ubuntu OS, run in your terminal:
289+
<pre>sudo snap install --classic heroku</pre>
290+
To confirm a successful installation, run:
291+
<pre>heroku --version</pre>
292+
As example, my output that confirms successful installation, looks like:
293+
<pre>heroku/7.22.7 linux-x64 node-v11.10.1</pre>
294+
295+
2. [Sign up](https://signup.heroku.com/) for Heroku, go to your Heroku dashboard and click purple <b>New</b> button on the right:
296+
![image](https://user-images.githubusercontent.com/10218864/54558094-12b1f100-497a-11e9-94dd-d36399052931.png)
297+
298+
On the next screen, give a name to your app and select a region. Click purple <b>Create app</b> button at the bottom:
299+
![image](https://user-images.githubusercontent.com/10218864/54558276-8eac3900-497a-11e9-9026-25aa5047af87.png)
300+
301+
You will be redirected to `Deploy` tab of your newly created Heroku app:
302+
![image](https://user-images.githubusercontent.com/10218864/54558544-417c9700-497b-11e9-8885-6fdfde21c747.png)
303+
304+
3. As you can see from the above screenshot, you have two options. You can deploy the app directly from your local machine using Heroku CLI or directly from GitHub.
305+
In this tutorial, we will deploy a `builderbook/builderbook/book/8-end` app from our public [builderbook/builderbook](https://github.com/builderbook/builderbook) repo hosted on GitHub. Deploying from a private repo will be a similar process.
306+
307+
Deploying from GitHub has a few advantages. Heroku uses git to track changes in a codebase. It's possible to deploy app from the local machine using Heroku CLI, however you have to create a [Git repo](https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository) for `builderbook/builderbook/book/8-end` with `package.json` file at the root level. A first advantage is that we can deploy from a non-root folder using GitHub instead of Heroku CLI.
308+
309+
A second advantage is automation, later on you can create a branch that automatically deploy every new commit to Heroku. For example, we have a [deploy branch](https://github.com/async-labs/saas/tree/deploy) for our demo for [SaaS boilerplate](https://github.com/async-labs/saas/). When we commit to `master` branch - there is no new deployment, when we commit to `deploy` branch - new change is automatically deployed to Heroku app.
310+
311+
Let's set up deploying from GitHub. On `Deploy` tab of your Heroku app at Heroku dashboard, click <b>Connect to GitHub</b>, then search for your repo, then click <b>Connect</b> next to the name of the proper repo:
312+
![image](https://user-images.githubusercontent.com/10218864/54560210-09775300-497f-11e9-9027-2e3850ec7ff1.png)
313+
314+
If successful, you will see green text `Connected` and be offered to select a branch and deploy app automatically or manually. Automatic deployment will deploy every new commit, manual deployment requires you to manually click on <b>Deploy Branch</b> button. For simplicity, we will deploy manually from `master` branch of our `builderbook/builderbook` repo.
315+
316+
Before we perform a manual deployment via GitHub, we need Heroku to run some additional code while app is being deploying. Firstly, we need to tell Heroku that `8-end` app in the `builderbook/builderbook` repo is not at the root level, it's actually nested at `/book/8-end`. Secondly, Heroku needs to know that our app is Node.js app so Heroku finds `package.json` file, properly installs dependencies and runs proper scripts (such as `build` and `start` scripts from `package.json`). To achieve this, we need to add so called `buildpacks` to our Heroku app. Click `Settings` tab, scroll to `Buildpacks` section and click purple <b>Add buildpack</b> button:
317+
![image](https://user-images.githubusercontent.com/10218864/54561192-50fede80-4981-11e9-976a-c3d7c88527ec.png)
318+
319+
Add two buildpacks, first is `https://github.com/timanovsky/subdir-heroku-buildpack` and second is `heroku/nodejs`:
320+
![image](https://user-images.githubusercontent.com/10218864/54561577-30835400-4982-11e9-997f-4711d999808e.png)
321+
322+
Next, scroll up while on `Settings` tab and click purple <b>Reveal Config Vars</b> button, create a new environmental variable `PROJECT_PATH` with value `book/8-end`:
323+
![image](https://user-images.githubusercontent.com/10218864/54561775-a5568e00-4982-11e9-9561-2e5827873779.png)
324+
325+
The above variable will be used by the first buildpack `subdir-heroku-buildpack` to deploy app from repo's subdirectory.
326+
327+
4. If we deploy app at this point, our app will deploy with errors since we did not add environmental variables. Similar to how you added `PROJECT_PATH` variable, add all environmental variables from `book/8-end/.env` file to your Heroku app. Remember to add:
328+
- `MONGO_URL`,
329+
- `Google_clientID`,
330+
- `Google_clientSecret`,
331+
- `EMAIL_SUPPORT_FROM_ADDRESS`,
332+
- `Github_Test_ClientID`,
333+
- `Github_Test_SecretKey`,
334+
- `Github_Live_ClientID`,
335+
- `Github_Live_SecretKey`,
336+
- `Stripe_Test_SecretKey`,
337+
- `Stripe_Live_SecretKey`,
338+
- `MAILCHIMP_API_KEY`,
339+
- `MAILCHIMP_PURCHASED_LIST_ID`,
340+
- `SESSION_SECRET`.
341+
342+
343+
5. While on `Settings` tab, scroll to `Domains and certificates` section and note your app's URL. My app's URL is: https://builderbook-8-end.herokuapp.com
344+
Let's deploy, go to `Deploy` tab, scroll to `Manual deploy` section and click <b>Deploy branch</b> button.
345+
After deployment process is complete , navigate to your app's URL:
346+
![image](https://user-images.githubusercontent.com/10218864/54564053-10569380-4988-11e9-87dd-f81a28dd6406.png)
347+
348+
6. Server logs are not available on Heroku dashboard. To see logs, you have to use Heroku CLI.
349+
In your terminal, run:
350+
<pre>heroku login</pre>
351+
352+
Follow instructions to log in to Heroku CLI.
353+
354+
After successful login, terminal will print:
355+
<pre>Logged in as [email protected]</pre>
356+
357+
Where `[email protected]` is an email address that you used to create your Heroku account.
358+
359+
To see logs, in your terminal run:
360+
<pre>heroku logs --app builderbook-8-end --tail</pre>
361+
362+
In your terminal, you will see your most recent logs and be able to see a real-time logs.
363+
364+
You can output certain number of lines (N) for retrieved logs by adding `--num N` to the `heroku logs` command.
365+
You can print only app's logs by adding `--source app` or system's logs by adding `--source heroku`.
366+
367+
7. Time to add a custom domain. The Heroku app that we created is deployed on `free dyno`. Free dyno plan does not let you to add a custom domain to your app. To add custom domain, go to `Resources` tab and click purple <b>Change Dyno Type</b> button:
368+
![image](https://user-images.githubusercontent.com/10218864/54622849-983faa80-4a27-11e9-957f-54fe5aa742ca.png)
369+
370+
Select a `Hobby` plan and click <b>Save</b> button.
371+
372+
Navigate to `Settings` tab and scroll to the `Domains and certificates` and click purple <b>Add domain</b> button:
373+
![image](https://user-images.githubusercontent.com/10218864/54623152-36cc0b80-4a28-11e9-974b-8a14fb56a86a.png)
374+
375+
Type your custom domain name, I added `heroku.builderbook.org` as a custom domain, click <b>Save changes</b> button.
376+
377+
Heroku will displa you a value for CNAME record that you have to create for your custom domain. For me, custom domain is `heroku.builderbook.org and I manage DNS records at Now by Zeit.
378+
379+
After you create a CNAME, ACM status on Heroku's dashboard will change to `Ok`:
380+
![image](https://user-images.githubusercontent.com/10218864/54624195-2452d180-4a2a-11e9-999d-a6a771cde73c.png)
381+
382+
It's important that you remember to manually add your custom domain to the settings of your Google OAuth app (Chapter 3) and GitHub OAuth app (Chapter 6). If you forget to do it, you will see errors when you try to log in to your app or when you try to connect GitHub to your app.
383+
266384
## Scaling
267385

268386
You may want to consider splitting single Next/Express server into two servers:

0 commit comments

Comments
 (0)