Installtion, Deployment of FastAPI with Swagger UI on VPS & Creation of CI-CD Pipeline with GitHub WorkFlows.
# Update System Packages
sudo apt update && sudo apt upgrade -y
# Install Docker
sudo apt install docker.io -y
#Install Docker Compose
sudo apt install docker-compose -y
- Before Cloning Private Repo following should be done
- Generate SSH Key from terminal to add it to the GitHub Settings
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Add SSH key to the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
- Copy the SSH public key to your clipboard:
cat ~/.ssh/id_rsa.pub
- Add SSH Key to GitHub:
- Copy the SSH public key:
- Copy the output of the cat ~/.ssh/id_rsa.pub command.
- Move to GitHub Repository:
-
Navigate to your GitHub repository.
-
Go to "Settings" > "Deploy keys" > "Add deploy key."
-
Paste the SSH public key and give it a title.
-
To set your GitHub username and email locally on your development machine, you can use the following Git commands:
- Set User Name:
git config --global user.name "Your GitHub Username"
- Set Email:
git config --global user.email "your.email@example.com"
- Clone Your GitHub Repository:
git clone https://github.com/yourusername/yourrepository.git
- Navigate to Your Project Directory:
cd yourrepository
-
Follow the current project tree for the more clarity.
-
Build and Run Docker Containers:
docker-compose up -d --build
- Install Nginx:
sudo apt install nginx -y
- Add the following configuration (replace your_domain_or_ip with your VPS IP or domain):
server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
- Create a Symbolic Link:
sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled
- Restart Nginx:
sudo service nginx restart
- Create GitHub Actions Workflow:
- Inside your GitHub repository, create a .github/workflows/main.yml file.
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Build and Push Docker Image
run: |
docker build -t your_docker_username/yourrepository:latest .
docker login -u your_docker_username -p ${{ secrets.DOCKER_PASSWORD }}
docker push your_docker_username/yourrepository:latest
- name: SSH into VPS and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
key: ${{ secrets.VPS_SSH_KEY }}
port: 22
script: |
cd yourrepository
git pull origin main
docker-compose down
docker-compose up -d --build
-
Make sure to replace placeholders like your_docker_username, yourrepository, secrets.DOCKER_PASSWORD, secrets.VPS_HOST, secrets.VPS_USERNAME, and secrets.VPS_SSH_KEY with your actual values.
-
GitHub Repository Secrets:
- Go to your GitHub repository.
- Navigate to "Settings" > "Secrets" > "New repository secret."
- Add the following secrets:
- DOCKER_PASSWORD: Your Docker Hub password.
- VPS_HOST: Your VPS IP address.
- VPS_USERNAME: Your VPS username.
- VPS_SSH_KEY: Your private SSH key for connecting to your VPS.