12
6

More than 1 year has passed since last update.

GitHub Actionsで複数のPHPのバージョンとOSでまとめてPHPUnitを動かしてみる

Posted at

PHPのライブラリなどを開発しているとPHP7.4 / 8.0 / 8.1 on Linux / macOS / Windowsのように複数の環境の組み合わせでテストを実行したい事があると思います。
手元でこれらの環境を作って毎回テストするのはあまりにも効率が悪いので、今回はGitHub Actionsを使って複数の環境でまとめてテストを実行できるようにしてみました。

用意するもの

  • 手元でPHPUnitが動くようになっているプロジェクト
  • GitHubのレポジトリ(publicでもprivateでもよい)
  • composer.jsonが存在してcomposer installで必要なライブラリが揃う状態になっているプロジェクト

GitHub Actionsの設定を入れる

.github/workflows/phpunit.yml に以下のYAMLファイルを配置します。おしまい!

とても簡単ですね。

今回は、Ubuntu(Linux)とWindowsとmacOSでPHP7.4と8.0と8.1を動かす設定を入れてみます。

.github/workflows/phpunit.yml
name: tests
on:
  push:
    branches:
      - main
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
jobs:
  phpunit:
    name: PHPUnit (PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }})
    runs-on: ${{ matrix.operating-system }}
    strategy:
      fail-fast: false
      matrix:
        operating-system: [ ubuntu-latest, windows-latest, macos-latest ]
        php-versions: [ '7.4', '8.0', '8.1' ]
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      # Docs: https://github.com/shivammathur/setup-php
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, dom, fileinfo, simplexml
          coverage: xdebug

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install Composer dependencies
        run: composer install --no-progress --prefer-dist --optimize-autoloader

      - name: Test with PHPUnit
        run: vendor/bin/phpunit --testdox --colors=always

実際に動かしてみると、このような形で実行されます。

image.png

さいごに

Public Repositoryの場合はActionsを無料で使用する事ができますが、Privateの場合は無料枠を超える場合は追加で課金が必要なので注意が必要です。

image.png

https://github.com/settings/billing から見ることが出来るので、使いすぎてないかよく見ておきましょう!(特にこのように複数環境で同時に実行する場合は)

ちなみに、macOSとWindows環境は少々割高(クォータの消費時間はWindowsの場合は2倍で、macOSの場合は10倍でカウントなので)使いすぎには注意が必要です。

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