Heroku is a "platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud."
Here's how to deploy your implementation of, say, C$50 Finance to Heroku from GitHub.
-
Watch Brian's seminar to learn about
gitand GitHub, if not already familiar. -
Create a new private repository at https://github.com/new (called, e.g.,
finance). -
Take note of the HTTPS URL of the repository (e.g.,
https://github.com/username/finance.git, whereusernameis your own GitHub username). -
Change to your
financedirectory in Visual Studio Code or CS50 IDE, as viacd. -
(Codespace users only) Copy your finance project files to
workspaces/finance.Create a folder named
financeunderworkspacesdirectory by runningmkdir /workspaces/financeCopy your finance project files to
/workspaces/financeby runningcp -r * /workspaces/financeThen change your directory to
/workspaces/financeby runningcd /workspaces/financeRun
lsto ensure all files have been copied over successfully. -
Create a
gitrepo therein.git init -
Add the GitHub repository as a "remote," where
USERNAMEis your own GitHub username.git remote add origin git@github.com:USERNAME/finance.git -
In the
requirements.txtfile inside of yourfinancedirectory, addgunicornandpsycopg2, each on separate lines. Your file should then resemble:cs50 Flask Flask-Session gunicorn psycopg2 requests -
Push your code to GitHub.
git add -A git commit -m "first commit" git branch -M main git push -u origin mainIf you visit
https://github.com/username/finance, whereusernameis your own GitHub username, you should see your code in the repository. -
Sign up for a free Heroku account at https://signup.heroku.com/, if you don't have one already.
-
Create a new app at https://dashboard.heroku.com/new-app.
-
Configure your app at
https://dashboard.heroku.com/apps/app-name/deploy/github, whereapp-nameis your Heroku app's name.-
Add this app to a pipeline: No need to configure; leave as is.
-
Deployment method: Select GitHub, then click Connect to GitHub. If prompted to log into GitHub, click Authorize heroku.
-
Connected to GitHub: Search for your app's repository (e.g.,
username/finance, whereusernameis your own GitHub username), then click Connect. -
Automatic deploys: Click Enable Automatic Deploys.
-
-
Configure your app at
https://dashboard.heroku.com/apps/app-name/settings, whereapp-nameis your Heroku app's name.-
Click Reveal Config Vars.
-
Add a new variable called API_KEY, the value of which is your API token for IEX. Recall that, after registering for a developer account at https://iexcloud.io/, you can obtain your API token under API Tokens. Be sure to use your PUBLISHABLE token as the value for API_KEY, not your SECRET token.
-
-
Search for and provision Heroku Postgres add-ons at
https://dashboard.heroku.com/apps/app-name/resources, whereapp-nameis your Heroku app's name; select a Plan name of Mini. -
At
https://dashboard.heroku.com/apps/app-name/resources, whereapp-nameis your Heroku app's name, click Heroku Postgres. In the tab that opens, click Settings, then click View Credentials.... Highlight and copy the URI that appears. -
In Visual Studio Code or CS50 IDE, open
app.pyinfinance/and replacedb = SQL("sqlite:///finance.db")
with
uri = os.getenv("DATABASE_URL") if uri.startswith("postgres://"): uri = uri.replace("postgres://", "postgresql://") db = SQL(uri)
so that the CS50 Library will connect to your PostgreSQL database instead of your SQLite database. Be sure to add
import os
atop
app.py, if not there already. -
Ensure that any column that's a primary key has the
AUTOINCREMENTkeyword, re-creating it if need be. -
In Visual Studio Code or CS50 IDE, execute the below to import
finance.dbinto your PostgreSQL database, whereURIis that same URI. Be sure to append?sslmode=allowto the URI. Note that disabling SSL's certification verification with--no-ssl-cert-verificationis not recommended in general but seems to be a temporary workaround.pgloader --no-ssl-cert-verification finance.db URI?sslmode=allowThereafter, if you'd like to browse or edit your PostgreSQL database, you can use Adminer (a tool like phpLiteAdmin for PostgreSQL databases), at adminer.cs50.net. Log in using your database's credentials: at
https://dashboard.heroku.com/apps/app-name/resources, whereapp-nameis your Heroku app's name, click Heroku Postgres. In the tab that opens, click Settings, then click View Credentials.... -
Create a new file in Visual Studio Code or CS50 IDE called
Procfileinfinance/whose contents are:web: gunicorn app:appThat file will tell Heroku to look in a file called
app.pyfor a variable calledappand serve it with Gunicorn, a production-quality web server. (Flask's built-in web server is "good enough for testing but probably not what you want to use in production.") -
Add that file to your repository and push it to GitHub.
git add -A git commit -m "added Procfile" git pushIf you visit
https://app-name.herokuapp.com/, whereapp-nameis your Heroku app's name, you should see your app! If you instead see some error, visithttps://dashboard.heroku.com/apps/app-name/logs, whereapp-nameis your app's name, to diagnose! Each time you add (new or changed) files to your repository and push to GitHub hereafter, your app will be re-deployed to Heroku.