1

My gitlab ci pipeline keeps failing. It seems am stuck here. Am actually still new to the CI thing so I don't know what am doing wrong. Any help will be appreciated Below is .gitlab-ci.yml file

image: python:latest

services:
  - postgres:latest

variables:
  POSTGRES_DB: thorprojectdb
  POSTGRES_PASSWORD: $''
  POSTGRES_USER: $majesty
  POSTGRES_HOST_AUTH_METHOD: trust

# This folder is cached between builds
# http://docs.gitlab.com/ee/ci/yaml/README.html#cache
cache:
  paths:
    - ~/.cache/pip/

before_script:
  - python -V

connect:
  image: postgres
  script:
  # official way to provide password to psql: http://www.postgresql.org/docs/9.3/static/libpq-envars.html
  - export PGPASSWORD=$POSTGRES_PASSWORD
  - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"

build:
  stage: build
  script:
  - pip install -r requirements.txt
  - python manage.py migrate
  only:
    - EC-30

In my settings.py file, I have the following settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'projectdb',
        'HOST': 'postgres',
        'PASSWORD': ''
    }
}

But when I push to gitlab, the build process keeps failing. The - pip install -r requirements.txt runs perfectly but once it gets to - python manage.py migrate, it fails. Below is the error I do get

django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1
Sammy
  • 311
  • 2
  • 9

1 Answers1

1

Analyzing the description of the .gitlab-ci.yml file, it is clear that you declared the database in the file (POSTGRES_DB), but you are missing information related to the credentials, DB_USER, DB_PASS, as described at this link below:

gitlab-config-postgres

Remembering that it's good practice use the declaring variables at CI/CD section at your repository. For more information:

ci-cd-gitlab-variables

Repository with a config example:

repository-example-gitlab-postgres

services:
  - postgres

variables:
  # Configure postgres service (https://hub.docker.com/_/postgres/)
  POSTGRES_DB: custom_db
  POSTGRES_USER: custom_user
  POSTGRES_PASSWORD: custom_pass

connect:
  image: postgres
  script:
  # official way to provide password to psql: http://www.postgresql.org/docs/9.3/static/libpq-envars.html
  - export PGPASSWORD=$POSTGRES_PASSWORD
  - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
marcosnasp
  • 43
  • 6
  • I just edited the question to include some lines. Can help me check for what I'm not doing right because am getting the following error `django.db.utils.OperationalError: FATAL: role "root" does not exist`. Please @marcosnasp – Sammy Oct 19 '21 at 21:39
  • @Sammy Please, check the link: https://stackoverflow.com/questions/30273347/how-to-solve-running-sudo-on-manage-py-causing-a-failure-to-connect-to-psql. If you not specify the user in DATABASE dictionary block, the root user will be used. – marcosnasp Oct 19 '21 at 21:53
  • After specifying the user, I started having the following error. `django.db.utils.OperationalError: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? Cleaning up project directory and file based variables ERROR: Job failed: exit code 1` – Sammy Oct 20 '21 at 14:40
  • @Sammy Try run the commands, starting and enabling the postgres, you should put this on section script, check this links: https://forum.gitlab.com/t/unable-to-connect-to-postgres-inside-container/14844/3 https://stackoverflow.com/questions/35455109/cant-run-the-server-on-django-connection-refused/35455323 https://docs.gitlab.com/ee/ci/services/index.html#accessing-the-services – marcosnasp Oct 20 '21 at 18:51