Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI dusk - MySQL connection refused

This was asked several times now but I tried every solution, I found and have a running "normal" PHPUnit test with a similar setting, so not everything I do can be wrong.

Well here's the problem:
I know that the main (only reported?) cause of this problem in combination with Gitlab CI and docker is using the wrong host. In virtually every case, the OP tried some kind of localhost to access the DB.
This can't be the case here because DB_HOST in my variables, DB_HOST in my .env file and the alias of my mysql service are all the same.

I even hooked into the container (by adding a sleep 1h right before the dusk command and doing a docker exec) and successfully logged into my database and saw the migrated and seeded tables. Tinker works out too.
The only problem is dusk.
Fun fact: dumping my DB_HOST gives mysql-test (I did a config:clear before running dusk and even added it into my setUp function.

I really run out of ideas and am hoping for some ideas from the crowd.

This is my .yml file

stages:
- build
- test  

variables:
  DB_HOST: mysql-test
  MYSQL_DATABASE: laravel
  MYSQL_ROOT_PASSWORD: secret
  DB_CONNECTION: mysql

composer:
  image: lorisleiva/laravel-docker:latest
  stage: build
  script:
  - composer install --no-progress --no-interaction
  - cp .env.gitlab-testing .env
  - php artisan key:generate
  artifacts:
    paths:
    - vendor/
    - bootstrap/
    - .env
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
    - vendor/
  tags:
  - docker

npm:
  image: lorisleiva/laravel-docker:latest
  stage: build
  script:
  - npm install
  - npm run prod
  artifacts:
    paths:
    - public/mix-manifest.json
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
    - node_modules/
  tags:
  - docker  

dusk:
  stage: test
  dependencies:
  - composer
  - npm
  tags:
  - docker
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
    - vendor
    - node_modules
    policy: pull

  services:
  - name: mysql:5.7
    alias: mysql-test

  image: chilio/laravel-dusk-ci:latest
  script:
  - cp .env.dusk.gitlab-testing .env
  - cp phpunit.dusk.xml phpunit.xml 
  - configure-laravel
  - start-nginx-ci-project
  - php artisan dusk --colors --debug

  artifacts:
    paths:
    - ./storage/logs 
    - ./tests/Browser/screenshots
    - ./tests/Browser/console
    expire_in: 7 days
    when: always

.env.dusk.gitlab-testing

APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

LOG_CHANNEL=stack
HEADLESS=true

DB_CONNECTION=mysql
DB_HOST=mysql-test
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync

phpunit.dusk.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Browser Tests">
            <directory suffix="Test.php">./tests/Browser</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="local"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>
like image 812
TimSch Avatar asked Nov 25 '25 08:11

TimSch


1 Answers

It indeed works now.
Here is how my files now look like:
.env.dusk.gitlab-testing:

APP_ENV=local
APP_KEY=base64:WY4y4XWUAKbCPzf8XqC92z5wWMn7oOHXZMFMbg9al3E=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=mysql-test
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync

.gitlab-ci.yml

stages:
- build
- test
- deploy

# Variables
variables:
  DB_HOST: mysql-test
  MYSQL_DATABASE: laravel
  MYSQL_USER: laravel
  MYSQL_PASSWORD: secret
  MYSQL_ROOT_PASSWORD: secretroot
  DB_CONNECTION: mysql

composer:
  image: lorisleiva/laravel-docker:latest
  stage: build
  script:
  - cp .env.gitlab-testing .env
  - composer install --no-progress --no-interaction
  - php artisan key:generate
  artifacts:
    paths:
    - vendor/
    - bootstrap/
    - .env
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
    - vendor/
    - bootstrap/
  tags:
  - docker

npm:
  image: lorisleiva/laravel-docker:latest
  stage: build
  script:
  - npm install
  - npm run prod
  artifacts:
    paths:
    - public/mix-manifest.json
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
    - node_modules/
  tags:
  - docker

phpunit:
  image: lorisleiva/laravel-docker:latest
  stage: test
  services:
  - name: mysql:5.7
    alias: mysql-test
  - redis:latest
  artifacts:
    when: always
    paths:
    - storage/logs/
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
    - vendor/
    - node_modules/
    policy: pull
  dependencies:
  - composer
  - npm
  script:
  - php artisan migrate
  - php artisan db:seed --class=TestingSeeder
  - php vendor/bin/phpunit --coverage-text --colors
  tags:
  - docker

dusk:
  stage: test
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
    - vendor/
    - node_modules/
    policy: pull

  services:
  - name: mysql:5.7
    alias: mysql-test

  image: chilio/laravel-dusk-ci:stable
  tags:
  - docker
  script:
  - cp .env.dusk.gitlab-testing .env
  - configure-laravel
  - composer dump-autoload
  - start-nginx-ci-project
  - php artisan dusk --colors

  artifacts:
    paths:
    - ./storage/logs # for debugging
    - ./tests/Browser/screenshots
    - ./tests/Browser/console
    expire_in: 7 days
    when: always

I suppose, that it was missing the MYSQL_PASSWORD variable in the .yml file but I'm not 100% sure. Just glad that it works now.

like image 65
TimSch Avatar answered Nov 26 '25 23:11

TimSch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!