J
J
Jekson2020-03-09 11:42:10
Django
Jekson, 2020-03-09 11:42:10

Setting up CI/CD on Gitlab for django?

I'm trying to add CI/CD for a Django project on gitlab for the first time. I want to set up automatic testing and deploy to the server in the develop branch in case of successful completion.
Almost everything worked out with the tests, the dependencies are installed and launched, python manage.py testbut there is a problem with the database. The error traceback is a little lower, and here I don’t quite understand how the interaction with the database occurs during the tests.

Creating test database for alias 'default'...
 Traceback (most recent call last):
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
     self.connect()
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 195, in connect
     self.connection = self.get_new_connection(conn_params)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
     return Database.connect(**conn_params)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
     return Connection(*args, **kwargs)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
     super(Connection, self).__init__(*args, **kwargs2)
 MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")
 The above exception was the direct cause of the following exception:
 Traceback (most recent call last):
   File "manage.py", line 21, in <module>
     main()
   File "manage.py", line 17, in main
     execute_from_command_line(sys.argv)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
     utility.execute()
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
     super().run_from_argv(argv)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
     output = self.handle(*args, **options)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/core/management/commands/test.py", line 53, in handle
     failures = test_runner.run_tests(test_labels)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/test/runner.py", line 629, in run_tests
     old_config = self.setup_databases(aliases=databases)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/test/runner.py", line 554, in setup_databases
     self.parallel, **kwargs
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/test/utils.py", line 174, in setup_databases
     serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 58, in create_test_db
     self._create_test_db(verbosity, autoclobber, keepdb)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 168, in _create_test_db
     with self._nodb_connection.cursor() as cursor:
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 256, in cursor
     return self._cursor()
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 233, in _cursor
     self.ensure_connection()
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
     self.connect()
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
     self.connect()
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/base/base.py", line 195, in connect
     self.connection = self.get_new_connection(conn_params)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
     return Database.connect(**conn_params)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
     return Connection(*args, **kwargs)
   File "/builds/spend-matters/spend-matters/venv/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
     super(Connection, self).__init__(*args, **kwargs2)
 django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")


In the django settings.py, it connects to the database through such variables in the .env file

SECRET_KEY=ja-t8ihm#h68rtytii5vw67*o8=o)=tmojpov)9)^$h%9#16v&
DEBUG=True
DB_NAME=db_name
DB_USER=username
DB_PASSWORD=dbpass
DB_HOST=127.0.0.1


And with the deployment, everything is still not clear at all. I would be grateful for help in setting up.

gitlab-ci.yml

stages:
  - test
  - deploy

test:
  stage: test
  script:
  - apt update -qy
  - apt install python3 python3-pip virtualenvwrapper -qy
  - virtualenv --python=python3 venv/
  - source venv/bin/activate
  - pip install -r requirements.txt
  - python manage.py test 

stage: deploy
  script:
  ...
  ???
  only:
  - develop

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-03-09
@tumbler

The machine running the gitlab agent needs MySQL. It is usually packaged into a test docker-compose.yml, but you can also just put it on the host machine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question