Skip to content

Instantly share code, notes, and snippets.

@leongjinqwen
Created November 13, 2020 12:08
Show Gist options
  • Save leongjinqwen/c32a1247db06626d34f3fe875a87b687 to your computer and use it in GitHub Desktop.
Save leongjinqwen/c32a1247db06626d34f3fe875a87b687 to your computer and use it in GitHub Desktop.
Step by step deploy your app to Heroku

Heroku Deployment

  1. Create an app on Heroku
  2. Create a free Postgres Heroku instance.
  3. Set a new git remote to your Heroku git link
  4. Copy your Heroku git link from Heroku's website
  5. Go back to the root of your project directory and type
git remote add production <your heroku git link>

Now your app has 2 git remotes! origin -> GitHub and production -> Heroku

  1. Set environment variables on Heroku! Our app relies on a few env variables. Locally on our computer, we used .env file to manage all these variables but in production, we don't do that. On heroku, here's how you can add env variables to the session.

    ensure you're in your project directory when running heroku commands. (add all the environment variables you need)

    heroku config:set FLASK_APP='start' FLASK_ENV='production' S3_KEY="yourawss3key" S3_SECRET="yourawss3secret" S3_BUCKET="your-bucket" # and more

    protip: For security, use a different AWS S3 bucket and key/secret for production vs staging vs development environments!

    DATABASE_URL is already set for you by Heroku when you created a Postgres DB at step 1.

    Pro tip: see all env variables set

    heroku config
  2. Temporarily remove peewee-db-evolve from requirements.txt (copy first; you'll need it later)

  3. Make a commit and deploy git push production master

  4. Add peewee-db-evolve back into requirements.txt (Please make sure peewee-db-evolve version is at least 3.7.3)

  5. Add a Procfile at the root of your project Procfile

    release: python migrate.py
    web: gunicorn start:app --preload
    

    The setting above tells Heroku to run python migrate.py each time you deploy new code, update env configs or modify heroku's add-on resources. And to start the flask server, use gunicorn start:app --preload command. gunicorn start:app here tells Gunicorn to refer to start.py for starting the app.

  6. pip install gunicorn Gunicorn is a production-ready web server software (usually for Python apps). Every time you start Flask using flask run, it uses a web server software that is not great for production. Hence the use of gunicorn! Remember to run

    pip freeze > requirements.txt
  7. Make a new commit (be sure to run git add Procfile first)

  8. Deploy again git push production master

That's all! You should see a link to your application at the end of GIT Push process.

All these steps are a one time affair. In future when you make changes to your app, it's as simple as committing to git and then pushing it with git push production master

Want your own domain?

  1. Buy a domain
  2. Go to heroku.com, add your domain to the app
  3. Go to your domain manager (usually where you buy from) and change the nameserver/dns settings!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment