github twitter email rss
Push to deploy a Python (Django) service via Github Actions
Aug 20, 2020
2 minutes read

After using ploi to deploy a Laravel app in the past I really enjoyed the simplicity of just using a git push to deploy my application to staging/production. Ploi configures your Github repository to use a webook to notify Ploi if a new commit was made. It then pulls the source and deploys it.

While ploi does not (yet) offer to manage python/django applications, I found a few resources where this functionality is solved with Github/Gitlab actions, which I adapted to my needs.

The following steps can be used to deploy a django app via push to a production branch.

Deployment scripts

For deployment, we make use of two scripts. One is responsible for deploying on the server ( while the other is will push the current master branch to production.

set -e

SCRIPT=`realpath $0`

echo "Deploying application ..."

cd $(dirname $SCRIPTPATH)

# Update codebase
git fetch origin production
git reset --hard origin/production

$PIP install -r requirements.txt

# collect static stuff
$PYTHON collectstatic --noinput;

# Migrate database
$PYTHON migrate

systemctl --user restart django_deploy_test

echo "Application deployed!"

This script will

  • use a virtualenv from the $PROJECT/venv folder
  • fetch and reset to the production branch
  • install all requirements
  • call collectstatic and migrate
  • restart the user service

Now let’s have a look at the local

set -e

(git push) || true

git checkout production
git merge master

git push origin production

git checkout master

Push workflow

To trigger the deployment via Github Actions, the following workflow can be used, which takes preconfigured secrets, authenticates over SSH and executes the script.


name: deploy on prod push

    branches: [ production ]

    name: Build
    runs-on: ubuntu-latest
    - name: executing remote ssh commands using password
      uses: appleboy/ssh-action@master
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USER }}
        key: ${{ secrets.PRIVATE_KEY }}
        port: ${{ secrets.PORT }}
        script: |

The secrets can be configured under the Settings tab in a repository:

Setting up the service

The last step is now to configure django with gunicorn as a service and enable it:


Description=django daemon

ExecStart=/home/user/projects/django_deploy_test/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/user/projects/django_deploy_test.sock django_deploy_test.wsgi:application


Enable it via: systemctl --user enable django_deploy_test. Because this is installed as a user service, no sudo is needed for restarting it.

And now we are ready to go. After executing the script you can see the github workflow beeing executed:

Back to posts