I’ve a self hosted gitlab runner,
I want to run unit tests in runner and I’ve a database container dump in aws s3 that I want to use while running unit tests.
Locally unit-tests and same database setup is working.
so,
- I’m running runner with custom image with php, docker, awscli and other installed
- I’m using service docker:dind to have container where I can load my database image dump
- I downloaded & loaded the db docker image dump to service container
- I run the container
- But I get error as below while connecting to mysql
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
#sectet.yml (php symfony)
database:
main:
dsn: "mysql:host=dev.dev;port=3308;dbname=******"
username: "*****"
password: "*********"
driver_options:
1002: "SET NAMES UTF8"
#.gitlab-ci.yml
default:
image: $CI_REGISTRY/bitcoin/docker-php-builds/php-fpm-ubuntu:8.1
stages:
- install
- lint
- build
- test
variables:
FF_NETWORK_PER_BUILD: true
CI_DEBUG_SERVICES: "true"
CI_DEBUG_TRACE: "true"
MYSQL_USER: "gkrw" # not possible to configure in variable because of length < 8
DOCKER_HOST: tcp://dev.dev:2375
DOCKER_DRIVER: overlay2 #required for DinD to work in GitLab CI
DOCKER_TLS_CERTDIR: ""
services:
- name: docker:dind
alias: dev.dev
cache: &global_cache
key:
files:
- composer.lock
- ${DB_BACKUP_FILE_NAME}
policy: pull-push
install-dependencies:
stage: install
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
before_script:
- apt-get update -yqq && apt-get install -yqq make
- make prepare-db
script: make install-dependencies
cache:
<<: *global_cache
artifacts:
expire_in: 1d
paths:
- vendor
lint-md:
stage: lint
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
script: make lint-md
lint-php:
stage: lint
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
script: make lint-php
build-php:
stage: build
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
script: make build-php
test-db:
stage: test
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
script: make test-db
test-integration:
stage: test
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
script: make test-integration
test-i18nl10n:
stage: test
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
script: make test-i18nl10n
test-unit:
stage: test
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- make test-unit
Makefile
.PHONY: prepare-db install install-dependencies lint lint-md lint-php build build-php test test-db test-integration test-i18nl10n test-unit
#### download dump from S3 and restore it.
prepare-db:
# Download database backup from S3
aws s3 cp ${DB_BACKUP_BUCKET_URL}${DB_BACKUP_FILE_NAME} ./ --only-show-errors;
# Login to Docker registry
docker login ${CI_REGISTRY} -u ${DOCKER_REGISTRY_USER} -p ${DOCKER_REGISTRY_PWD}
# Load the Docker image backup
docker load -i $(DB_BACKUP_FILE_NAME)
# Run container from the loaded image
docker run -d --name=phpdev-db-snap -h "dev.dev" -p 3308:3306 --restart=always -m 8GB --oom-kill-disable IMAGE_NAME
#### install
install: install-dependencies
install-dependencies:
#Install dependencies
composer install --no-progress
docker logs --since=2h phpdev-db-snap
composer init-dev-environment # HERE IT'S FAILING AS I'M CONNECTING TO DATABASE TO MAKE OPERATION
#### lint
lint: lint-md lint-php
lint-md:
@echo lint-md
lint-php:
@echo lint-php
#### build
build: build-php
build-php:
@echo build-php
#### test
test: test-db test-i18nl10n test-unit test-integration
test-db:
@echo test-db
test-integration:
@echo test-integration
test-i18nl10n:
@echo test-i18nl10n
test-unit:
@echo --------- Running unit-tests ---------
vendor/phpunit/phpunit/phpunit
@echo --------- unit-tests finished ---------
config.toml
concurrent = 1
check_interval = 0
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "gitlab-runner"
url = "https://*****.de"
id = 6
token = "****"
token_obtained_at = 2023-12-13T10:20:05Z
token_expires_at = 0001-01-01T00:00:00Z
executor = "docker"
cache_dir = "/cache"
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache","/certs"]
shm_size = 0
network_mtu = 0
[[runners]]
name = "gitlab-runner"
url = "https://******.de"
id = 7
token = "****"
token_obtained_at = 2023-12-13T10:26:46Z
token_expires_at = 0001-01-01T00:00:00Z
executor = "docker"
cache_dir = "/cache"
[runners.cache]
MaxUploadedArchiveSize = 0
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache","/certs"]
shm_size = 0
network_mtu = 0
Tried same code and image dump locally which is working fine.