I'm learning to use Gitlab CI and trying to configure the file .gitlab-ci
to run in my Django project.
- I have the following file
.gitlab-ci
(it is an example, it is not complete):
stages:
- test
variables:
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
services:
- mysql:8.0
test:
image: alpine:3.13
stage: test
script:
- ...(up the app)...
- I have set environment variables in Gitlab (Settings -> CI/CD -> Variables), eg:
- And I have the following configuration in the file
settings.py
with the DB section:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': getenv('DB_NAME'),
'USER': getenv('DB_USER'),
'PASSWORD': getenv('DB_PASSWORD'),
'HOST': getenv('DB_HOST'),
'PORT': getenv('DB_PORT'),
'TEST' : {
'NAME' : f"test_{getenv('DB_NAME')}",
}
}
}
When it runs, everything installs correctly but when it gets to the section of running the django tests it throws the following error:
django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'mysql' (-2)")
Why doesn't it connect if the Gitlab documentation indicates that it can be used mysql
as a DB host?