rverrips / laravel-google-cloud-function-config
Basic Configuration for running Laravel as a native php Google Cloud Function
Requires
- php: ^8.1|^8.0|^7.4
- google/cloud-functions-framework: ^1.1
- laravel/framework: ^9.0|^8.0
This package is auto-updated.
Last update: 2024-04-13 04:08:26 UTC
README
Basic Configuration for running Laravel as a native php Google Cloud Function
Highly inspired by and with special thanks to the Bref Service Provider
Installation
-
For local testing and deployment install Google Cloud SDK
Might be as simple as:
:~$ brew install google-cloud-sdk
-
Locally install new Laravel Application
Should be as simple as:
:~$ cd ~/code :~/code$ laravel new laravel-gcf ##(Installation details here)## :~/code$ cd laravel-gcf :~/code/laravel-gcf$
-
Add this package
:~/code/laravel-gcf$ composer require rverrips/laravel-google-cloud-function-config
-
Publish the assets (new
index.php
+.gcloudignore
):~/code/laravel-gcf$ php artisan vendor:publish tag=laravel-google-cloud-function-config
Running Locally
-
Optional: Add local "start" script to composer.json in root of your project in the "scripts" section
"scripts": { + "start": [ + "Composer\\Config::disableProcessTimeout", + "FUNCTION_TARGET=laravel php -S localhost:${PORT:-8080} vendor/bin/router.php" + ], "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi" ], "post-update-cmd": [ "@php artisan vendor:publish --tag=laravel-assets --ansi" ], "post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], "post-create-project-cmd": [ "@php artisan key:generate --ansi" ] },
-
Run locally with the Google Cloud Function PHP runtime
:~/code/laravel-gcf$ composer start
or if you did not update your composer.json
:~/code/laravel-gcf$ export FUNCTION_TARGET=YOUR_FUNCTION_NAME :~/code/laravel-gcf$ php -S localhost:8080 vendor/bin/router.php
You should see Laravel running on http://localhost:8080 being served by the GCF runtime exactly as it would be in the cloud. (Ctrl-C to end/exit)
Environment Setup
It is highly recommend that you setup an ENV.YML file which will set your runtime environment variables in the Google Cloud (using .env will not suffice). Remember also that the Google Cloud function does not persist any data, so if you require any file cache, session caching to files, logfiles etc. to be shared across deployed functions you will need to map a Google Storage location. (Currently outside the scope of this README.md).
The GoogleCloudFunctionConfigServiceProvider is basically a set of working defaults for GCF - It works, maybe not the best, but it works.
At a minimum it's recommend to set the APP_KEY as a runtime the Environment Variable.
Note that ENV.YML file syntax would be something like APP_KEY: "base64:/abc123....123abcs=" (colon, not equals like in .env)
Deployment
-
Login to Google Cloud SDK:
:~/code/laravel-gcf$ gcloud auth login
(This will open a browser to set the Google account to use)
-
Set the Project to deploy the Cloud Function into:
:~/code/laravel-gcf$ gcloud config set project YOUR_PROJECT_HERE
-
Deploy the Cloud Function
:~/code/laravel-gcf$ gcloud functions deploy YOUR_FUNCTION_HERE --runtime php81 --allow-unauthenticated --trigger-http --env-vars-file env.yml --entry-point=laravel
-
Test Deployment
Deployment should take about 2 minutes.
Assuming no errors, you can now view your laravel app at the specified location in the deployment trigger.
Usually something like https://us-central1-YOUR_PROJECT_HERE.cloudfunctions.net/YOUR_FUNCTION_HERE
Note: This package is still very much Alpha Code / PRs and Updates to the Docs welcome.
License
This package is open-sourced software licensed under the MIT license.