Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use FORGE_ENV_KEYS when .env.example contains empty lines #127

Open
Liamso opened this issue Feb 10, 2025 · 6 comments
Open

Unable to use FORGE_ENV_KEYS when .env.example contains empty lines #127

Liamso opened this issue Feb 10, 2025 · 6 comments

Comments

@Liamso
Copy link

Liamso commented Feb 10, 2025

Running harbor with the line

          FORGE_ENV_KEYS: "APP_ENV=staging"

Results in the following:

[2025-02-10 05:55:15] development.ERROR: Unde***ned array key 1 {"exception":"[object] (ErrorException(code: 0): Unde***ned array key 1 at /composer/vendor/mehrancodes/laravel-harbor/app/Actions/MergeEnvironmentVariables.php:59)
[stacktrace]
#0 /composer/vendor/laravel-zero/foundation/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(255): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /composer/vendor/mehrancodes/laravel-harbor/app/Actions/MergeEnvironmentVariables.php(59): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}()
#2 /composer/vendor/mehrancodes/laravel-harbor/app/Actions/MergeEnvironmentVariables.php(31): App\\Actions\\MergeEnvironmentVariables->searchReplaceExistingVariables()
#3 /composer/vendor/lorisleiva/laravel-actions/src/Concerns/AsObject.php(22): App\\Actions\\MergeEnvironmentVariables->handle()
#4 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/UpdateEnvironmentVariables.php(47): App\\Actions\\MergeEnvironmentVariables::run()
#5 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/UpdateEnvironmentVariables.php(36): App\\Services\\Forge\\Pipeline\\UpdateEnvironmentVariables->getBothEnvsMerged()
#6 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\UpdateEnvironmentVariables->__invoke()
#7 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/EnableQuickDeploy.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#8 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\EnableQuickDeploy->__invoke()
#9 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/ObtainLetsEncryptCerti***cation.php(28): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#10 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\ObtainLetsEncryptCerti***cation->__invoke()
#11 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/InstallGitRepository.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#12 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\InstallGitRepository->__invoke()
#13 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/CreateDatabase.php(28): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#14 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\CreateDatabase->__invoke()
#15 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/NginxTemplateSearchReplace.php(31): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#16 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\NginxTemplateSearchReplace->__invoke()
#17 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/OrCreateNewSite.php(35): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#18 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\OrCreateNewSite->__invoke()
#19 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/FindSite.php(32): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#20 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\FindSite->__invoke()
#21 /composer/vendor/mehrancodes/laravel-harbor/app/Services/Forge/Pipeline/FindServer.php(34): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#22 /composer/vendor/illuminate/pipeline/Pipeline.php(184): App\\Services\\Forge\\Pipeline\\FindServer->__invoke()
#23 /composer/vendor/illuminate/pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#[24](https://github.com/Care-Vicinity/care-vicinity/actions/runs/13234483529/job/36936860186#step:4:25) /composer/vendor/mehrancodes/laravel-harbor/app/Commands/ProvisionCommand.php(69): Illuminate\\Pipeline\\Pipeline->then()
#[25](https://github.com/Care-Vicinity/care-vicinity/actions/runs/13234483529/job/36936860186#step:4:26) /composer/vendor/illuminate/container/BoundMethod.php(36): App\\Commands\\ProvisionCommand->handle()
#26 /composer/vendor/illuminate/container/Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#27 /composer/vendor/illuminate/container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#28 /composer/vendor/illuminate/container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod()
#29 /composer/vendor/illuminate/container/Container.php(662): Illuminate\\Container\\BoundMethod::call()
#30 /composer/vendor/illuminate/console/Command.php(211): Illuminate\\Container\\Container->call()
#31 /composer/vendor/symfony/console/Command/Command.php(3[26](https://github.com/Care-Vicinity/care-vicinity/actions/runs/13234483529/job/36936860186#step:4:27)): Illuminate\\Console\\Command->execute()
#32 /composer/vendor/illuminate/console/Command.php(181): Symfony\\Component\\Console\\Command\\Command->run()
#33 /composer/vendor/symfony/console/Application.php(1096): Illuminate\\Console\\Command->run()
#34 /composer/vendor/symfony/console/Application.php(324): Symfony\\Component\\Console\\Application->doRunCommand()
#35 /composer/vendor/symfony/console/Application.php(175): Symfony\\Component\\Console\\Application->doRun()
#36 /composer/vendor/laravel-zero/foundation/src/Illuminate/Foundation/Console/Kernel.php(201): Symfony\\Component\\Console\\Application->run()
#37 /composer/vendor/laravel-zero/framework/src/Kernel.php(97): Illuminate\\Foundation\\Console\\Kernel->handle()
#38 /composer/vendor/mehrancodes/laravel-harbor/harbor(37): LaravelZero\\Framework\\Kernel->handle()
#39 /composer/vendor/bin/harbor(119): include('...')
#40 {main}
"} 
   ErrorException 
  Unde***ned array key 1
  at /composer/vendor/mehrancodes/laravel-harbor/app/Actions/MergeEnvironmentVariables.php:59
     55▕                 $output .= "$variable\n";
     56▕                 continue;
     57▕             }
     58▕ 
  ➜  59▕             [$key, $value] = explode('=', $variable, 2);
     60▕ 
     61▕             // If the key is empty, issue a warning and skip
     62▕             if (empty($key)) {
     63▕                 $this->warning("No key found for the assigned value \"$value\" inside your environment variables! Make sure to remove it.");

Have attempted with multiple var options - this seems like the simplest reproduction step with just a single value in there. It doens't work with the ; delimiter at the end, either.

It appears that this issue only persists when there are empty lines in the .env.example

@mehrancodes
Copy link
Owner

@Liamso, could you try removing FORGE_ENV_KEYS from your Harbor config? If that doesn’t make it work yet, you might have an issue with the .env.example for your custom env keys.

Maybe give Laravel’s default .env.example a shot: https://github.com/laravel/laravel/blob/11.x/.env.example and see if that work.

@Liamso
Copy link
Author

Liamso commented Feb 10, 2025

Thanks a ton @mehrancodes .

Without the FORGE_ENV_KEYS, it does indeed work.

With my FORGE_ENV_KEYS: "APP_ENV=staging"or FORGE_ENV_KEYS: "APP_ENV=staging;", still no dice - even when using the default laravel .env.example.

Thanks for your work on this tool - it's really great.

If I happen to find time in the coming days would you be open to a PR that introduces phpdotenv as the env parser which should mirror what Laravel accepts a little more closely?

@mehrancodes
Copy link
Owner

@Liamso, Could you share a snippet of your GitHub workflow where you’re seeing this error?

Thanks for checking out Harbor - it’s built on Laravel Zero and uses phpdotenv for handling environment variables.

@Liamso
Copy link
Author

Liamso commented Feb 20, 2025

Sure @mehrancodes;

name: preview-provision
on:
  pull_request:
    types: [opened, edited, reopened, ready_for_review]
jobs:
  harbor-provision:
    if: |
      github.event.pull_request.draft == false &&
      contains(github.event.pull_request.title, '[harbor]')
    runs-on: ubuntu-latest
    container:
      image: kirschbaumdevelopment/laravel-test-runner:8.1
    steps:
      - name: Install Harbor via Composer
        run: composer global require mehrancodes/laravel-harbor -q
      - name: Start Provisioning
        env:
          GIT_COMMENT_ENABLED: true
          GIT_TOKEN: ${{ github.token }}
          GIT_ISSUE_NUMBER: ${{ github.event.number }}
          FORGE_DEPLOY_SCRIPT: ${{ secrets.FORGE_DEPLOY_SCRIPT }}
          FORGE_QUICK_DEPLOY: true
          FORGE_SSL_REQUIRED: true
          FORGE_DB_CREATION_REQUIRED: true
          FORGE_PHP_VERSION: php82
          FORGE_TOKEN: ${{ secrets.FORGE_DEPLOY_PREVIEW_TOKEN }}
          FORGE_SERVER: XXXXXX
          FORGE_GIT_REPOSITORY: ${{ github.repository }}
          FORGE_GIT_BRANCH: ${{ github.head_ref }}
          FORGE_DOMAIN: staging.XXXXXXXX.com.au
          FORGE_ENV_KEYS: "APP_ENV=staging;"
        run: harbor provision

@Liamso
Copy link
Author

Liamso commented Feb 23, 2025

Looks like it's empty lines in the .env.example which makes sense when reading

[$key, $value] = explode('=', $variable, 2);
.

Will let you close if you want @mehrancodes - thanks again for the tool.

I'll update the title & description to better reflect the issue I was experiencing.

@Liamso Liamso changed the title Unable to use FORGE_ENV_KEYS Unable to use FORGE_ENV_KEYS when .env.example contains empty lines Feb 23, 2025
@Liamso
Copy link
Author

Liamso commented Feb 26, 2025

huh, this one's cropped up again on one of our PRs, even after removing whitespace. Will let you know if there's anything we identify causing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants