Search code examples
laravelnginxgitlabcomposer-phplaravel-artisan

Laravel - composer.json file not being deployed when also deploying custom Artisan commands


I have a Laravel application that is is deployed on a test and live server using GitLab pipelines.

I recently decided to make my own Artisan command, which I now want to deploy to the test server, however I've noticed the following error when doing so:

In Application.php line 1258:
                                                                               
  file_get_contents(/data/app/my-app-name/composer.json): failed to open stream: No such file or directory

I have SSH'd into the server and can confirm that the composer.json file is not there, but I don't know why it isn't there.

I have tested the deploy with and without the command, and it works fine without - the only difference between the successful and failed deploys is the addition of one custom artisan command.

In case it's useful, my command is as follows:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Config;
use File;

class Maintenance extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'maintenance:toggle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Toggles a custom maintenance mode on or off';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Get current config file
        $array = Config::get('maintenance');

        // Toggle 'enabled' value
        $array['enabled'] = !$array['enabled'];

        // Get string representation of array
        $data = var_export($array, 1);

        // Overwrite file - this causes change to persist, unlike Config::set()
        if(File::put(config_path() . '/maintenance.php', "<?php\n return $data ;")) {
            $this->info('Maintenance mode ' . (!$array['enabled'] ? 'enabled' : 'disabled') . '!');

            // Clear and re-cache config
            $this->callSilent('config:cache');
        } else {
            $this->error('Failed to modify config value');
        }
    }
}

This command is located in my app/Console/Commands directory, and I haven't changed my app\Console\Kernel.php file, as the Laravel documentation indicates any command in the Commands folder will be automatically imported by Kernel.php using this line, $this->load(__DIR__.'/Commands');

Thanks in advance for any help, and let me know if there's any other info that would be useful.

Cheers

EDIT:

The app is deployed using the following gitlab-ci.yml script:

    - env | grep DOTENV | sed  's/DOTENV_//g' > .env
    - |
      rsync \
        -a -z \
        --include '.env' \
        --include './public/angular' \
        --exclude /.vscode/ \
        --exclude ./angular/ \
        --exclude database/ \
        --exclude docs/ \
        --exclude node_modules/ \
        --exclude /scripts/ \
        --exclude tests/ \
        --exclude '/*.*' \
        ./ \
        $SRV_SSH_USER@$SRV_IP_ADDRESS:$DEPLOY_DIR
    - |
      ssh $SRV_SSH_USER@$SRV_IP_ADDRESS <<EOF
        rm -rf /tmp/$SRV_DEPLOY_DIR
        mv /data/app/$SRV_DEPLOY_DIR /tmp
        chmod 775 -R $DEPLOY_DIR
        chown $SRV_SSH_USER:www-data -R $DEPLOY_DIR
        mv $DEPLOY_DIR /data/app/$SRV_DEPLOY_DIR
        php /data/app/$SRV_DEPLOY_DIR/artisan config:cache
        php /data/app/$SRV_DEPLOY_DIR/artisan view:clear
        php /data/app/$SRV_DEPLOY_DIR/artisan route:clear
        php /data/app/$SRV_DEPLOY_DIR/artisan up
      EOF

I don't see how this script could be causing the issue however, since it works for deployments without this new artisan command.


Solution

  • So I managed to solve the issue. The command was located in the App\Console\Commands directory - I renamed this to App\Console\Custom_commands, and in App\Console\Kernel.php, I manually imported it,

    protected $commands = [
        Maintenance::class
    ];
    

    It also worked if I stored the command in another directory, i.e. the naming I've used here isn't relevant (as long as it wasn't called Commands)

    Honestly, I have no idea why I had to do this to get it to work, so I'm just answering this in case someone else has the same issue.

    Cheers