With Laravel 6.2, PHP 7.4, on Ubuntu 18 LTS I'm having trouble using env and config in production. Locally, since I do not cache my config, I am able to use env with Console Commands. However in production with a cached config, both env and config return nulls when running a command.
These same variables work when accessed via a web route
$ php artisan config:clear
Configuration cache cleared!
$ php artisan scratch
^ "my-s3-bucket"
^ null
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
$ php artisan scratch
^ null
^ null
$
Here is the command
<?php
namespace App\Console\Commands;
use App\Models\Publication;
use Aws\S3\S3Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Process;
class Scratch extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scratch';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
dump(env('AWS_BUCKET'));
dump(config('AWS_BUCKET'));
}
}
I am using https://deployer.org/ to deploy apps
So the .env file is symlinked to ../../shared/.env. If I cat .env it shows the variables correctly and again the web app works fine, this is limited to only console commands.
It is a fairly simple deploy.php
<?php
namespace Deployer;
require 'recipe/laravel.php';
// Project name
set('application', 'my-app');
// Project repository
set('repository', '[email protected]:my-app/my-app.git');
// tty breaks github actions
set('git_tty', false);
// Shared files/dirs between deploys
add('shared_files', ['.env']);
add('shared_dirs', ['storage']);
// Writable dirs by web server
add('writable_dirs', [
"storage",
"bootstrap/cache",
]);
// Hosts
host('prod.v2.console')
->user('my-app')
->set('deploy_path', '~/{{application}}');
// Tasks
task('build', function () {
// run('cd {{release_path}} && build');
});
// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
// Migrate database before symlink new release.
before('deploy:symlink', 'artisan:migrate');
task('restart supervisor', function() {
run('sudo /etc/init.d/supervisor restart');
});
What steps am I missing or proper way to access an environment var via console?