LoginSignup
3
1

More than 3 years have passed since last update.

How to deploy and migrate Laravel-Admin on ECS

Last updated at Posted at 2019-08-11

I made a management screen using Laravel-Admin.
However, I was a little confused when deploy and migrate to an environment build by ECS.

At local environment

You can easy to run Laravel-Admin on local environment just as written on document.
However, it's a bit different for environment made by ECS.

Answer

To conclude, run composer install and vendor:publish on the Docker file when building.
After that, you can migrate admin and application tables with ecs-cli command.

Deploy

Dockefile
...
RUN composer install \      
    && php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"
...

Then, deploy to production environment.

Migration

After deploying, you use "ecs-cli compose start" command which can execute command like a "heroku run".
ecs-cli compose start

docker-composer.migration.yml
version: '2'                                  
services:                                     
  app:                                        
    command: bash -c "php artisan migrate:refresh --seed && echo 'yes' | php artisan admin:install"

This is actual command we use to migrate.

$ ecs-cli compose \
    --cluster-config app-dev \
    --project-name app-migration-ecs-cli \
    --file docker-compose.development.yml \
    --file docker-compose.migration.yml \
    --ecs-params ecs/ecs-params.development.yml \
        start

Issues I faced

Laravel confirms migration for production environment.

Package manifest generated successfully.
Copied Directory [/vendor/encore/laravel-admin/config] To [/config]
Copied Directory [/vendor/encore/laravel-admin/resources/lang] To [/resources/lang]
Copied Directory [/vendor/encore/laravel-admin/database/migrations] To [/database/migrations]
Copied Directory [/vendor/encore/laravel-admin/resources/assets] To [/public/vendor/laravel-admin]
Publishing complete.
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:
 > 

  Aborted.  

Docker file at that time is here.

Dockerfile
...
RUN composer install \  
    && php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider" \
    && php artisan admin:install
...

Detail

vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php
class MigrateCommand extends BaseCommand {

    use ConfirmableTrait;
vendor/laravel/framework/src/Illuminate/Console/ConfirmableTrait.php
trait ConfirmableTrait {
    //...
    public function confirmToProceed($warning = 'Application In Production!', Closure $callback = null)
    {
        $shouldConfirm = $callback ?: $this->getDefaultConfirmCallback();

        if (call_user_func($shouldConfirm))
        {
            if ($this->option('force')) return true;
            //...
        }
    //...
    }
    //...
    protected function getDefaultConfirmCallback()
    {
        return function() { return $this->getLaravel()->environment() == 'production'; };
    }

Solution

I tried to add "--force" option to command.

php artisan admin:install doesn't have force option.

Package manifest generated successfully.
Copied Directory [/vendor/encore/laravel-admin/config] To [/config]
Copied Directory [/vendor/encore/laravel-admin/resources/lang] To [/resources/lang]
Copied Directory [/vendor/encore/laravel-admin/database/migrations] To [/database/migrations]
Copied Directory [/vendor/encore/laravel-admin/resources/assets] To [/public/vendor/laravel-admin]
Publishing complete.

[2019-08-08 19:53:33] production.ERROR: The "--force" option does not exist. {"exception":"[object] (Symfony\\Component\\Console\\Exception\\RuntimeException(code: 0): The \"--force\" option does not exist. at /var/www/html/vendor/symfony/console/Input/ArgvInput.php:218)

Oops.

Docker file at that time is here.

Dockerfile
...
RUN composer install \  
    && php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider" \
    && php artisan admin:install --force
...

Solution

I tried to say yes using standard output when Laravel confirms.

We can't connect to DB yet when building.

Package manifest generated successfully.
Copied Directory [/vendor/encore/laravel-admin/config] To [/config]
Copied Directory [/vendor/encore/laravel-admin/resources/lang] To [/resources/lang]
Copied Directory [/vendor/encore/laravel-admin/database/migrations] To [/database/migrations]
Copied Directory [/vendor/encore/laravel-admin/resources/assets] To [/public/vendor/laravel-admin]
Publishing complete.
**************************************
*     Application In Production!     *
**************************************

 Do you really wish to run this command? (yes/no) [no]:
 > 
[2019-08-08 20:28:00] production.ERROR: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE') {"exception":"[object] (Illuminate\\Database\\QueryException(code: 2002): SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE') at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\\DBAL\\Driver\\PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection refused at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:3

Docker file at that time is here.

Dockerfile
...
RUN composer install \  
    && php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider" \
    && echo "yes" | php artisan admin:install
...

Solution

I tried to separate timing of execution command building and migration.

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1