mysqldump misuse of shell builtins error when executing phpunit in github actions

Viewed 883

I am working on deploying my laravel application using github actions and I get the following error during my testing workflow:

Spatie\DbDumper\Exceptions\DumpFailed: The dump process failed with exitcode 2 : Misuse of shell builtins : mysqldump: Got error: 1045: Access denied for user 'alessandro'@'localhost' (using password: YES) when trying to connect

Resulting from test:

public function testDBBackup() {
        $new_file_name = storage_path('db_backup/'.env('DB_DATABASE')).'/'.env('DB_DATABASE')."__".now()->format('Y-m-d_H-i').".sql.gz";

        $this->artisan('db:backup')
            ->expectsOutput('command started')
            ->expectsOutput('command finished')
            ->assertExitCode(0);

        self::assertTrue(file_exists($new_file_name));
    }

Which executes this code:

MySql::create()
                ->setDbName($database)
                ->setUserName(env('DB_USERNAME'))
                ->setPassword(env('DB_PASSWORD'))
                ->setHost('localhost') // makes no difference for error if present
                ->addExtraOption('-u ' . env('DB_USERNAME', 'forge'))  // makes no difference for error if present
                ->useCompressor(new GzipCompressor())
                ->dumpToFile($path.'/'.$database.'__'.$time.'.sql.gz');

Which uses this package: https://github.com/spatie/db-dumper

The error occurs only if I execute this github actions pipeline:

name: Tests
on:
  push:
    branches-ignore: [master]

jobs:
  phpunit:
    name: Run PHP Unit Tests
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: ubuntu
          MYSQL_DATABASE: circle_test
        ports:
          - '8888:3306'
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/checkout@v2

      - name: Configure PHP 8.0
        uses: shivammathur/setup-php@master
        with:
          php-version: 8.0
          extensions: mbstring, ctype, fileinfo, openssl, PDO, bcmath, json, tokenizer, xml

      - name: create test database
        run: |
          mysql --version
          sudo apt-get install -y mysql-client
          sudo systemctl start mysql
          mysql -h 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -u root -pubuntu -e 'CREATE DATABASE IF NOT EXISTS circle_test;'
          mysql -h 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -u root -pubuntu -e 'CREATE USER "alessandro"@"%" IDENTIFIED BY "ubuntu";'
          mysql -h 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -u root -pubuntu -e 'GRANT ALL PRIVILEGES ON * . * TO "alessandro"@"%"; FLUSH PRIVILEGES;'
      - name: Composer install
        env:
          COMPONENTS_AT: ${{ secrets.COMPONENTS_AT }}
        run: |
          composer config -g github-oauth.github.com ${COMPONENTS_AT}
          composer install --no-interaction --prefer-dist

      - name: Copy Enviroment and prepare enviroment
        run: |
          cp .env.testing_online .env
          php artisan key:generate

      - name: Migrate and Seed
        run: |
          php artisan migrate:fresh
          php artisan db:seed

      - name: Run Tests
        run: |
          php artisan config:clear
          vendor/bin/phpunit

On my local machine everything runs fine so I asume it depends on something on github actions mysql configuration which I did not consider. I also tested it with DB user "root". The other tests require a DB connection which works fine.

I already checked: https://github.com/spatie/laravel-backup/issues/172 and https://github.com/spatie/laravel-backup/issues/1035 and removing ~/.my.cnf made no difference.

I have the following database.php configuration:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            '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' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
            'dump' => [
                'dump_command_path' => '/usr/bin',
                'add_extra_option' => '-u ' . env('DB_USERNAME', 'forge'),
            ]
        ],
0 Answers
Related