LoginSignup
0
0

More than 1 year has passed since last update.

Google App EngineにLaravel 8をGithub Actionsでデプロイする

Posted at

インスタンスバージョンとLaravelのバージョンアップをしてからしばらく手動アップばっかりやってたのですが、いいかげんCICDを組まなければということで、Github Actionsで組んでみました。まだ途中ですがつまづきポイントを忘れないうちにメモしておきます。
対象読者としては手元のローカル環境でAppEngineにシンプルなPHPアプリ(Hello Worldとか)を手作業でデプロイしたことがあるくらいの方です。

公式の手順がなくなっている

LaravelをGoogle App Engine Standard環境で実行するには、公式?ガイドがありました

https://cloud.google.com/community/tutorials/run-laravel-on-appengine-standard

しかしいまはなくなって別ページにリダイレクトしてしまいます。

そこで、いくつかのサイトをもとにメモしていこうと思います。

環境変数の設定、とにかく/tmpに書き込ませる

App Engineの基本は、とにかく書き込み可能ディレクトリが/tmpのみなので、そこに各種設定を行うことです。

app.yaml
runtime: php81
handlers:
- url: /assets
  static_dir: public/assets
(略)
- url: .*
  script: auto

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

なお、末尾がphpで終わるものも、事前に作成したりはしなくてだいじょうぶです。
また、bootstrap/app.phpの下から2行目にstoragePathの設定を追記します。

$app->useStoragePath(env('APP_STORAGE', base_path() . '/storage')); # ここ
return $app;

livewireを使う場合は、そちらも変更する必要があるのですが、少し面倒です。まず最初にconfigファイルをpublishします。

$ php artisan livewire:publish --config

そして、適当な環境変数を用意し、以下のパスを設定します。

LIVEWIRE_MANIFEST_PATH: /tmp/storage/bootstrap/cache/livewire-components.php

環境変数をconfigに書き込みます。

config/livewire.php
'manifest_path' => env('LIVEWIRE_MANIFEST_PATH'),

composerはApp Engine側でinstallしてくれるのでignoreする

Googleのデプロイ時に無視するファイルを設定できる、.gcloudignoreというファイルがあります。ここに不要なものを設定します。いま自分が使っているサンプルです。node_modulesもいらないの?と思われがちですが、デプロイ時にビルドして必要なファイルはpublic配下に設置しているはずなので、問題ないと思われます。これらをやらないとファイル数制限にひっかかってデプロイできない場合があります。

/node_modules
/storage/debugbar
/storage/framework/cache/data/*
/storage/debugbar
/storage/logs/*
/vendor/
.env
.github
.git
.gitignore
.idea

Github Actionsに落とし込んでいく

本番環境はsecret.yamlを用意してリポジトリにアップし、kms出でコードしたりするのですが、開発用なので横着してGithub Secretにべちゃっと貼り付けます。

deploy.yaml
name: Deploy

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ dev ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  deplay-staging:
    name: GAE deplay
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: "checkout"
      - uses: actions/setup-node@v3
        with:
          node-version: 16
        run: npm install
      - run: npm run production

      - name: Create app_dev.yaml file
        run: echo "${{ secrets.ENV_APPENGINE_YAML_DEV }}" > app_dev.yaml

      - name: Set current datetime as env variable
        env:
          TZ: 'Asia/Tokyo' # 
        run: echo "CURRENT_DATETIME=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV

      - name: Deploy an App Engine app
        id: deploy-app
        uses: google-github-actions/deploy-appengine@v0
        with:
          project_id: ${{ secrets.GCP_DEV_PROJECT_ID }}
          deliverables: app_dev.yaml
          credentials: ${{ secrets.GCP_DEV_SA_KEY }}
          version: v${{ env.CURRENT_DATETIME }}

ここでは、バージョン文字列にYmdHisを設定しています。ENV_APPENGINE_YAML_DEVには手元で管理してる開発用のapp.yamlを丸ごと貼り付けて、それをechoで書きだしています。Github Actionsのログにも残らないので、権限管理さえちゃんとしておけば開発メンバーにも渡すものなのでまぁよいかなと。

GCP_DEV_SA_KEYはサービスアカウントでの認証方式だったのですが、こちらdeplicatedになっているので、時間を見て新しい方法に差し替えたいと思います。

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0