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.
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:
master
branch to main
, be sure to account for that.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With