Skip to content

Instantly share code, notes, and snippets.

@aybruhm
Last active February 26, 2023 01:32
Show Gist options
  • Save aybruhm/68d99656efa35b4d103e003975329be8 to your computer and use it in GitHub Desktop.
Save aybruhm/68d99656efa35b4d103e003975329be8 to your computer and use it in GitHub Desktop.
This is an action workflow that will deploy your python (django, fastapi, flask) application to a VPS (Digital Ocean, EC2 Instance, etc)
name: Build & Deploy
on:
push:
branches:
- dev
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Pull New Changes
uses: appleboy/ssh-action@v0.1.5
with:
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }} # if you deploying to aws ec2, replace ssh_key with your pem file
script: |
cd /path/to/project_dir
git pull origin dev
- name: Deploy Backend
uses: appleboy/ssh-action@v0.1.5
with:
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }} # if you deploying to aws ec2, replace ssh_key with your pem file
script: |
cd /path/to/project_dir
# ---------------------------------
# INSERT DEPLOYMENT COMMAND HERE
# ---------------------------------
echo 'Deployment successful!'
@aybruhm
Copy link
Author

aybruhm commented Feb 23, 2023

In the section where you see: "INSERT DEPLOYMENT COMMAND HERE", kindly replace the entire comment with the deployment command for the Python web framework you are using.

Examples

1). For Django, update with the following:

source venv/bin/activate
python manage.py makemigrations
python manage.py migrate

# using docker - option: 1
# no need for any command; 
# your application should automatically detect new file changes and the web compose service will restart

# using gunicorn - option 2
systemctl restart gunicorn # add other services like celery if you have any

2). For FastAPI, update with the following:

# using docker - option: 1
# no need for any command; 
# your application should automatically detect new file changes and the web compose service will restart

# using uvicorn - option 2
source venv/bin/activate
# -----------------------------
# add more commands here;
# run database migrations, etc
# -----------------------------
uvicorn main:app --host='0.0.0.0' --port=<PORT>

# using gunicorn x uvicorn - option 3
source venv/bin/activate
# -----------------------------
# add more commands here;
# run database migrations, etc
# -----------------------------
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:<PORT> # replace <PORT> with port number

3). For Flask, update with the following:

# using docker - option: 1
# no need for any command; 
# your application should automatically detect new file changes and the web compose service will restart

# using gunicorn - option 2
source venv/bin/activate
# -----------------------------
# add more commands here;
# run database migrations, etc
# -----------------------------
gunicorn "main:app" -w 4 --threads 4 -b 0.0.0.0:<PORT> # replace <PORT> with port number

@aybruhm
Copy link
Author

aybruhm commented Feb 23, 2023

Where;

  • secrets.SSH_HOST is the IP address of the server you wish to SSH into
  • secrets.SSH_PORT is the port number of the server you wish to SSH into. Default is 22
  • secrets.SSH_KEY is the private key of the server or pem file for ec2 instance
  • secrets.SSH_USERNAME is the user of the server you want to SSH into. Default is ubuntu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment