LoginSignup
3
3

More than 5 years have passed since last update.

LaravelアプリをCircleCIを使ってherokuにデプロイ

Last updated at Posted at 2018-11-06

LaravelのアプリをCircleCIを使って、herokuにアップしたいと思い、
Github→CircleCI→herokuの流れを構築した。

環境

  • git 2.18.0
  • PHP 7.1.16
  • Composer 1.5.6
  • heroku 7.18.3
  • Laravel 5.5

手順

CircleCIの手順を参考に実施

herokuのアプリを作成

herokuの手順を参考にsetupまで完了させておく。

herokuのdashboardでアプリを作成

後で、CircleCIと連携するときに必要な、API Keyとアプリ名をメモする。
API KeyはherokuのAccountから調べられる。

githubのリポジトリとローカル環境にLaravelを構築

github上にリポジトリを作成する
githubにアップするためのアプリをローカル環境に構築

ローカルディレクトリ上でLaravelのプロジェクトを追加
サンプルでは「blog」というプロジェクトができる
最後にAPP_KEYが表示されるのでメモしておく

cd プロジェクトを作りたいディレクトリへ移動
composer create-project --prefer-dist laravel/laravel blog "5.5.*"
Installing laravel/laravel (v5.5.28)
  - Installing laravel/laravel (v5.5.28): Loading from cache
…
…

とりあえず、プロジェクトをGithubにpush

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

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

CircleCI用のconfig.ymlを作成

mkdir .circleci
cd .circleci
vi config.yml

config.ymlの中身は以下の通り

workflowでjobを実行する
jobにはdeploy(と命名したもの)がいて、
それは、githubのmaster branchに更新があった時動く
deployジョブではgithubからソースをチェックアウトして、herokuにデプロイする
HEROKU_API_KEY、HEROKU_APP_NAMEは後で使うのでここでは省略

config.yml
version: 2
jobs:
  deploy:
    docker:
      - image: circleci/php:7.1-browsers
    steps:
      - checkout
      - run:
          name: heroku deploy
          command: |
            git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master
workflows:
  version: 2
  deploy:
    jobs:
      - deploy:
          filters:
            branches:
              only: master

CircleCI関係のソースをgithubにpush

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

herokuでアプリを動かすため、Procfileをプロジェクトの直下に作成

Procfile
web: vendor/bin/heroku-php-apache2 public/
git add .
git commit -m "Procfile"
git push origin master

Github,CircleCI,herokuを繋ぐ

CircleCIにログインして、Organization Settingsを開く(左の方にある歯車のアイコン)
ORGANIZATION→Projectsでgithub,herokuと連携させたいプロジェクトを編集する(右の方にある歯車のアイコン)
BUILD SETTINGS→Environment Variablesを選び、herokuでメモしたAPI KEYとアプリ名を設定する

HEROKU_API_KEYとHEROKU_APP_NAMEというパラメータで変数を設定。
値はherokuで調べたものを使う

Add Project(左にある+のアイコン)を押してgithubのリポジトリとCircleCIを繋ぐ

Laravelアプリをgithub,CircleCIを経由して、herokuに反映

welcomページを編集して、githubにアップ

cd resources/views
vi welcome.blade.php

一部抜粋。82行目を編集。

 80             <div class="content">
 81                 <div class="title m-b-md">
 82                     Hello Laravel
 83                 </div>
 84
 85                 <div class="links">
 86                     <a href="https://laravel.com/docs">Documentation</a>
 87                     <a href="https://laracasts.com">Laracasts</a>
 88                     <a href="https://laravel-news.com">News</a>
 89                     <a href="https://forge.laravel.com">Forge</a>
 90                     <a href="https://github.com/laravel/laravel">GitHub</a>
 91                 </div>
 92             </div>

git add .
git commit -m "Change Welcome Page"
git push origin master

CircleCIのジョブ実行状況を見ると動いている。
SUCCESSとなればOK。

herokuで結果を確認

herokuにログインして、環境変数を設定
APP_KEYはLaravelプロジェクトを作成したときに表示された値を設定する

heroku login

heroku config:set APP_KEY=[値] --app [アプリ名]
heroku config --app [アプリ名]

herokuのアプリを開いて、Hello Laravelのwelcom pageが表示されていれば成功!

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