Introduction
In this article, I will walk you through how I deployed a FastAPI application with continuous integration (CI) and continuous deployment (CD).
Requirements
- Python
- FastAPI Application
- Docker
- GitHub Actions Secrets
- AWS Linux server
Step 1: Clone the Repository
The first step is to fork the
- I then started the FastAPI application using Uvicorn
uvicorn main:app --reload
- As you can see, our containers are up and running and Nginx is successfully routing incoming traffic to our FastAPI application.
Step 7: Create a Linux Server
For hosting and deploying our FastAPI application, I set up a Linux server on AWS. This server serves as the backbone of our deployment environment, where our Dockerized application will run and be accessible to users.
Step 8: Set Up the CI Pipeline
I created a GitHub Actions Workflow File for this step to automatically run tests on every pull request targeting the main branch. This ensures that any new changes are validated before they can be merged.
mkdir -p .github/workflows/
cd .github/workflows/
touch ci.yml
name: ci pipeline
on:
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: pytest
- push your changes to GitHub and check the status of your pipeline

Step 9: Set Up the Deployment Pipeline
For continuous deployment, I created another GitHub Actions Workflow File named deploy.yml. This deployment pipeline is triggered whenever changes are pushed to the main branch, automating the process of updating the live application.
touch deploy.yml
name: CD Pipeline
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Deploy via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
# Update package index and install dependencies
sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Install Docker
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# Add the SSH user to the Docker group
sudo usermod -aG docker ${{ secrets.SSH_USERNAME }}
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Verify installations
docker --version
docker-compose --version
# Navigate to the project directory and deploy
cd /home/ubuntu/
git clone <your-github-repo>
cd fastapi-book-project/
git pull
docker-compose up -d --build
- Ensure your secrets are stored: Make sure
SSH_HOST,SSH_USERNAME, andSSH_PRIVATE_KEYare added to your GitHub repository's secrets. - Push your changes to GitHub and check the status of your pipeline.

- Post-Deployment Verification: Once the pipeline completes, verify that your application is running as expected.

Step 10: Repository Access Requirement
Before submission, we are supposed to invite the hng12-devbotops to our GitHub account as a collaborator in our repository. To do this;
- Open the repository you want to add a collaborator
- Click on the Settings tab, which is usually located on the right side of the repository menu.
- In the left sidebar, under access, select Collaborators.
- Click on add people, add
hng12-devbotops

Thanks for reading, don't forget to drop a like and comment 😄😄 👩🏻💻👩🏻💻

SOCIAL SHARE CARD GENERATOR