shivammathur / setup-php

GitHub action to set up PHP with extensions, php.ini configuration, coverage drivers, and various tools.
https://setup-php.com
MIT License
2.9k stars 337 forks source link

Environment variables not defined / not available in symfony #476

Closed 4d4ch4u32 closed 3 years ago

4d4ch4u32 commented 3 years ago

Describe the bug The workflow runs some checks and tests using this action in version 2. Because other services are used, some environment variables are defined in the github workflow which should override the variables defined in the .env file of Symfony. But the variables defined in the workflow are not available in the Symfony project.

Version

Runners

Operating systems ubuntu-18.04

PHP versions 8.0

To Reproduce In the workflow yaml are some services such as postgres and redis defined. In the .env file of Symfony are the hosts defined with a default value, which are overriden in the workflow. So, for example we have the env var DATABASE_HOST with default value (docker image) postgres in .env and the value localhost in the workflow. Github is showing the correct values of the defined variables.

For debugging, I have added a step php -r 'var_dump($_ENV)' to get the variables from the docker setup which is done some steps before. The array is empty.

When I dump the $_ENV variable directly in a phpunit test (in Symfony), there are only the default variables defined in '.env'.

Expected behavior The environment variables must be available in the php setup and must override the .env-vars with the values defined in the github workflow.

shivammathur commented 3 years ago

@antond1990 $_ENV only has environment variables that are passed to the PHP script, which in the case of laravel or symfony would be from the .envfile. Please use getenv for system environment variables that are set in the workflow.

Ref: 1) https://www.php.net/manual/en/reserved.variables.environment.php 2) https://www.php.net/manual/en/function.getenv.php

4d4ch4u32 commented 3 years ago

That's right, but when I define a variable in the workflow, the variable is disappeared in Symfony, instead of overriden.

Happens when phpunit tests are running in the pipeline.

shivammathur commented 3 years ago

@antond1990 I could not reproduce the issue you are facing. I was able to get the environment variables in a PHPUnit test from the workflow using getenv. Test workflow: https://github.com/shivammathur/test-setup-php/runs/3087279025?check_suite_focus=true#step:13:21

If you can provide a link to your workflow or create a test repo to reproduce the issue, I can have a look.

Edit: Please re-open if you can reproduce the issue.

ramsey commented 1 year ago

I've just run into this problem, as well. It took me by surprise that my environment variables were in $_SERVER but not in $_ENV. This was especially frustrating because vlucas/phpdotenv recommends against using getenv() and putenv().

The reason this happens is because the variables_order setting in setup-php's php.ini is set to GPCS. This excludes setting $_ENV values (i.e., E).

To override this behavior, you can configure setup-php with ini-values: variables_order=EGPCS.

After doing this, $_ENV values are populated, as expected.

For more info, see https://www.php.net/manual/en/ini.core.php#ini.variables-order