Automated-FastAPI-Deployment

πŸš€ FastAPI Docker Deployment with GitHub Actions

Welcome to the FastAPI Docker Deployment project! This repository demonstrates how to automate the creation and deployment of a Dockerized FastAPI application using GitHub Actions. Whether you’re a DevOps enthusiast or a developer looking to streamline your CI/CD pipeline, this project is for you!


🌟 Features


πŸ› οΈ Prerequisites

Before you begin, ensure you have the following installed:


πŸš€ Quick Start

1. Clone the Repository

git clone https://github.com/your-username/devops-assignment.git
cd devops-assignment

2. Run the FastAPI Server Locally

  1. Install dependencies:
    pip install -r requirements.txt
    
  2. Start the server:
    uvicorn main:app --host 0.0.0.0 --port 8000 --reload
    
  3. Access the server at: http://localhost:8000

🐳 Docker Setup

1. Build the Docker Image

docker build -t your-dockerhub-username/fastapi-app:latest .

2. Run the Docker Container

docker run -p 8000:8000 your-dockerhub-username/fastapi-app:latest

3. Access the FastAPI Server

Open your browser and go to: http://localhost:8000


πŸ€– GitHub Actions Workflow

This project uses GitHub Actions to automate the build and deployment process. Here’s how it works:

  1. Trigger: The workflow runs on every push event to the main branch.
  2. Steps:
    • Check out the repository.
    • Log in to Docker Hub using a secret token.
    • Build the Docker image.
    • Push the image to Docker Hub.

View the Workflow File

Check out the workflow file: .github/workflows/docker-deploy.yml


πŸ” Docker Hub Token Setup

To push the Docker image to Docker Hub, you need to set up a Personal Access Token:

  1. Go to Docker Hub β†’ Account Settings β†’ Security β†’ Access Tokens.
  2. Generate a new token and copy it.
  3. Add the token as a secret in your GitHub repository:
    • Go to Settings β†’ Secrets and variables β†’ Actions.
    • Add a new secret named DOCKERHUB_TOKEN and paste the token value.

πŸ“¦ Docker Hub Image

The Docker image for this project is available on Docker Hub. You can pull it using:

docker pull your-dockerhub-username/fastapi-app:latest

Docker Hub URL: https://hub.docker.com/repository/docker/your-dockerhub-username/fastapi-app

My Docker Hub URL: https://hub.docker.com/repository/docker/simrannegi/fastapi-app

πŸ“‚ Project Structure

devops-assignment/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── docker-deploy.yml  # GitHub Actions workflow
β”œβ”€β”€ main.py                    # FastAPI server code
β”œβ”€β”€ requirements.txt           # Python dependencies
β”œβ”€β”€ Dockerfile                 # Docker configuration
└── README.md                  # Project documentation

πŸ› οΈ Built With


Happy Coding! πŸŽ‰