This repository contains a FastAPI application configured for automatic deployment to Heroku using Docker containers through GitHub Actions. The setup ensures seamless CI/CD workflows, allowing for automated testing and deployment with minimal manual intervention.
Copy code
my_project/
├── app/
│ ├── __init__.py
│ ├── main.py
│ └── routers/
│ ├── items.py
│ ├── users.py
│ └── grimoire.py
├── .github/
│ └── workflows/
│ └── deploy_to_heroku.yml
├── Dockerfile
├── requirements.txt
└── README.md- app/: Contains the FastAPI application code, including routers and the main app instance.
- .github/workflows/deploy_to_heroku.yml: Defines the GitHub Actions workflow for CI/CD.
- Dockerfile: Instructions for building the Docker image.
- requirements.txt: Lists the Python dependencies for the application.
The Dockerfile is set up to package the FastAPI application into a Docker container, making it ready for deployment to Heroku. Key considerations:
- Uses python:3.9-slim as the base image.
- Sets /app as the working directory within the container.
- Installs Python dependencies from requirements.txt.
- Copies the application code into the container.
- Starts the application using uvicorn with dynamic port binding to accommodate Heroku's $PORT.
-
The CI/CD process is managed through a GitHub Actions workflow defined in .github/workflows/ci_cd.yml. The workflow automates the following steps:
-
Build: Creates the Docker image based on the Dockerfile.
-
Test: (Optional) Runs automated tests to ensure the application behaves as expected.
-
Deploy:
- Logs into Heroku Container Registry.
- Pushes the Docker image to the Heroku Container Registry.
- Releases the image on Heroku, updating the application.
Deployment is handled automatically by the GitHub Actions workflow. To deploy manually or make changes:
- Ensure you have the Heroku CLI installed and logged in.
- Use heroku container:login to log in to the Heroku Container Registry.
- Build and push the Docker image to Heroku using docker build and docker push.
- Release the image on Heroku to update the application.
HEROKU_API_KEY: Your Heroku API key, stored as a GitHub secret for CI/CD. HEROKU_APP_NAME: The name of your Heroku app, also stored as a GitHub secret.
To update the application:
- Push changes to your GitHub repository.
- The GitHub Actions workflow will automatically trigger, running through the build, test, and deploy steps.
- Monitor the Actions tab in GitHub for the status of your CI/CD pipeline.
For local development:
- Ensure Docker is installed and running.
- Build the Docker image: docker build -t my_fastapi_app .
- Run the container: docker run -p 8000:80 my_fastapi_app
- Adjust port mappings and image names as necessary for your setup.
##Additional Notes Review the Dockerfile and GitHub Actions workflow to ensure they meet your project's needs. Modify the .env file or set environment variables in Heroku for any application-specific configuration.