firevel/artisan

AppEngine上的无服务器Laravel Artisan

0.1.1 2023-08-15 21:10 UTC

This package is auto-updated.

Last update: 2024-09-16 21:01:31 UTC


README

为运行在App Engine或Google Cloud Run上的Laravel/Firevel提供Artisan支持。可用于远程命令执行或云调度器。

安装

安装包

composer require firevel/artisan

发布配置

php artisan vendor:publish --provider="Firevel\Artisan\ArtisanServiceProvider" --tag="config"

使用方法

使用Google Cloud SDK,您可以从命令行直接执行Artisan命令。请确保您的用户有cloudscheduler.jobs.runappengine.runtimes.actAsAdmin权限。

{command}替换为Artisan命令(例如:route:list),并将{project}替换为您项目的名称,然后可以运行

curl -X POST -d "{command}" -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Accept: application/json" https://{project}.appspot.com/_artisan/call

如果您正在运行多个服务,将{service}替换为您服务的名称并运行

curl -X POST -d "{command}" -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Accept: application/json" https://{service}-dot-{project}.appspot.com/_artisan/call

使用队列

如果您想使用队列来运行命令,您需要首先设置ARTISAN_CONNECTIONARTISAN_QUEUE环境变量。

默认连接设置为cloudtasks,默认队列设置为artisan。如果您想使用默认配置,请确保

要调度命令,请运行

curl -X POST -d "{command}" -H "Authorization: Bearer $(gcloud auth print-access-token)" https://{project}.appspot.com/_artisan/queue

或者

curl -X POST -d "{command}" -H "Authorization: Bearer $(gcloud auth print-access-token)" https://{service}-dot-{project}.appspot.com/_artisan/queue

Google Cloud Scheduler

您可以使用此包通过Cloud Scheduler运行命令。

Cloud Run

通过Google Cloud控制台中的Cloud Scheduler页面添加作业。首先,选择目标类型为HTTP,然后在URL字段中指定https://{APP_URL}/_artisan/call,方法POST,并在Body字段中指定适当的Artisan命令(例如:route:cache)。对于认证头,选择“添加OICD令牌”,对于服务帐户,选择默认App Engine帐户。如果您想使用不同的服务帐户,您需要将服务帐户电子邮件添加到配置文件中的artisan.authorized_service_accounts下。

默认的artisan.authorized_service_accounts

    'authorized_service_accounts' => [
        env('GOOGLE_CLOUD_PROJECT') . '@appspot.gserviceaccount.com',
    ],

如果您使用默认配置,请确保已设置GOOGLE_CLOUD_PROJECT

App Engine

如果您使用App Engine,可以使用标准的cron.yaml文件。

安全性

请求验证基于

  • GAE_SERVICE环境变量和x-appengine-cronx-google-internal-skipadmincheckx-cloudschedulerx-appengine-cron
  • OIDC令牌验证,如果携带令牌是JWT。
  • 否则,它将使用testIamPermissions验证携带令牌

警告

如果您在App Engine之外使用此包,请确保GAE_SERVICE环境变量未设置。