Automating Django Deployments with github actions and docker
In today's fast-paced development landscape, ensuring the seamless integration and deployment of web applications is paramount. By incorporating Continuous Integration (CI) and Continuous Deployment (CD) practices into your workflow, you can automate the testing and deployment processes, streamlining development and improving overall efficiency.
This guide will walk you through the steps to implement a CI/CD pipeline for a Dockerized Django application. From containerizing your Django project to setting up automated testing, and deploying to production, we'll explore best practices to enhance your development workflow.
To successfully follow this guide, ensure that you have a VPS server with Ubuntu (minimum of 2GB RAM) , docker and docker compose installed and with a basic firewall. You should have a github account as well.
If you're interested in letsencrypt with certbot (SSL Certificate), you need to connect your domain name to your server and update the AAAA and A records as well. These are required for certbot to work.
You can go to https://dnschecker.org/ to check dns propergation.
Create Dockerfile, docker-compose.yml, entrypoint.sh and .env file in your project folder
We start by creating Dockerfile, docker-compose.yml and entrypoint.sh file for our configurations.
Paste the code below in your Dockerfile
Paste the code below in entrypoint.sh file
We want to migrate and collectstatic immediately our web service spins up.
Next is to setup the services in our docker-compose.yml file
Paste the code below in docker-compose.yml file
We have web, db and nginx services.
Web service holds code for our web server, db is out database and in this case we're using postgres and finallu nginx services helps us to configure nginx with our web service.
We have three volumes,
1. static which makes our static files persistent
2. certbot which will be used to store our ssl certificate and make it persistent as well
3. postgres_data is used to store details from our postgres database in order to make it persistent
What I mean by persistent in this case is that our data is not wiped off or deleted when docker compose restarts, instead is still saved and not lost.
Create a .env file and save these details there
STEP 2: SETUP NGINX
Create folder called nginx and create two files, Dockerfile and nginx.conf
Paste the code below in the Dockerfile
Paste the code below in the nginx.conf file
STEP 3: SETTING UP GITHUB ACTIONS
Create a folder called .github and in this folder, create another folder called workflows and finally, create a file called docker-image.yml.
Your structure should be like .github/workflows/docker-image.yml
In the docker-image.yml file, paste the code below
Create a repository in github and push your code
STEP 4: SETTING UP ENV VARIABLE WITH GITHUB ACTIONS
In your github repo, go to settings and click on secrets and variables, under the drop down, click on Actions
Under Repository secret, add your secrets
Anything you want to be in your .env file should be added here
STEP 5: SETTING UP RUNNSER WITH GITHUB RUNNSERS ON OUR SERVER]
We have to connect github runners to our ubuntu server so that when there is a push on our main branch, deployment can be automated.
On the settings page, click on Actions on the sidebar and click on Runnners under the dropdown
Click on new self hosted runner.
SSH into your ubuntu server and follow the instructions on the runnser page.
You might face an error when trying ./run.sh:
To fix this, you need to add RUNNER_ALLOW_RUNASROOT=true to your command
Now we want it to take effect immediately,
source ~/.bashrc or
source ~/.bash_profile or
source ~/.bash_profile or
depending on your linux system
When you close your terminal, the session closes, if you want ./run.sh to keep listening, use the command below
STEP 6: PUSH TO GITHUB
Push your code to github and click on Actions tab and monitor your github actions
Monitor the workflow to make sure everything is successfully deployed.
Go to your web page and confirm it has been deployed.
STEP 7: SSL CERTIFICATE WITH CERTBOT
We want to install ssl certificate to make our website secured. We do it with certbot
Open your docker-compose.yml and add certbot service in file
Your code should be like this.
Push your code to github and make sure it deploys.
STEP 8: CONFIGURE NGINX TO HANDLE SSL CERTIFICATE
Add the below code to the end of your nginx/nginx.conf file
My domain name here is scorezone.xyz
STEP 9: PUSH TO GITHUB
Update your current code to github and it should be up. GO to https://yourdomain.com. In my case its https://scorezone.com
Do not make the mistake of pushing the nginx certbot configuration and nginx configuration at the same time.
The server needs to be runnning first before certbot is added. So configure nginx first for the server and upload, then when it is running, add the ssl configuraiton block and upload push again .
In conclusion, automating Django deployments with GitHub Actions and Docker offers a streamlined and efficient workflow for managing and deploying Django applications. By leveraging the power of GitHub Actions, you can automate various tasks such as building Docker images, running tests, and deploying your application to production.