Automating Django Deployment with Github Actions

Introduction & Prerequisites
In this guide, we will go through steps to integrate github actions into our production server. In our previous How to Deploy Django to Production blog, we discssed about how can we deploy our django website to production. Everytime we update our code and push it to github, we need those changes to reflect in our production server too. But we need to manually perform those repetitive tasks which is overwhelming. To solve this, we can use github actions which will help us implment continuous delivery on ur project.
We are assuming you already have followed our How to Deploy Django to Production tutorial or you already have a django project deployed on Ubuntu 22.4 server. We assume you have hosted your project on github and aready configured server access to your github repository using our previous tutorial if your project is private. To understand this blog in short time, you will require knowledge on topis like:
- What is github and git
- What is CI/CD (we will discuss here)
- Basic knowlegde of bash scipting is a plus
How this works ?
Github provides us a service named github actions that allows us to automate tasks and workflows directly within your GitHub repository. It works on these steps:
- We create .yaml files inside .github/workflows directory inside our project. These YAML files contain the configuration for our workflows, including the events we want to trigger when deploying and the actions to be performed.
- We trigger the workflow by simply pushing our project repository to github. We can customize it too.
- When the trigger occurs, github automatically starts workflow defined in .yaml file. We will create a pre-defined script that will perform repetative tasks for us.
- The scripts will be used to execut commands inside our production server and once it is done, our server will host a fresh website.
- We can view the status of worflow and actions inside actions section of our github repository.
- Everytime we push our new changes to github or trigger the worflow, same process happens again and again.
Project Configuration
Let's start configuring our project for CI/CD. Follow these steps accordingly to ensure easy and smooth process:
- Go to your project root and open project in VS Code or any editor you prefer.
- Create a folder named
.scripts
inside project root folder. - Inside .scripts folder, create a file named
deploy.sh
file.(You can name<anything>.sh
but I use it for convenience) - Paste this code inside deploy.sh and save file:
#!/bin/bash set -e echo "Deployment started ..." # Pull the latest version of the app echo "Copying New changes...." git pull origin master echo "New changes copied to server !" # Activate Virtual Env source <zvenv>/bin/activate # activate your virtual environment. change zvenv with your venv name echo "Virtual env '<zvenv>' Activated !" echo "Clearing Cache..." python manage.py clean_pyc python manage.py clear_cache echo "Installing Dependencies..." pip install -r requirements.txt --no-input echo "Serving Static Files..." python manage.py collectstatic --noinput echo "Running Database migration..." python manage.py makemigrations python manage.py migrate # Deactivate Virtual Env deactivate echo "Virtual env '<zvenv>' Deactivated !" echo "Reloading App..." ps aux |grep gunicorn |grep <project__name> | awk '{ print $2 }' |xargs kill -HUP # insert project name here. echo "Deployment Finished !"
- Now we need to go to .scripts folder and set file permissions to git. It helps to prevent permissions issue later in server.
$ cd .scripts $ sudo git update-index --add --chmod=+x deploy.sh
- Now we have to create directory named .github inside your root project folder. And then create folder named workflows inside .github. Eg:
zvenv myproject - .scripts/deploy.sh - .github/workflows - manage.py - requirements.txt - myproject - settings.py
-
Inside .github/workflows, create a file named
deploy.yml
. Again strict file name is not necessary but recommended. -
Paste this code inside deploy.yml file and save file:
name: Deploy # Trigger the workflow on push and # pull request events on the master branch on: push: branches: ["master"] pull_request: branches: ["master"] # Authenticate to the the server via ssh # and run our deployment script jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Deploy to Server uses: appleboy/ssh-action@master with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} port: ${{ secrets.PORT }} key: ${{ secrets.SSHKEY }} script: "cd /home/<username>/web/<projectname> && ./.scripts/deploy.sh" # change directory accordingly.
- Note: If you have your project inside
/home/username/demowebsite
then use accordingly. I have assumed that my project is inside/home/user/web/demowebsite
. I use this because I keep zvenv and project folder inside web like:home - username - web - zvenv # my virtual env name - demowebsite # my root project folder - manage.py
Server and Github Setup
Now that we have done essential configuration in our project folder. We will need to do some configurations in Ubuntu server and github too.
Github Setup
- Go to your Github project repository and click on Settings.
- Click on Secrets and Variables from the Sidebar then choose Actions
- On Secret Tab, Click on New Repository Secret
- Add Four Secrets HOST, PORT, USERNAME and SSHKEY as below:
Name: HOST Secret: Your_Server_IP
Name: PORT Secret: Your_Server_PORT #mostly 22
Name: USERNAME Secret: Your_Server_User_Name #use whoami command to get username
- Let's create a ssh key. You can use old but I am creating new for reference. Use:
ssh-keygen -f /home/<username>/.ssh/gitaction_ed2551920 -t ed25519 -C "gitactionautodep" #name can be anything
- Let's copy public SSH keys we created just now and paste it to authorized_keys files. Typically authorized_keys file is inside .ssh/authroized_keys directory :
$ cat ~/.ssh/gitaction_ed25520.pub # again name can be anything you choose instead of gitaction_ed25520 $ cd .ssh $ sudo nano authorized_keys # enter password if it demands password for sudo command
- Now save the file using CTRL + X, then Y, then enter.
- Now we need public key to save as SSHKEY in our github actions secrets. Just copy and paste using:
#copy public key: $ sudo cat ~/.ssh/gitaction_ed25520 #go to your github settings where we left and paste: Name: SSHKEY # name must be SSHKEY or same aswe setup in deploy.yml file Secret: Private_SSH_KEY_Generated_On_Server
- Now just go to VS Code, make a simple change and git push your repository for once. The workflow should trigger and become automatic
Conclusion
In this tutorial we have learned how to automate django deployment and use github actions to achieve continuous delivery of our fresh django website. We have discussed on basic topics that we need to know before achieveing continuous deployment. From next git push, your code will be automatically update on server which will save tons of time.
Hey! Have a complex requirement? BatchNepal is always there to help your business succeed in any market. Just Chat on us live or view our marketplace