LoginSignup
3
1

More than 3 years have passed since last update.

GitHub Actions(+Heroku)を使用してLaravelのCI/CD環境を作成する

Last updated at Posted at 2020-04-06

前提条件

  • GitHubにLaravelの環境が設定済み
  • HerokuにLaravelアプリケーションが構築済み

GitHub Actionsの設定を行う

Laravel用のテンプレートがあるのでこれを選んでカスタマイズする。

Image from Gyazo

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側の設定

Image from Gyazo

  • Heroku側の管理画面からApp connected to GitHubでボタンをぽちぽちして、GitHubとの連携をONにする。

Image from Gyazo

  • 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の結果を待つ。

Image from Gyazo

続いてMergeしてHerokuのデプロイ状況が確認できればOK。

Image from Gyazo

Image from Gyazo

参考

Using Github Actions to setup CI/CD with Laravel (and MySQL)

3
1
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
3
1