vincent15000's avatar

Laravel Envoy and environment variables

Hello,

Is there any way to access the environment variables or the configuration files from Envoy.blade.php ?

Thanks for your help.

V

0 likes
5 replies
DoubleClickDesignLtd's avatar

@vincent15000 Yes but with important caveats.

Laravel’s configuration system is the correct layer to expose values to Blade.

create new file in the config folder. For example config/services.php


<?php

return [
    'per_page' => env('PER_PAGE', 15),
];

In your blade you can now do the following

{{ config('services.per_page') }}

DO NOT DO THE FOLLOWING IN YOUR BLADE!!!

{{ env('APP_ENV') }} 

The above is how you do it correctly in Laravel.

However your question was about Envoy and that works differently.

Envoy work differently let me explain

Envoy runs outside the Laravel HTTP runtime, essentially as a standalone PHP script for deployment tasks.

Here, env() works fine because Envoy is not using cached configs.

Envoy reads the .env file directly when it runs, so there’s no “config caching” problem like in Laravel Blade.

For example

@task('deploy')
    echo "{{ env('APP_ENV') }}"
@endtask
1 like
vincent15000's avatar

Envoy reads the .env file directly when it runs, so there’s no “config caching” problem like in Laravel Blade.

I'm not sure that Envoy really reads the .env file.

I already have tested env() inside the Envoy.blade.php file and it doesn't work.

As you mentioned

Envoy runs outside the Laravel HTTP runtime, essentially as a standalone PHP script for deployment tasks.

That's why env() doesn't work, because env() is a Laravel PHP function and not a native PHP function.

But you can use getenv() to retrieve global environment variable and you have to declare these variables for example in your .bashrc file.

Snapey's avatar
Snapey
Best Answer
Level 122

perhaps include the env helper in your envoy script? I have no idea, but if it can use a php library, you should be able to use it the same way the framework uses it.

https://github.com/vlucas/phpdotenv

1 like
DoubleClickDesignLtd's avatar

Sorry I did your last reply before ending for the day. Looking over what I wrote to your post you are correct. Envoy does NOT automatically load your Laravel .env file, and your test result is correct.

I've done it like this in the past in command line

envoy run deploy --env=production --branch=main

Then in your envoy file.

@task('deploy', ['env', 'branch'])
    echo "Deploying {{ $branch }} to {{ $env }}"
@endtask 

This should work for you.

If you export variables in your shell, Envoy can read them I believe you can do something like this below but I've not tried it this way.

export APP_ENV=production
export DEPLOY_PATH=/var/www/app
envoy run deploy
@task('deploy')
    echo "{{ getenv('APP_ENV') }}"
    cd {{ getenv('DEPLOY_PATH') }}
@endtask

Try this and let me know how you get on.

1 like
Glukinho's avatar

Can you somehow instantiate Laravel in your script (like it is done in Laravel's public/index.php and artisan files) and get usable config() function with all features included?

Please or to participate in this conversation.