Skip to content

Raghul-M/Python-Github_Actions-Heroku

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Webapp Deployment on Heroku Using GitHub Actions

1_HY_qNXxMeD3LyUaXZHlcug

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.

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/[email protected]
       with:
           heroku_api_key: ${{secrets.HEROKU_API_TOKEN}}
           heroku_app_name: ${{secrets.HEROKU_APP_NAME}} #Must be unique in Heroku
           heroku_email: "[email protected]"
     

Screenshots

Localhost App

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

Deployed App on Heroku

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

Releases

No releases published

Packages

No packages published