Skip to content

Conversation

@Snaver
Copy link

@Snaver Snaver commented Feb 14, 2019

Setup Laravel correctly to run in a production environment on Google App Engine Standard.

Issues:
When deploying using gcloud app deploy PHP composer dependencies should be ignored via .gcloudignore and instead installed on each deploy/build automatically via Google Cloud.

Part of this composer process is the running of the callback post-autoload-dump, which triggers a nifty Laravel feature to autoload dependencies into the App namespace - however this causes issues as the framework expects bootstrap/cache to be writable. In App Engine Standard the only writable directory the app has access to is the /tmp directory.

Laravel actually uses this directory for a few other things (config and route cache), so we need to configure the framework to use a different directory.

Changes:

  • Change cached framework files directory from bootstrap/cache to /tmp.
  • Exclude local bootstrap/cache files from deployment
  • Tweak useStoragePath() call to only be used when ENV var is present

Setup Laravel correctly to run in a production environment on Google App Engine Standard.

Changes:
- Change cached framework files directory from bootstrap/cache to /tmp. This is required as /tmp is the only writable directory in the system
- Exclude local bootstrap/cache files from deployment
- Tweak useStoragePath() call to only be used when ENV var is present
@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.

@Snaver
Copy link
Author

Snaver commented Feb 18, 2019

Hmm during my testing I hard coded the cache path to /tmp - which worked, however this unfortunately cached the results in Cloud Build upon subsequent builds. I changed the code in Laravel to use an Environment var that doesn't appear to exist in the Build process, however my build continue to pass as it was using the cache.. I've just pushed again and the build broke.

I've hacked it temporarily to work in the short term (at the expense of breaking the magic of my local environment, but would be good to figure out a way to set Environment vars dynamically for the Build process???

"scripts": {
    "post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "php -r 'var_dump(@$_ENV);'",
        "php -r 'var_dump(@$_SERVER);'",
        "APP_BOOTSTRAP_PATH=/tmp php artisan package:discover --ansi"
    ],
    "post-create-project-cmd": [
        "php artisan key:generate --ansi"
    ]
}

Copy link
Contributor

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the pull request, but it does not match the tutorial.

Can you explain the problems you're having when you follow the above tutorial and we can attempt to solve them from there? I will look into ignoring the bootstrap/cache/* directory, although that should be getting regenerated in the Cloud Build environment (where the filesystem is still writeable).

Thank you!


# PHP Composer dependencies:
/vendor/
/node_modules/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this line is specific to your personal deployment of Laravel, and so shouldn't be ignored in the example


namespace App;

class Application extends \Illuminate\Foundation\Application
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I follow the tutorial and run laravel new blog, this file is not generated. So I am not sure how this is supposed to fit in the tutorial

APP_SERVICES_CACHE: /tmp/services.php
APP_PACKAGES_CACHE: /tmp/packages.php
APP_CONFIG_CACHE: /tmp/config.php
APP_ROUTES_CACHE: /tmp/routes.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These environment variables are never used

@bshaffer
Copy link
Contributor

I am closing this because it contains fixes that are not accurate. Try following the tutorial on the community site and open an issue if you encounter errors: https://cloud.google.com/community/tutorials/run-laravel-on-appengine-standard

@bshaffer bshaffer closed this Feb 19, 2019
@sertaconay
Copy link

This actually worked. I was getting "/srv/bootstrap/cache directory not writable" error even I copied and pasted everything from the documentation. Thanks!
I use GAE standard and Laravel 5.8.24

@VoordeMensen
Copy link

+1 - after reading https://medium.com/meetech/deploying-a-laravel-app-to-google-app-engine-with-gitlab-ci-2f9dbde5bef4 and adding all the temp env vars to app.yaml I got Bitbucket CI working on GAE.

@uziel-bueno
Copy link

Setting the following environment variables pointing the writable /tmp path allow Laravel to override the default bootstrap/cache path:

env_variables:
  APP_STORAGE: /tmp
  VIEW_COMPILED_PATH: /tmp
  APP_SERVICES_CACHE: /tmp/services.php
  APP_PACKAGES_CACHE: /tmp/packages.php
  APP_CONFIG_CACHE: /tmp/config.php
  APP_ROUTES_CACHE: /tmp/routes.php
  APP_EVENTS_CACHE: /tmp/events.php

As mentioned in the tutorial, an additional line must be added to the bootstrap/app.php file in order to override
the default storage directory path which can be done better in this way instead:

// boostrap/app.php

$app->useStoragePath(env('APP_STORAGE', $app->storagePath()));

I think this is better because there is no hardcoded /storage string. This will be compatible with new versions of
the framework in case the authors decide to internally change the default storage path from /storage to something
else.

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

Successfully merging this pull request may close these issues.

6 participants