前提条件
- GitHubにLaravelの環境が設定済み
- HerokuにLaravelアプリケーションが構築済み
GitHub Actionsの設定を行う
Laravel用のテンプレートがあるのでこれを選んでカスタマイズする。
example.yml
name: example
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
laravel-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:10.8
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v2
- name: Copy .env
run: cp .env.ci .env
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Execute Migration
run: php artisan migrate --force
- name: Execute Seed
run: php artisan db:seed --force
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
- PostgreSQLのバージョンとかは任意
.env.ci
の作成
.env.ci
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=test
DB_USERNAME=postgres
DB_PASSWORD=postgres
Heroku側の設定
- Heroku側の管理画面から
App connected to GitHub
でボタンをぽちぽちして、GitHubとの連携をONにする。
- Heroku側の管理画面から
Automatic deploys
でボタンをぽちぽちして、GitHubとの連携をONにする。
Procfileの編集
-
Procfile
を編集してマイグレーションとかSeederを自動化する
release: php artisan migrate --force && php artisan db:seed --force
web: vendor/bin/heroku-php-apache2 public/
後はPull Request時にGitHubActionsでTestの結果を待つ。
続いてMergeしてHerokuのデプロイ状況が確認できればOK。
参考
Using Github Actions to setup CI/CD with Laravel (and MySQL)