それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

github actionでテストを自動実行する例

github のActionでテストを実行する例をつくりました。

https://github.com/takuya/github-action-sample-testing-code

テストの設定

github action を実行するには、レポジトリにディレクトリとYAMLを作成します。

## レポジトリの作成
mkdir my-repo
cd my-repo
touch README.md
git init 
git add README.md
git commit -m initial
git remote add origin git@github.com/xxxx/xxxx 
## actions の設定ファイルを設置
mkdir -p .github/workflows/
touch .github/workflows/actions.yml
## push で起動する
git add .github
git push origin master

テスト実行する

アクションの記述例はシンプルにしてあります。

name: build and test

on:
  push:
    branches:
      - master
  
jobs:
  build_and_test:
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: composer install
        shell: bash
        run : |
          composer install  -q --no-ansi --no-interaction --no-scripts --no-progress
          composer dump-autoload
      - name : run
        shell : bash
        run : |
          vendor/bin/phpunit
          

github アクションの限界

action は主に ubuntu-latest のgithub が用意したインスタンスが利用されます。

node や npm/yarn や webdriver などほしいと思うものは一通りインストール済みなので、無理せず既存のインスタンスを活用するのがいいかともう。

自分のdocker イメージを利用するのは少し手間なので、docker イメージを利用するなら circleCIのほうが楽かもしれない。

実行結果バッジ

実行結果バッジは次のようなURLで画像を取得できる。

https://github.com/takuya/cronjob-alternative/workflows/test%20src/badge.svg

URLの構造は次の通り。

https://github.com/takuya/{$レポジトリ名}/workflows/${ジョブ名}/badge.svg

ジョブ名のところには、yaml の最上位で指定した name が入る。

次のようなYAMLの場合は build and testになる。スペースは%20 になる。URLエンコードされる。

name: build and test

on:
  push:
    branches:
      - master
  

github actionsは動作がメチャクチャ速いので活用できる範囲で使っていきたい。

参考資料

https://flutter.takuchalle.dev/posts/how-to-test-on-github-actions/