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

Seems like Forge overrides environment configuration after site has been installed #105

Closed
dees040 opened this issue Feb 15, 2021 · 6 comments
Assignees

Comments

@dees040
Copy link

dees040 commented Feb 15, 2021

  • Forge SDK Version: 3.3.0
  • Laravel Version: 8.14.0
  • PHP Version: 8.0.1
  • Database Driver & Version: mysql Ver 8.0.21 for macos10.15 on x86_64

Description:

I’m automatically creating a site (createSite()), installing git (installGitRepository()), creating a database (createDatabase()) and setting all the environment variables (updateSiteEnvironmentFile()). I perform the API requests in this order as well, but I check if the site status, git status and database status is correct before going to the next step. When coming to the last step I retrieve the default content of the environment file using siteEnvironmentFile(). Then, I change the variables which need to change. At last, I update the env file using updateSiteEnvironmentFile().

Now if I go to the site on Forge and check the Environment tab, I see the original/default content of the env file. It seems/feels like Forge updates the env file one more time with the default content right after everything is installed. A few things which come to mind: maybe I’m updating the .env file too fast or maybe the .env file is not created yet? But this is a wild guess. If I execute the exact same code after (let’s say) 30 seconds, it does work. That’s why I think Forge updates/overrides it.

The code for updating the env file:

protected function updateEnvFile(Site $site)
{
    $env = ForgeManager::api()->siteEnvironmentFile($site->serverId, $site->id);

    dump($env); // First line outputs: APP_NAME={{app_name}}\n

    $attributes = collect([
        'APP_NAME' => $this->catalog->id,
        'APP_ENV' => 'production',
        'APP_DEBUG' => 'true',
        'APP_URL' => "https://{$site->name}",
        'DB_DATABASE' => data_get($this->attributes, 'database.name'),
        'DB_USERNAME' => data_get($this->attributes, 'database.user'),
        'DB_PASSWORD' => data_get($this->attributes, 'database.password'),
    ]);

    foreach ($attributes as $key => $value) {
        $env = preg_replace("/^{$key}=(.*)\n/m", "$key=$value\n", $env);
    }

    ForgeManager::api()->updateSiteEnvironmentFile($site->serverId, $site->id, $env);

    dump($env); // First line outputs: APP_NAME=38\n
}

So if you check what the dump outputs on the last line, it seems like the content which is sent to the API is correct.

Steps To Reproduce:

  1. Create a fresh site via the Forge API
  2. Almost Immediately update the env file after the site has been installed
  3. Check the env file content on Forge
@dees040
Copy link
Author

dees040 commented Feb 15, 2021

Sorry, accidentally pressed enter.. my bad..

@dees040 dees040 changed the title Seems like Forge overrides environment configuration Seems like Forge overrides environment configuration after site has been installed Feb 15, 2021
@dees040
Copy link
Author

dees040 commented Feb 15, 2021

@driesvints I've updated the title and description

@jbrooksuk
Copy link
Member

@dees040 thanks for reporting this.

You're right in your thinking that Forge will automatically create a default .env file once it's finished installing the Git repository. We actually copy the .env.example file if it exists and update some of the settings there if possible.

It's not possible to write the Environment file before the project is installed.

Does this clarify things?

@dees040
Copy link
Author

dees040 commented Feb 15, 2021

@jbrooksuk That clarifies it! Thank you very much. Two questions which pop up for me.

  1. Is it correct that when $site->repositoryStatus has been set to installed (by Forge) it still needs to copy the .env.example file on the server?
  2. What if the .env file already exists before installing the GIT repo? So executing updateSiteEnvironmentFile() before installGitRepository(). Would Forge override the existing .env file?

@jbrooksuk
Copy link
Member

  1. Yes, you should wait for the repository_status to be set to installed before you can manage the Environment yourself.
  2. Any existing .env file will be overwritten.

@cord
Copy link

cord commented Nov 11, 2023

@jbrooksuk this should be mentioned in the documentation!

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

No branches or pull requests

4 participants