Skip to content

Commit de41397

Browse files
authored
Merge pull request trallard#54 from trallard/heroku-guide
Added step-by-step Heroku deployment guide
2 parents 1c5cfd8 + f9e245f commit de41397

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

assets/create_heroku_app.png

40.8 KB
Loading

assets/heroku_config.png

70.5 KB
Loading

assets/heroku_dashboard.png

53.2 KB
Loading

deploy-to-heroku.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Deploying your app to Heroku - a step by step guide
2+
3+
Up until now, we have been working on our app and running it locally on our laptops. Whilst
4+
this is useful for testing out our app during development, it's no good if our friends want to see
5+
what we've done in this course!
6+
7+
In order for our friends to see the app, we need to upload (deploy) our code to Heroku, a cloud-based platform
8+
which will assign a publicly accessible URL to our app that can then be visited!
9+
10+
Without further ado, let's go through how you can deploy your first app to Heroku!
11+
12+
0. Before you start to deploy your app, there are some chores we need to do first:
13+
* Make a `Procfile` in the **root folder** of your application. This file contains the Command Prompt/Terminal command telling Heroku exactly how to start/run your application.
14+
It normally contains only the following line:
15+
```
16+
web: python app.py
17+
```
18+
Replace `app.py` with the actual name of your file containing your Flask code if you named it something different.
19+
In case you're curious: the `web:` part before the command basically tells Heroku to run your Python code as a web application.
20+
21+
* Make a file named `requirements.txt` in the **root folder** of your application. This file lists all the external libraries
22+
(such as `Flask` and `tweepy`) required by your application, so that Heroku knows exactly the required libraries to install
23+
before trying to run your application.
24+
In this file, type in the libraries your application require line by line, which may look something like the following:
25+
```
26+
flask
27+
requests
28+
tweepy
29+
requests
30+
```
31+
32+
* In `app.py` (or whatever you named your main file containing the Flask code), add the following line to the top
33+
of your Flask code:
34+
```python
35+
import os
36+
```
37+
For the curious: the `os` module is imported so that we can access *environment variables* set by Heroku, which includes the
38+
*port* it has assigned to run your application on.
39+
40+
Next (usually near the bottom of your Flask code), replace
41+
```python
42+
app.run(debug=True)
43+
```
44+
with the following:
45+
```python
46+
if 'PORT' in os.environ:
47+
app.run(host='0.0.0.0', port=int(os.environ['PORT']))
48+
else:
49+
app.run(debug=True)
50+
```
51+
Again, for the curious: this piece of conditional logic checks whether you are running your application on Heroku
52+
(in which case, the `PORT` environment variable would exist), or if you are running your application locally. The
53+
`host=0.0.0.0` bit instructs your Flask application to listen on **all** web address. This is important to ensure
54+
the application runs on Heroku since we don't know which host Heroku will decide to host the application on.
55+
56+
1. If you haven't already, go make a Heroku account at https://www.heroku.com
57+
58+
2. Once you have made a Heroku account, upon logging in, you will be taken to a dashboard, which looks something like the
59+
following:
60+
61+
![alt text](assets/heroku_dashboard.png "Heroku Dashboard")
62+
63+
3. To deploy the app, we need to make one first on Heroku, so do that by clicking **New > Create New app** (button at top right of the
64+
dashboard)
65+
66+
4. Type in the name of your application, and change the Runtime Selection to "Europe" - this is so that the application loads
67+
faster as we are geographically closer to Europe than the United States. Finally, click "Create App".
68+
69+
![alt text](assets/create_heroku_app.png "Create app screen")
70+
71+
5. After clicking "Create App", you should be greeted with the following screen:
72+
73+
![alt text](assets/heroku_config.png "App configuration screen")
74+
75+
6. Under "Deployment Method", change it to "GitHub". A "Connect to GitHub" section should now appear.
76+
77+
7. Under the "Connect to GitHub" section that just appeared, search for the GitHub repository corresponding to your project, then
78+
click "Connect" to link the relevant repository (appeared as a result of the search) to the Heroku application you've just created.
79+
80+
8. To deploy your project, click "Deploy Branch" under "Manual deploy".
81+
82+
9. (Optional) You may have just noticed that a section named "Automatic deploys" has appeared in addition to the "Manual deploy"
83+
section once you have connected your GitHub repository properly. To save you having to come back to this page in the future
84+
and click "Deploy Branch" each time you pushed something new to your repository, if you click "Enable Automatic Deploys", then
85+
Heroku will do so automatically.
86+
87+
Congratulations! You have now successfully deployed your application to Heroku!

0 commit comments

Comments
 (0)