Automate Your Way to Successful Django Deployments: A Guide to GitHub Actions CI/CD



On Your Local Machine, Open Your Project using VS Code or any Editor
  1. Create A Folder named .scripts inside your root project folder e.g. demo/.scripts.
  2. Inside .scripts folder Create A file with .sh extension e.g. demo/.scripts/
  3. Write the below script inside the created .sh file:
set -e

echo "Deployment started ..."

# Pull the latest version of the app
git pull origin main
echo "New changes copied to server !"

# Activate Virtual Env
source ../venv/bin/activate
echo "Virtual env 'venv' Activated !"

echo "Installing Dependencies..."
pip install -r requirements.txt --no-input

echo "Serving Static Files..."
python collectstatic --noinput

echo "Running Database migration"
python makemigrations
python migrate

# Deactivate Virtual Env
echo "Virtual env 'venv' Deactivated !"

# Reloading Application So New Changes could reflect on website
pushd elitesync

echo "Deployment Finished!"
  1. Go inside the .scripts Folder then Set File Permission for .sh File:
git update-index --add --chmod=+x
  1. Create a Directory Path named .github/workflows inside your root project folder e.g. demo/.github/workflows.
  2. Inside the workflows folder Create A file with a .yml extension e.g. demo/.github/workflows/deploy.yml.
  3. Write the below script inside the created .yml file:
name: Deploy

# Trigger the workflow on push and
# pull request events on the master branch
    branches: ["main"]
    branches: ["main"]

# Authenticate to the server via SSH
# and run our deployment script
    runs-on: ubuntu-latest
      - uses: actions/checkout@v3
      - name: Deploy to Server
        uses: appleboy/ssh-action@master
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          key: ${{ secrets.SSH_KEY }}
          script: "cd /var/www/html/demo && ./.scripts/"
  1. Go to Your Github Repo Click on Settings.
  2. Click on Secrets and Variables from the Sidebar then choose Actions.
  3. On the Secret Tab, Click on New Repository Secret.
  4. Add Four Secrets HOST, PORT, USERNAME, and SSHKEY as below:
    • Name: HOST
      Secret: Your_Server_IP
    • Name: PORT
      Secret: Your_Server_PORT
    • Name: USERNAME
      Secret: Your_Server_User_Name
  5. You can get the Server User Name by logging into your server via ssh and then run the below command:
  1. Generate SSH Key for Github Action by Login into the Remote Server then run the below Command OR You can use the old SSH Key But I am creating a New one for GitHub Action:
Syntax:- ssh-keygen -f key_path -t ed25519 -C ""
Example:- ssh-keygen -f /home/ubuntu/.ssh/gitaction_ed25519 -t ed25519 -C "gitactionautodep"
  1. Open Newly Created Public SSH Keys then copy the key:
cat ~/.ssh/
  1. Open the authorized_keys File which is inside .ssh/authorized_keys then paste the copied key in a new line:
cd .ssh
nano authorized_keys
  1. Open Newly Created Private SSH Keys then copy the key, we will use this key to add a New Repository Secret On the GitHub Repo:
cat ~/.ssh/gitaction_ed44856
  1. Name: SSH_KEY
    Secret: Private_SSH_KEY_Generated_On_Server
  2. Commit and Push the change to Your Github Repo.
  3. Get Access to the Remote Server via SSH:
  1. Go to Your Project Directory:
cd /var/www/html/demo
  1. Pull the changes from GitHub just once this time:
git pull
  1. Your Deployment should become automated.
  2. On Local Machine make some changes in Your Project then Commit and Push to Github Repo. It will automatically be deployed on Live Server.
  3. You can track your action from GitHub Actions Tab.
  4. If you get any File Permission error in the action, then you have to change file permission accordingly.

All Done!