Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github ssh-action configuration to deploy changes in django application

I have set up a workflow to execute a script which essentially just makemigrations, migrates and runs collectstic before restartting gunicorn and reloading nginx.

I have configured my settings.py file to pick up the secret and some other variables from the environment. The problem is though, that the script executes successfully when I manually ssh into the server and run it whereas when doing the same via ssh-action, it throws an error

My script

# Cd into the required directory
cd myproject/

# Pull the changes
git pull

# Makemigrations and migrate
myenv/bin/python manage.py makemigrations
myenv/bin/python manage.py migrate

# Collectstatic
myenv/bin/python manage.py collectstatic --noinput

# Restart gunicorn and reload nginx
systemctl restart gunicorn
systemctl reload nginx

My action config

name: deploying changes
on:
  push:
    branches: [main]

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: deploying changes
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.KEY }}
          script: |
            sh deploy_changes.sh

This successfully connects to the server but following is the error thrown when it tries to execute the makemigrations and migrate command

err:     raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
err: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Also, I have a .env file on server and I use the python-dotenv package to load the environment variables in the settings.py file

from dotenv import load_dotenv
load_dotenv(verbose=True)

Please help with the configuration. Thanks in advance.

like image 531
Amartya Gaur Avatar asked Sep 17 '25 02:09

Amartya Gaur


1 Answers

I solved it by using the following:


name: deploying changes
on:
  push:
    branches: [main]

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: deploying changes
        uses: appleboy/ssh-action@master
        env:
          DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }}
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.KEY }}
          envs: DJANGO_SECRET_KEY
          script: |
            export DJANGO_SECRET_KEY=$DJANGO_SECRET_KEY
            sh deploy_changes.sh
            ls -al

The important points here are:

  • You can manually export all the required environment variables by first supplying them in the env part of your configuration.
  • Github has renamed the master branch to main, be sure to account for that.
like image 183
Amartya Gaur Avatar answered Sep 19 '25 16:09

Amartya Gaur