CircleCI対応のLaravel開発環境を作る方法

CircleCI対応のLaravel開発環境を作る方法

Laravel+Git+CircleCIの開発環境を構築するための方法(ほとんどコマンドの列挙)です!

前提

  • Vim, PHP, MySQL, Apache, Composer, Node.js, npmはインストール済みとします。
  • バーチャルホストでの運用を想定しています。
  • var/www/html/ドメイン名/の下にプロジェクトを作ります。
この記事で利用している検証環境の各バージョン
$ vim --version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Jan 30 2019 13:26:28)

$ php -v
PHP 7.3.1 (cli) (built: Jan  8 2019 13:55:51) ( NTS )

$ mysql --version
mysql  Ver 8.0.14 for Linux on x86_64 (MySQL Community Server - GPL)

$ httpd -v
Server version: Apache/2.4.6 (CentOS)

$ composer --version
Composer version 1.8.3 2019-01-30 08:31:33

$ node --version
v11.9.0

$ npm --version
6.9.2

環境構築

ディレクトリ作成
$ sudo mkdir [ドメイン]
$ sudo chown [作業ユーザ]:[作業ユーザ] /var/www/html/[ドメイン]
$ cd [ドメイン]
Laravelインストール
$ composer global require laravel/installer
パスを通す
$ export PATH=$PATH:$HOME/.config/composer/vendor/bin
Laravelのプロジェクトを新規作成
$ laravel new [プロジェクト名]
未定義ホストのアクセス拒否
$ sudo vi /etc/httpd/conf.d/undefined.conf
<VirtualHost _default_:80>
    ServerName any
    <Location />
        Require all denied
    </Location>
</VirtualHost>
<VirtualHost _default_:443>
    ServerName any
    <Location />
        Require all denied
    </Location>
</VirtualHost>
バーチャルホストの設定
$ sudo vi /etc/httpd/conf.d/[ドメイン].conf
<VirtualHost *:80>
  ServerName [ドメイン]
  DocumentRoot /var/www/html/[ドメイン]/[プロジェクト名]/public
  ErrorLog logs/[ドメイン]-error_log
  CustomLog logs/[ドメイン]-access_log common
</VirtualHost>
<VirtualHost *:443>
  ServerName [ドメイン]
  DocumentRoot /var/www/html/[ドメイン]/[プロジェクト名]/public
  ErrorLog logs/[ドメイン]-ssl_error_log
  TransferLog logs/[ドメイン]-ssl_access_log
</VirtualHost>
再起動
$ httpd -t
Syntax OK
$ sudo systemctl restart httpd
ファイル・ディレクトリの権限設定
$ sudo chown -R [作業ユーザ]:apache /var/www/html/[ドメイン]/プロジェクト名
$ sudo find /var/www/html/[ドメイン]/[プロジェクト名] -type d -exec chmod 750 {} \;
$ sudo find /var/www/html/[ドメイン]/[プロジェクト名] -type f -exec chmod 640 {} \;
$ sudo chmod -R 770 /var/www/html/[ドメイン]/[プロジェクト名]/storage/ /var/www/html/[ドメイン]/[プロジェクト名]/bootstrap/cache/
DB作成
$ mysql -u root -p
mysql> CREATE DATABASE [DB名];
mysql> CREATE USER '[ユーザ名]'@'localhost' IDENTIFIED BY '[パスワード]';
mysql> GRANT ALL PRIVILEGES ON [DB名].* TO '[ユーザ名]'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> SHOW DATABASES;
mysql> SELECT host, user FROM mysql.user;
mysql> exit;
Laravelプロジェクトの環境設定
$ cd /var/www/html/[ドメイン]/[プロジェクト名]
$ vim .env
APP_NAME=[アプリ名]
APP_URL=https://[ドメイン]/

DB_DATABASE=[DB名]
DB_USERNAME=[ユーザ名]
DB_PASSWORD=[パスワード]

$ vim config/app.php
'timezone' => 'Asia/Tokyo',
シンボリックリンク生成
$ php artisan storage:link
PHPUnitの準備
$ composer install
$ ./vendor/bin/phpunit
Gitのリポジトリに登録
$ git init
$ git add .
$ git config --global user.email "[メールアドレス]"
$ git config --global user.name "[名前]"
$ git commit -m 'Laravel導入'
$ git remote add origin [リポジトリURL]
$ git push -u origin master
CircleCI用のDB設定
$ vim config/database.php
'connections' => [
    'circleci' => [
        'driver' => 'mysql',
        'host' => '127.0.0.1',
        'port' => '3306',
        'database' => 'circle_test',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
    ],
],
CircleCIをGitHubのアカウントと連携した上で、ADD Projectをクリック
対象リポジトリのSet Up Projectをクリック
CircleCIの設定ファイル追加
$ mkdir .circleci
$ vim .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.3.1-apache-node-browsers 
      - image: circleci/mysql:8.0.14
        command: mysqld --default-authentication-plugin=mysql_native_password

    environment:
      - APP_DEBUG: true
      - APP_ENV: testing
      - APP_KEY: #任意の値
      - DB_CONNECTION: circleci

    steps:
      - checkout

      - run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
      - run: sudo docker-php-ext-install zip pdo_mysql

      # Download and cache dependencies
      - restore_cache:
          keys:
            # "composer.lock" can be used if it is committed to the repo
            - 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:
          key: v1-dependencies-{{ checksum "composer.json" }}
          paths:
            - ./vendor
      - restore_cache:
          keys:
            - node-v1-{{ checksum "package.json" }}
            - node-v1-
      - run: yarn install
      - save_cache:
          key: node-v1-{{ checksum "package.json" }}
          paths:
            - node_modules

      # prepare the database
      - run: php artisan migrate

      # run tests with phpunit
      - run: ./vendor/bin/phpunit
Gitリポジトリにcommit & push
$ git add .
$ git commit -m ‘CircleCIの設定追加’
$ git push origin master
CircleCIでStart Buildingをクリック

以上です!?

1 Comment

現在コメントは受け付けておりません。