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.
This project includes:
- A basic Flask web application.
- Tests written in pytest.
- Automated deployment to Heroku using GitHub Actions.
- Python 3.10+
- Git
- GitHub account
- Heroku account
- Heroku CLI
-
Clone the repository:
git clone https://github.com/Raghul-M/Python-Github_Actions-Heroku.git cd Python-Github_Actions-Heroku
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Run the application locally:
python3 app.py
Run tests with pytest:
pytest
Heroku Setup
-
Login to Heroku:
heroku login
-
Create a new Heroku app:
heroku create your-app-name
-
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.
-
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]"