Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPで始めるGitHub Actions

y_ahiru
January 27, 2021

PHPで始めるGitHub Actions

第27回ゆるはち.it で発表したスライドです
https://yuruhachi-it.connpass.com/event/200703/

その他参考リンク
- koriym/Koriym.PhpSkeleton: CI ready PHP project skeleton https://github.com/koriym/Koriym.PhpSkeleton
- dorny/paths-filter: Conditionally run actions based on files modified by PR, feature branch or pushed commits https://github.com/dorny/paths-filter
- mheap/phpunit-matcher-action: Add annotations to your PHPUnit tests when running under Github Actions https://github.com/mheap/phpunit-matcher-action
- shivammathur/setup-php: GitHub action to setup PHP with required extensions, php.ini configuration, code-coverage support and various tools like composer... https://github.com/shivammathur/setup-php
- actions/cache: Cache dependencies and build outputs in GitHub Actions https://github.com/actions/cache
- styfle/cancel-workflow-action: ⏹️ GitHub Action to cancel previous running workflows on push https://github.com/styfle/cancel-workflow-action
- sebastianbergmann/phpunit: The PHP Unit Testing framework. https://github.com/sebastianbergmann/phpunit/
- phpstan/phpstan: PHP Static Analysis Tool - discover bugs in your code without running it! https://github.com/phpstan/phpstan
- vimeo/psalm: A static analysis tool for finding errors in PHP applications https://github.com/vimeo/psalm
- FriendsOfPHP/PHP-CS-Fixer: A tool to automatically fix PHP Coding Standards issues https://github.com/FriendsOfPHP/PHP-CS-Fixer
- infection/infection: AST based PHP Mutation Testing Framework https://github.com/infection/infection
- nektos/act: Run your GitHub Actions locally 🚀 https://github.com/nektos/act

y_ahiru

January 27, 2021
Tweet

More Decks by y_ahiru

Other Decks in Programming

