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?
After reviewing and receiving suggestions from some users in the community (thanks @abulafia @padaleiana @Trauma), it occurred to me to look at something that I should have done much earlier: review the logs from the beginning even though the pipeline is very extensive.
Among so many logs, one of the lines indicated the following:
So it is necessary, of course, to default to the root user's password for the mysql service or allow an empty password.
The only variable needed for the mysql service to start is one of the 3 mentioned above:
My gitlab-ci file would look like this:
And I would export the variable DB_ROOT_PASSWORD with the password inside the configuration: Settings -> CI/CD -> Variables. Thanks to all for the help.