LoginSignup
13
11

More than 3 years have passed since last update.

LaravelをCircleCI 2.0で自動テストしherokuへ自動デプロイ

Posted at

はじめに

ポートフォリオをLaravelで作成した際に、テストからデプロイまでをCircleCIで自動化したので、まとめてみました。

前提条件

  • herokuアカウントを作成し、Heroku CLIをインストール済み
  • heroku config:setでDBを設定しLaravelアプリが既にherokuにデプロイできる状態になっていること
  • CircleCIとGitHubを連携済み

デプロイまでの流れ

  1. GitHubのリモートリポジトリへpush
  2. pushをフックにCircleCIでマイグレーションとシーディングを実行
  3. テストデータが入った状態で、テストを実行
  4. 問題なければherokuへ自動デプロイ

CircleCI用のデータベース設定を追加

使用するDockerイメージ(https://hub.docker.com/r/circleci/mysql/)
の設定値をLaravelのconfig/database.phpに追記

config/database.php
'connections' => [
        'circle_test' => [
            'driver' => 'mysql',
            'host' => '127.0.0.1',
            'port' => '3306',
            'database' => 'circle_test',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ]
 ]

.circleci/config.ymlを作成

Larevelプロジェクトのルートディレクトリに
.circleci/config.ymlを作成

Laravel-project
  ├ .circleci
  │   └config.yml
  ├ app
  ├ bootstrap
  ├ config
  ├ database
  ・
  ・
  ・

CircleCIでプロジェクトを作る際に出てくるおすすめ設定をベースに
いくつか変更しました。

.circleci/config.yml
# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/php:7.2
      - image: circleci/mysql:5.7

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/mysql:9.4

    environment:
      - APP_DEBUG: true
      - APP_ENV: testing
      - APP_KEY: APP_KEYを入れてください
      - DB_CONNECTION: circle_test
      - MYSQL_ALLOW_EMPTY_PASSWORD: true

    working_directory: ~/repo

    steps:
      - checkout

      # Install PHP Extension
      - run: sudo docker-php-ext-install pdo_mysql

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "composer.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: composer install -n --prefer-dist

      - save_cache:
          paths:
            - ./vendor
          key: v1-dependencies-{{ checksum "composer.json" }}

      # run seeding
      - run: php artisan migrate
      - run: php artisan db:seed

      # run tests!
      - run: php ./vendor/bin/phpunit

      #heroku deploy
      - deploy:
          name: Deploy Master to Heroku
          command: |
            if [ "${CIRCLE_BRANCH}" == "master" ]; then
              git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master
            fi

変更箇所

Dockerイメージを指定

DBにMySQLを使うため、MySQLのDokerイメージを指定

docker:
  - image: circleci/php:7.2
  - image: circleci/mysql:5.7

環境変数を指定

environment:
  - APP_DEBUG: true
  - APP_ENV: testing
  - APP_KEY: APP_KEYを入れてください
  - DB_CONNECTION: circle_test
  - MYSQL_ALLOW_EMPTY_PASSWORD: true

PDO_MYSQLのインストール

DBを使用したテストのためPDO_MYSQLをインストール

# Install PHP Extension
  - run: sudo docker-php-ext-install pdo_mysql

マイグレーションとシーディング

# run seeding
    - run: php artisan migrate
    - run: php artisan db:seed

herokuへデプロイ

#heroku deploy
      - deploy:
          name: Deploy Master to Heroku
          command: |
            if [ "${CIRCLE_BRANCH}" == "master" ]; then
              git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master
            fi

HEROKU_API_KEYとHEROKU_APP_NAMEを設定

herokuへログインしAccount SettingsからAPI Keyをコピー
FireShot Capture 020 - Account - Heroku - dashboard.heroku.com.png

CircleCIでプロジェクト名の設定にAPI Keyをペースト
CircleCI - circleci.com.png

HEROKU_APP_NAMEはherokuのプロジェクト名を入力

GitHubへpush

cd Laravel-project
git init
git remote add origin [GitHubで作成したリポジトリのURL]
git remote -v
でリモートリポジトリを設定

git add .
git commit -m "CircleCI"
git push origin master

実行結果

CircleCIへログインしプロジェクトのjobsがSUCCESSになっていればデプロイ成功
IMG_4260.PNG

13
11
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
13
11