@Lepilov

Настройка CI/CD на Gitlab для django?

Впервые пробую добавить CI/CD для проекта на Django на gitlab. Хочу настроить автоматическое тестирование и деплой на сервер в ветку develop в случае успешного выполнения.
С тестами почти все получилось, зависимости устанавливаются и запускается python manage.py test но проблема с БД. Трейсбек ошибки чуть ниже и вот тут я не совсем понимаю как происходит взаимодействие с БД во время тестов.
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)")


В настройках django settings.py к базе конектится через такие переменные в .env файле

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


А с деплоем пока совсем все не понятно. Буду признателен за помощь в настройке.

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
  • Вопрос задан
  • 59 просмотров
Пригласить эксперта
Ответы на вопрос 1
@tumbler
бекенд-разработчик на python
На машине, на которой работает агент гитлаба, нужен MySQL. Обычно он пакуется в тестовый docker-compose.yml, но можно и просто на хост-машину поставить.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы