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.
Sign up for free, private GitHub repositories at https://education.github.com/discount_requests/new, if you haven’t already.
Create a new private repository at https://github.com/new (called, e.g.,
Take note of the HTTPS URL of the repository (e.g.,
usernameis your own GitHub username).
Change to your implementation’s directory on CS50 IDE.
Add the GitHub repository as a “remote,” where
usernameis your own GitHub username.
git remote add origin https://github.com/username/finance.git
Push your code to GitHub.
git commit -am "first commit" git push -u origin master
If you visit
usernameis 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
app-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.
- App connected to GitHub: Search for your app’s repository (e.g.,
usernameis your own GitHub username), then click Connect.
- Automatic deploys: Click Enable Automatic Deploys.
Search for and provision Heroku Postgres at
app-nameis your Heroku app’s name; select a Plan name of Hobby Dev — Free.
app-nameis your Heroku app’s name, click Heroku Postgres :: Database. In the tab that opens, click Settings, then click View Credentials…. Highlight and copy the URI that appears.
In CS50 IDE, open
db = SQL("sqlite:///finance.db")
sqlite:///finance.dbwith that URI (so that the CS50 Library will connect to your Postgres database instead of your SQLite database).
In CS50 IDE, execute the below to import
finance.dbinto your Postgres database, where
URIis that same URI. Be sure to append
?sslmode=requireto the URI.
pgloader finance.db URI?sslmode=require
Thereafter, if you’d like to browse or edit your Postgres database, you can use Adminer (a tool like phpLiteAdmin for Postgres databases), at adminer.cs50.net. Log in using your database’s credentials: at
app-nameis your Heroku app’s name, click Heroku Postgres :: Database. In the tab that opens, click Settings, then click View Credentials….
Create a new file in CS50 IDE called
~/pset8/finance/whose contents are:
web: gunicorn application:app
That file will tell Heroku to look in a file called
application.pyfor a variable called
appand 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 -am "added Procfile" git push
If you visit
app-nameis your Heroku app’s name, you should see your app! If you instead see some error, visit
app-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.