Transcript

  1. PHPで始めるGitHub Actions
    ゆるはち.it #27
    吉⽥あひる (@strtyuu)

    View Slide

  2. 話すこと
    GitHub Actionsのいいところ
    QAツールの活⽤
    よく使うアクションの紹介
    ローカルでGitHub Actionsを実⾏する⽅法

    View Slide

  3. 話さないこと
    GitHub Actionsの使い⽅
    公式ドキュメントがわかりやすいので
    他のCIとの⽐較
    ⽐較できるほど詳しくないので

    View Slide

  4. GitHub Actionsとは
    2019年11⽉にGitHubがリリースしたソフトウェア開発ライフサイクル内のタスクを⾃動化す
    るツール。
    任意のイベントをきっかけにタスクを実⾏することができ、commitがpushされた時などgit
    周りのイベントだけでなく、issueやPRが作成/更新/削除された時やcronによるスケジューリ
    ングなど、様々なイベントに対してタスクを紐づけることができる。
    主にCI/CDによく使われ、似たようなものとしてはCircleCIやTravis CIなどがある。

    View Slide

  5. GitHub Actionsのいいところ
    安い!
    ⼿軽!
    GitHub との連携が充実!
    便利なActionも充実!

    View Slide

  6. 安い!
    従量課⾦だけど無料枠があるので、個⼈使⽤であればだいたい無料枠におさまる。

    View Slide

  7. ⼿軽!
    .github/workflows ディレクトリにymlファイルを置くだけ!
    skeleton projectなどを予め⽤意しておけば、新しいリポジトリを作った直後にはCIの構築が
    終わってるということもありえる

    View Slide

  8. skeleton project?
    プロジェクトのよくあるセッティングを予めしておいたもの。
    参考 https://github.com/koriym/Koriym.PhpSkeleton

    View Slide

  9. (僕は最近こんな感じでPHPプロジェクトを作成していま
    すというデモ)

    View Slide

  10. 僕がよく使うアクションの紹介

    View Slide

  11. shivammathur/setup-php
    マシン上にPHPの動作環境を構築をしてくれるやつ。
    extensionやCIでしか使わないようなツールを簡単に⼊れることができてとても便利。
    - name: Setup PHP
    uses: shivammathur/[email protected]
    with:
    php-version: 8.0
    extensions: mbstring, mysql
    tools: cs2pr

    View Slide

  12. dorny/paths-filter
    gitのdiffを正規表現ベースで簡単に取得できるアクション。
    「差分のあったファイルにだけlintをかけたい」とか on.push.paths
    とかだと対応がちょっ
    と⾯倒な時に便利。

    View Slide

  13. - uses: dorny/[email protected]
    id: changed
    with:
    list-files: shell
    filters: |
    php:
    #
    正規表現が使える
    - '**/*.php'
    without_delete:
    #
    新規作成/
    編集されたファイルのみみたいな指定もできる
    - added|modified: '*.php'
    - name: Run php-cs-fixer
    run: |
    vendor/bin/php-cs-fixer fix \
    --dry-run \
    ${{ steps.changed.outputs.without_delete_files }}

    View Slide

  14. actions/cache
    任意のファイル/ディレクトリをキャッシュして、ジョブ間で使い回しができるやつ。
    CIを⾼速化したいときに役に⽴つ。
    - name: Get Composer Cache Directory
    id: composer-cache-dir
    run: |
    echo "::set-output name=dir::$(composer config cache-files-dir)"
    - name: Restore composer cache
    id: composer-cache
    uses: actions/[email protected]
    with:
    path: ${{ steps.composer-cache-dir.outputs.dir }}
    key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}
    restore-keys: |
    ${{ runner.os }}-composer-

    View Slide

  15. styfle/cancel-workflow-action
    同じPRで連続してpushしたときとかに既に⾛ってるジョブをキャンセルしてくれるやつ。
    ⼀回のCIがとても⻑いなど、CIが終わるのを待つのが⾯倒なプロジェクトとかで便利。
    - name: Cancel previous runs
    uses: styfle/[email protected]
    with:
    access_token: ${{ github.token }}

    View Slide

  16. QAツールとProblem Matcher

    View Slide

  17. Problem
    Matcher とは
    GitHub Actionsで特定
    のフォーマットの⽂字
    列を出⼒すると、発⽣
    したエラーなどがこん
    な感じでみれるように
    なる奴

    View Slide

  18. PHPUnit
    デファクトスタンダードなテスティングフレームワーク
    shivammathur/setup-php
    を使っている場合
    - name: Setup problem matchers for PHPUnit
    run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
    - name: Run PHPUnit tests
    run: vendor/bin/phpunit
    shivammathur/setup-php
    を使っていない場合は mheap/[email protected]
    を使う
    - name: Configure phpunit-matcher
    uses: mheap/[email protected]
    - name: Run PHPUnit tests
    run: vendor/bin/phpunit

    View Slide

  19. PHPStan
    0.12.32 以上の場合
    何もしなくてもOK。明⽰的にやる場合は --error-format=github
    を⾜してあげる
    - name: Run phpstan
    run: vendor/bin/phpstan analyse --error-format=github
    0.12.32 未満の場合
    staabm/annotate-pull-request-from-checkstyle
    を使う
    - name: Run phpstan
    run: vendor/bin/phpstan analyse --error-format=checkstyle --no-progress | cs2pr

    View Slide

  20. psalm
    3.8.4 以上の場合
    --output-format=github
    を⾜してあげる
    - name: Run psalm
    run: vendor/bin/psalm --output-format=github
    3.8.4 未満の場合
    staabm/annotate-pull-request-from-checkstyle
    を使う
    - name: Run psalm
    run: vendor/bin/psalm --output-format=checkstyle --no-progress | cs2pr

    View Slide

  21. infection
    --logger-github
    を⾜してあげる
    - name: Run Infection
    run: vendor/bin/infection --logger-github

    View Slide

  22. php-cs-fixer
    staabm/annotate-pull-request-from-checkstyle
    を使う
    - name: Run php-cs-fixer
    run: vendor/bin/php-cs-fixer fix --dry-run --format=checkstyle | cs2pr

    View Slide

  23. (おまけ)ローカルでGitHub Actionを実⾏する⽅法

    View Slide

  24. nektos/act を使う
    $ brew install act
    $ act pull_request

    View Slide

  25. まとめ
    GitHub Actionsはいいぞ
    Actionという発明は偉⼤
    QAツールを適切に設定してあげることでレビューの体験が向上するかも

    View Slide