Use GitHub action as CI for django
When we develop a Django application, we need to test it before deploying it to production. We can use GitHub action as CI (Continuous Integration) to automate the testing process. This post explains how to set up GitHub action as CI for Django.
Django settings file
The settings file should have configuration for database:
1 |
|
Note:
- Django create a test database for testing. The name is “test_” + database name. For example, if the database name is “journalapp”, the test database name is “test_journalapp”.
- It’s better to use root user here. Otherwise, it may have permission error when running tests.
Docker file
When we use GitHub action as CI, we need to create a Docker file to build the image and create environment for testing. Here is an example of a Docker file:
1 |
|
Note: We shouldn’t create test_journalapp database in the MySQL container. Django will create it automatically.
GitHub action
Create a .github/workflows
folder in the root directory of the project. Then create a django.yml
file in this folder:
1 |
|
Note: After running docker, we should wait for MySQL to be ready before running tests. Otherwise, we will have error: MySQLdb.OperationalError: (2013, “Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0”).