chilio / laravel-dusk-ci

Docker Test suite for Laravel Dusk in gitlab CI
MIT License
160 stars 51 forks source link

Can't connect to DB during script in .yml #24

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hello again,

My build is passing but another error is appearing in the test stage.

I'm using this in .gitlab-ci.yml

variables:
  MYSQL_ROOT_PASSWORD: secret
  MYSQL_DATABASE: testing
  MYSQL_USER: user
  MYSQL_PASSWORD: secret

test:
  stage: test
  services:
    - mysql:5.7

  image: chilio/laravel-dusk-ci:stable
  script:
    - cp .env.dusk.local .env
    - cd ${CI_PROJECT_DIR}
    - chmod -R 775 storage
    - chmod 775 bootstrap/cache
    - chown -R www-data ./
    - php artisan key:generate
    - php artisan migrate:fresh
    - php artisan db:seed -n
    - chrome-system-check
    - sleep 10s
    - start-nginx-ci-project
    - php artisan dusk --colors --debug

But during the CI, the php artisan migrate:fresh is faliing with this error

$ php artisan migrate:fresh

In Connection.php line 664:

  SQLSTATE[HY000] [2002] Connection refused (SQL: SHOW FULL TABLES WHERE tabl  
  e_type = 'BASE TABLE')                                                       

In Connector.php line 68:

  SQLSTATE[HY000] [2002] Connection refused  

But if I connect manually to the docker container. And I run manually the command, everything is working fine concerning the migration & seeding.

Have you any idea why it is not working?

More info

When I connnect to my docker container, I'm also able to run mysql like this

musql -u user -p -h mysql

My database testing exist

chilio commented 6 years ago

cd project dir should be before cp. env....

ghost commented 6 years ago

I changed it but no difference. My .env was present in both situation.

Why is it working manually but not within the script? Do you have any idea how can I debug this?

chilio commented 6 years ago

@Raccoon5031 what do you mean by running manually? Do you have properly set port for mysql connection (env and db config) ?

ghost commented 6 years ago

By manually I mean In my .gitlab-ci.yml I added a `sleep like this

script:
    - cp .env.dusk.local .env
    - cd ${CI_PROJECT_DIR}
    - chmod -R 775 storage
    - chmod 775 bootstrap/cache
    - chown -R www-data ./
    - sleep 1h

So the container is not destroyed if I run my CI. And then I connect to the container by using this (on my machine running docker) docker exec -it [container_id] /bin/bash

And then I cdinto my laravel project (/build/project/my_project)

And If I type by myself the following, it is working

php artisan key:generate
php artisan migrate:fresh
php artisan db:seed -n

The correct database is selected, the tables created and filled by my seeds. No errors. But the script section is doing exactly the same but is failing. Maybe I should put a timer? (Like you did after chrome-system-check)

chilio commented 6 years ago

@Raccoon5031 ok good, you are right, it is strange. Unless .... manually you might be calling some different db like for example the one configured in .env and not the one defined in .env.dusk.local

Try to add to variables:

DB_HOST: mysql
DB_CONNECTION: mysql

BTW Did you try with timer? Which runner executor did you choose while registering runner BTW?

ghost commented 6 years ago

@chilio In the script in copy the .env.dusk.local into .env I tried to add the variables and timer after the migration & seeds but this is failing like before.

I also tried this in the script section

    ...
    - php artisan key:generate
    - cat .env
    - sleep 5h

And the result contain the following information

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=user
DB_PASSWORD=secret

So everything looks fine. But the job is still failing.

Which runner executor did you choose while registering runner BTW? --> I don't really understand the question but I guess the answer is docker

I don't know if it is useful but ... my runner config looks like this

[[runners]]
  name = "my-web-runner"
  url = "http://gitlab.local/"    #This is a bare-metal machine, accessible from internal network
  token = "0axxxxxxxxxxxxxxxxxxxxad"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "chilio/laravel-dusk-ci:stable"
    privileged = true
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
  [runners.cache]
chilio commented 6 years ago

@Raccoon5031 just to double check I would disable cache in runner and also try calling service in this way:

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

and then in variables section:

variables:
  ...
  DB_HOST: mysql-test

To make sure there is no problem with mysql name...

Have you tried to cache config? If so php artisan config:clear should help in script. Finally I would check if there are any custom modifications in config/database.php which could break the flow for example regarding APP_ENV

ghost commented 6 years ago

If I add the alias and change the variable should I also change the DB_HOST of my .env from mysql to mysql-test?

My config is not chached, but I'll add config:clear in the script anyway, I'll see.

Here is my config/database.php, nothing really special here

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'modes' => [
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_AUTO_CREATE_USER',
                'NO_ENGINE_SUBSTITUTION'
            ],
            'engine' => "InnoDB",
        ],

Test

I tried to add the alias but now I have another error


$ php artisan migrate:fresh

In Connection.php line 664:

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name o  
  r service not known (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')  
chilio commented 6 years ago

@Raccoon5031 mystic things are happening here...
Could you post your complete .gitlab-ci.yml? What versions of gitlab, runner, docker, host os are you running?

ghost commented 6 years ago

I removed the MySQL service from the build part

stages:
  - build
  - test

variables:
  MYSQL_ROOT_PASSWORD: secret
  MYSQL_DATABASE: testing
  MYSQL_USER: user
  MYSQL_PASSWORD: secret
  DB_HOST: mysql
  DB_CONNECTION: mysql

build:
  stage: build

  image: chilio/laravel-dusk-ci:stable
  script:
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
    - npm install
    - npm run dev
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - vendor
      - node_modules

test:
  stage: test
  services:
    - mysql:5.7

  image: chilio/laravel-dusk-ci:stable
  script:
    - cd ${CI_PROJECT_DIR}
    - cp .env.dusk.local .env
    - composer update
    - chmod -R 775 storage
    - chmod 775 bootstrap/cache
    - chown -R www-data ./
    - php artisan key:generate
    - php artisan config:clear
    - php artisan migrate:fresh
    - php artisan db:seed -n
    - chrome-system-check
    - sleep 10s
    - start-nginx-ci-project
    - sleep 4h
    - php artisan dusk --colors --debug
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - vendor
      - node_modules
    policy: pull

And now the migration and seeding are fine. That's strange but my DB is not necesarry for the builing part so it's not a big deal for me.

I tried to retry everything from a fresh new machine (re-install docker and so on) but I still have the same problem.

But I'll close this issue anyway, thanks again for your time and patience!