Deploy My Flask App to Heroku Using Git

I heard deploying the projects I’ve worked on onto Heroku is one of the greatest way to demo my final product. So I did a little research and here’s what I gathered.

  1. Install Git and Heroku CLI

    According to my reference, I need to install git and Heroku CLI, but since I already have git, I only needed to install Heroku CLI. I did that by simplying typing:

    $ curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
    

    To verify installation by using the following command:

    $ heroku --version
    
  2. Sign Up/Log in for Heroku and Create Project

    Type in the following command and sign up/sign in. It will take you to the page.

    $ heroku login
    

    Once you have your account created or you’ve signed in, create your app by typing:

    $ heroku create {project name}
    

    If you already created your app previously, you could skip this part on creating an app.

  3. Create a virtual environment with pipenv

    In order to deploy your app onto heroku, you’d need the Pipefile and Pipefile.lock. Pipefile.lock and Pipefile will tell heroku the requirements for your app. If you don’t have pipenv installed, you could use command:

    $ pip3 install pipenv
    $ pipenv install {your requirements}
    

    To create the virtual environment:

    $ pipenv install flask gunicorn psycopg2-binary
    
  4. Create Procfile

    To create a Procfile, you could use the following command.

    $ touch Procfile
    

    In your Procfile, enter:

    web: gunicorn wsgi:app
    
  5. Create WSGI

    What is WSGI? It stands for Web Server Gateway Interface; it describes how the web server communicate with the app. To create file,

    $ touch wsgi.py
    

    Enter the following into the file:

    from <folder.file name of your app> import <name of app>
    if __name__ == "__main__":
        app.run()
    
  6. Run the Virtual Environment

    $ pipenv shell
    
  7. Use git to make your commit

    Make your commit like how you normally push your commit on github:

    $ git init
    $ git add .
    $ git commit -m "comment here"
    

    To check on whether you’re in the right repo/project:

    $ git remote -v
    

    To nevigate to the right repo:

    $ heroku git:remote -a <name of project>
    

    Yay! Now you can finally deploy:

    $ git push heroku master
    

If everything goes well, you should be able to view your app with the link provided under Settings > Domains in your account. Don’t make the mistake like I did using the Heroku git URL (ends with .git) :(

But yay! I finally got it to deploy!!!! I was so excited to see my app up in production!!!… Until… I ran into prod issue. More to come once I get the issue resolved.

Reference:

https://devcenter.heroku.com/articles/heroku-cli#download-and-install https://devcenter.heroku.com/articles/git#prerequisites-install-git-and-the-heroku-cli https://www.geeksforgeeks.org/deploy-python-flask-app-on-heroku/

comments powered by Disqus