Module 4: Web Application Deployment

Module Overview

You've put together a nice application - time to get it out there! A GitHub repo is great, but a deployed application is more effective in your portfolio as everyone in the hiring process (not just technical people) can check it out.

Note: While we use Heroku in this module as our primary deployment platform, it's important to know that Heroku has changed its pricing model and is no longer free by default. Alternatives like Render, Railway, or Fly.io offer similar functionality with generous free tiers for student projects. We'll include resources for these alternatives below.

Learning Objectives

Objective 01 - Diving into MongoDB

What is MongoDB?

MongoDB is a document-oriented NoSQL database. It stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time. This provides the ability to store, query, and process large amounts of data in myriad ways.

Setting up a MongoDB Account

First, you'll need to sign up for a free MongoDB account.

Then you'll need a MongoDB project.

Once MongoDB is set up, you can interact with it using PyMongo.

What is PyMongo?

PyMongo is a Python driver for MongoDB. It provides tools to work with MongoDB, allowing you to interact with MongoDB databases within your Python programs.

First, you need to install PyMongo with the command:

pip install python-dotenv fastapi pymongo

Then, you can connect to your MongoDB instance and perform various operations:

PyMongo Basic

import os
from pymongo import MongoClient
from dotenv import load_dotenv

# load the environment variables from the `.env` file
load_dotenv()

# create a MongoClient to interact with your MongoDB instance
db_url = os.getenv("MONGODB_URL")
client = MongoClient(db_url)

# get a reference to your database
db = client['mydatabase']

# get a reference to your collection
collection = db['mycollection']

# insert a document into the collection
collection.insert_one({
  "name": "John Smith", 
  "age": 30, 
  "job": "developer",
  "favorite_language": "Python",
})

# find a document in the collection
document = collection.find_one({"favorite_language": "Python"})

print(document)

Additional Resources

Objective 02 - Securely connect a deployed web application to a relational database back-end

Important Note:

Starting November 28, 2022, all free Heroku programs will no longer be available. You may choose to complete this module with the Heroku paid versions, but it is not required to complete this Sprint.

Overview

As we learned when we discussed full-stack web applications, the back-end handles requests but it is generally not the right place to persist data in the longterm. For that, we need a database, and for it to work in production we need to connect securely to it. This means using environment variables for secrets (passwords), not checking them into git or otherwise exposing them.

GitHub is great for your code - but not so great for your API keys. How do you avoid committing secrets that grant account access, when git itself is the tool for both backing up, sharing, and now deploying your code?

For local development, we set up python-dotenv - for the cloud, we'll use Heroku config vars.

Follow Along

After you deploy your application for the first time, you need to set all your config vars (Heroku's flavor of environment variables). You can retrieve all or one of them from the CLI as follows:

$ heroku config
USERNAME:jane
ENVIRONMENT:production

$ heroku config:get USERNAME
jane

You can set a username (creating or overriding it) with heroku config:set USERNAME=janesmith, and heroku config:unset USERNAME will simply remove it entirely.

Challenge

Note - you don't need to set the DATABASE_URL! Why? Because that gets set automatically when it is created (see the last line):

$ heroku addons:create heroku-postgresql:hobby-dev
Creating heroku-postgresql:hobby-dev on ⬢ sushi... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-concave-52656 as DATABASE_URL

Additional Resources

Resource - Git for Build Sprint

Important Note:

Starting November 28, 2022, all free Heroku programs will no longer be available. You may choose to complete this module with the Heroku paid versions, but it is not required to complete this Sprint.

Git MVP

Git MVP includes 4 main requirements:

  1. Use main as your primary branch
    You must use main as your primary branch, which is the default when creating a new repository.

    Why?
    Branching from the branches other than the main branch can create opportunities for merge conflicts.
  2. Feature Branch Naming
    Feature branches must be named: feature/<descriptive name>

    For example, if you're working on a feature that allows users to change the email address in their profile, the branch should be named feature/change-email

    More info on branches and how to create a branch can be found here.

    Why?
    Without a good naming convention, it is very easy to lose track of the purpose of a branch.
  3. Delete Merged Branches
    After a branch has been merged to the main branch, it must be immediately deleted.

    Why?
    Short-lived branches are a best-practice for minimizing merge conflicts. Leaving many branches active in a repository makes it difficult for team members to navigate the on-going work. Once a branch has been reviewed, approved, and merged into the main branch, it should be immediately deleted.
    A healthy Git repository has a minimum of active branches.
    This is easy to comply with using GitHub.
  4. Build Sprint Git Workflow
    Following this workflow make it easier to collaborate with your teammates. It will also help prepare you for Labs, since it is very similar to the Git workflow you will use in Labs!

    The idea is that each Trello card should be on its own pull request as a feature!

    The Build Sprint Git Workflow is designed to prepare you for your Labs experience. You can learn more about Git & GitHub in Labs, check out the Engineering Standards.

Getting Started

You're ready to work on a new Trello card - let's get started!

git checkout main
git pull origin main

To get started on your first task let's make a branch. Making sure you are on the main branch, start a new branch with a name that matches or correlates to the task you are about to begin.
The trick here is to think beyond yourself when naming the branch, stay aligned with your Trello board so you and others can easily make sense of it.
git checkout -b [feature/<descriptive name>]

Sharing Is Good

Now you have a branch to make all your awesome commits to!

Copying the description from the Trello card, which should summarize the actual work in the PR, create a Draft Pull Request using the mainbranch as the base after you have made your first commit.
Github created pull request drafts so that notifications will be turned off by default until you decide you are ready for review.

This animated gif is of the GitHub page to create pull requests. The dropdown to create a pull request is opened, and the option "Create Draft Pull Request" is selected.

We have lift off! 🚀
Now we're ready to make some awesome software. Committing and pushing our code regularly are habits worth having. Before we know it, we'll be ready to take the PR out of draft mode.

Tip: do your best to be a great team member by making commit messages as clear as possible. They don't have to be elaborate- keep it to the point.

One small step 👨🏾‍🚀

When you feel you've completed the task you've been working on, it's time to:

The image shows a pull request page on GitHub. The "Ready to review" button is highlighted.

The team will then be notified of the PR and they can begin a review before merging it to the main branch.

Tip: Follow the team policy regarding the number of reviewers required before merging. It's great practice to take the time and make comments in the code, even if just positive notes on good work your teammate did.

Merging to main

If there are no conflicts to be resolved in the code choose Rebase and merge or Squash and merge from the Merge button.

A screenshot of GitHub showing a pull request and the merge dropdown menu. The option "Rebase and merge" is selected in the dropdown menu.

If you find yourself with a merge conflict, there are a number of ways to solve it. The Github tools are very handy. You can also do it locally. When going down the local path, there is a good set of instructions here.

Your code changes are now on the main branch, ready to wow! users with your updates.

Deploy your code (if not otherwise handled by Github Events) and be ready to support and address any issues that arise.

A comic about git. Cueball is showing git on his computer to Ponytail and Hairy. Cueball says, "This is git. It tracks collaborative work on projects through a beautiful distributed graph theory tree model." Ponytail responds, "Cool. How do we use it?" Cueball replies, "No idea. Just memorize these shell commands and type them to sync up. If you get errors, save your work elsewhere, delete the project, and download a fresh copy."

Humor Break!

Revision History

Guided Project

Open guided-project.md in the GitHub repository to follow along with the guided project.

Starting November 28, 2022, all free Heroku programs will no longer be available. You are strongly advised to deploy on Render, which as of May 28th, 2025, has a free tier.

Module Assignment

Deploy your app! Name it something like twitoff-yourusername. You'll may have to debug. Use Render.com for deployment and configuration, and help your classmates with deployment challenges. You can find information about Render deployment in the Guided Project Resources above.

Assignment Solution Video

Additional Resources

Deployment Platforms

Database Configuration

Version Control