LoginSignup
11
6

More than 5 years have passed since last update.

CircleCI+PHP_CodeSniffer+reviewdogでコードに規律を

Last updated at Posted at 2019-01-13

前提

プロジェクトにPHP_CodeSnifferのインストール

$ composer require --dev squizlabs/php_codesniffer

CIでテストする際にcurlでダウンロードでも良い気がする?
今回はローカルでphpcbfを使いたいときがあるので同梱する

CircleCIの設定

CircleCI > ADD PROJECTS > Set Up Project

設定ファイルの作成・配置

プロジェクト直下に.circleci/config.ymlを作成しpush

.circle/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.1-browsers
        environment:
          REVIEWDOG_VERSION: "0.9.11"

      # 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

    working_directory: ~/repo

    steps:
      - checkout

      # 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 tests!
      - run: curl -fSL https://github.com/haya14busa/reviewdog/releases/download/$REVIEWDOG_VERSION/reviewdog_linux_amd64 -o reviewdog && chmod +x ./reviewdog
      - run: ./vendor/bin/phpcs --warning-severity=0 --standard=PSR2 --report=code --report-width=120 ./src
      - run: |
          ./vendor/bin/phpcs --error-severity=0 --runtime-set ignore_warnings_on_exit 1 --standard=PSR2 --report=emacs ./src | ./reviewdog -efm="%f:%l:%c: %m" -reporter=github-pr-review

Githubアクセストークンの設定

reviewdogにコメントさせるためには権限を持ったGithubアクセストークンを発行する必要があります。

New personal access token > public_repo権限をつけたトークンを発行

  • CircleCI > WORKFLOWS > 対象のブランチの歯車アイコン > Environment Variables > Add Variables
  • REVIEWDOG_GITHUB_API_TOKENの値に先ほど作成したトークンを設定

ビルドするタイミングの設定

デフォルトではPRの有無にかかわらずプッシュ毎にビルドが動いてしまいますが、
reviewdogはPRに対してコメントをつけるツールであるため、PRが存在していないとコメントをつけることが出来ません。

PRが存在するブランチのみビルドする設定へと変更します。

CircleCI > WORKFLOWS > 対象のブランチの歯車アイコン > Advanced Settings > Only build pull requests
Onに設定

(この設定も弊害があるようでアプリを作り対策をしている方もおられるようです。)

参考: CircleCIのOnly build pull requestsをoffにしてもプルリクエストを作ったらJobを実行したい

ブランチの保護設定

このままではCircleCIでエラーが出てもマージできてしまうため、ブランチの保護設定を変更します。

対象のリポジトリ > Settings > Branches > Add rule
で下記のように設定します。

参考:Protected Branches機能で柔軟なワークフローを構築する

スクリーンショット 2019-01-13 22.16.32.png

最後に

テストにひっかかりそうなコードをコミットし、PRを作成します。
CircleCIでビルドされコメントがつき、マージがブロックされているのがわかります。

スクリーンショット 2019-01-13 23.20.03.png

おまけ

機械的に直せるものは直してしまう

$ ./vendor/bin/phpcbf --standard=PSR2 ./src
11
6
4

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