phpro / grumphp

A PHP code-quality tool
MIT License
4.11k stars 429 forks source link

Using Dotenv in yaml config does not provide value of env var #1042

Closed benr77 closed 7 months ago

benr77 commented 1 year ago
Q A
Version 1.13.0
Bug? maybe
New feature? no
Question? yes
Documentation? no
Related tickets #1040

A new issue to try and resolve my question in the final comment of #1040 as it's likely an unrelated issue.

As per your suggestion in https://github.com/phpro/grumphp/issues/1040#issuecomment-1228604767 I am trying to inject the value of an environment variable into the GrumPHP yaml configuration file.

I have encountered a strange issue trying to get this setup. Instead of making the actual value of my GITLAB_TOKEN env var available, I get a strange string which looks like env_e7937a168f382d27_GITLAB_TOKEN_4e06944bcec1f665r4a82bf0125e3f8d.

# .env.local

GITLAB_TOKEN=XXXXXX
# grumphp.yml

grumphp:
    tasks:
        gitlab_lint:
            api_token: '%env(GITLAB_TOKEN)%'
    environment:
        files:
            - .env.local

Any pointers much appreciated. Thank you.

veewee commented 1 year ago

Hello @benr77,

Took some time to like into the issue - but haven't found a solution yet. There are 2 issues:

  1. The env vars from your grumphp config are registered in grumPHP AFTER the container has been built. See https://github.com/phpro/grumphp/blob/f482be4a872078d0e3c0a50a8968976eba4b6bd6/src/Configuration/ContainerFactory.php#L28-L35

  2. Even if I tried registering them before - I get the placeholder. Symfony dependency-injection discovers that we want to use a ENV var: https://github.com/symfony/dependency-injection/blob/b9c797c9d56afc290d4265854bafd01b4e379240/ParameterBag/EnvPlaceholderParameterBag.php#L72 Yet it fails to resolve that - even though the https://github.com/symfony/dependency-injection/blob/6.1/Compiler/RegisterEnvVarProcessorsPass.php goes over it. It seems like the container does not provide any container.env_var_processor by default. Meaning the env var doesn't get resolved into it's corresponding value.

The first issue might something we need to change in our configuration. The second one: I'm not sure.... Maybe you can open up a ticket in symfony to ask what the recommended way is for processing .env vars outside the framework (inside a new container)?

Care to take a further look at this? I currently lack some time ...

Maybe an easier way to test this ;)

HELLO=WORLD
grumphp:
  tasks:
    git_commit_message:
      matchers:
        env matcher: "/%env(string:HELLO)%-\\d+/"
  environment:
    files:
      - .env.local
benr77 commented 1 year ago

Thanks for taking the time to look into this.

I have opened a discussion on Symfony to try and get a solution. https://github.com/symfony/symfony/discussions/47771

benr77 commented 1 year ago

Unfortunately I have not been able to find out any more information from the Symfony community about this.

veewee commented 1 year ago

Then the only way forward is figuring out what is going on in symfony ... It's probably something small or stupid that needs to be activated

veewee commented 7 months ago

@benr77 It should be fixed with #1113.