Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Jul 20, 2018 · 10 comments
Closed

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

ghost opened this issue Jul 20, 2018 · 10 comments

Comments

@ghost
Copy link

ghost commented Jul 20, 2018

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
Copy link
Owner

chilio commented Jul 20, 2018

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

@ghost
Copy link
Author

ghost commented Jul 20, 2018

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
Copy link
Owner

chilio commented Jul 20, 2018

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

@ghost
Copy link
Author

ghost commented Jul 23, 2018

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 cd into 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
Copy link
Owner

chilio commented Jul 23, 2018

@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
Copy link
Author

ghost commented Jul 24, 2018

@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
Copy link
Owner

chilio commented Jul 24, 2018

@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
Copy link
Author

ghost commented Jul 25, 2018

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
Copy link
Owner

chilio commented Jul 25, 2018

@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
Copy link
Author

ghost commented Jul 30, 2018

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!

@ghost ghost closed this as completed Jul 30, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant