Skip to main content

Command Palette

Search for a command to run...

Python Webapp Deployment on Heroku Using GitHub Actions

Updated
•3 min read
Python Webapp Deployment on Heroku Using GitHub Actions

This project demonstrates how to deploy a simple Python web application using Flask to Heroku deployment, with automated testing using pytest and deployment using GitHub Actions.

Github Repo link : 🔗Heroku Deployment using Github Actions

Project Description

This project includes:

  • A basic Flask web application.

  • Tests written in pytest.

  • Automated deployment to Heroku using GitHub Actions.

Prerequisites

  • Python 3.10+

  • Git

  • GitHub account

  • Heroku account

  • Heroku CLI

Local Setup

  1. Clone the repository:

     git clone https://github.com/Raghul-M/Python-Github_Actions-Heroku.git
     cd Python-Github_Actions-Heroku
    
  2. Create a virtual environment:

     python -m venv venv
     source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    
  3. Install dependencies:

     pip install -r requirements.txt
    
  4. Run the application locally:

     python3 app.py
    

Running Tests

Run tests with pytest:

pytest

Screenshot from 2024-06-28 15-29-45

Deployment

Heroku Setup

Screenshot from 2024-06-28 15-40-58

  1. Login to Heroku:

     heroku login
    
  2. Create a new Heroku app:

     heroku create your-app-name
    
  3. Set up GitHub Actions for Heroku deployment:

    • Go to your GitHub repository.

    • Navigate to Settings > Secrets > New repository secret.

    • Add the following secrets:

      • HEROKU_API_KEY: Your Heroku API key.

      • HEROKU_APP_NAME: Your Heroku app name.

  4. Add the GitHub Actions workflow file (.github/workflows/deploy.yml):

     name: Python application
     on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
      permissions:
        contents: read
      jobs:
        build:
    
      runs-on: ubuntu-latest
    
      steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Python 3.10
        uses: actions/setup-python@v3
        with:
          python-version: "3.10"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8 pytest
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
      - name: Lint with flake8
        run: |
          # stop the build if there are Python syntax errors or undefined names
          flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
          # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
          flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
      - name: Test with pytest
        run: |
          pytest
    
      deploy:
      needs: build
      runs-on: ubuntu-latest
      steps:
      - uses: actions/checkout@v2
      - uses: akhileshns/heroku-deploy@v3.12.12
        with:
            heroku_api_key: ${{secrets.HEROKU_API_TOKEN}}
            heroku_app_name: ${{secrets.HEROKU_APP_NAME}} #Must be unique in Heroku
            heroku_email: ${{secrets.HEROKU_EMAIL}}
    
  5. Create a Procfile

  6. Create a Runtime file

Output :

Localhost

Screenshot from 2024-06-28 15-36-18

Deployed App on Heroku

Screenshot from 2024-06-28 15-37-23

Contributing

Contributions are welcome! If you have suggestions, bug reports, or want to add new features, feel free to submit a pull request.

Feel free to explore, contribute, and adapt this project to suit your needs. If you encounter any issues or have suggestions for improvement, please raise them in the GitHub repository's issues section. Happy coding! 🚀

Connect with me on Linkedin: Raghul M

More from this blog

T

Tech Journal 📚

11 posts

Founder @CareerPod | SQE @Redhat | Python Developer | Cloud & DevOps Enthusiast | AI / ML Advocate | Tech Enthusiast

Heroku Python Deployment via GitHub Actions