2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UnityとGitHubActionsを使って自動テストを行い、結果をSlackに報告する

Posted at

はじめに

この記事はGitHub Actions上で自動テストを行った後、結果をSlackに報告する方法について解説する記事です。

自動テストの方法自体は以下の記事で解説しています。

経緯

以下の記事を見ながらテストの結果をSlackに報告してみようと思ったのですが、情報が古くて苦戦したので備忘録的に残しておきます。

手順

前提として、自動テストの手順を完了している必要があります。

1. WebhookのURLを生成・取得する

GitHubActionsからSlackにメッセージを送信するために、SlackのIncoming WebhookのURLが必要になります。

以下のページに従い、Webhook URLを作成します。

手順通りに進めていくと、Webhook URLにhttps://hooks.slack.com/services/から始まるURLが生成されているので、この後にそのURLを使用します。

WebhookURL

2. SecretsにIncoming WebhookのURLを登録する

取得したWebhook URLをプロジェクトリポジトリのSettings > Secretsに登録します。

新しいSecretを追加し、NameにSLACK_HOOKと入力し、ValueにWebhookのURLを入力します。

SLACK_HOOK

3. テスト用のYAMLにSlack報告処理を記述する

自動テスト処理を記述したYAMLに、Slack報告処理を追加します。

test.yaml
name: Test

on: [push, pull_request]

jobs:
  test:
    name: ${{ matrix.testMode }} on ${{ matrix.unityVersion }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        projectPath:
          - .
        unityVersion:
          - 2020.3.1f1
        testMode:
          - playmode
          - editmode
    steps:
      # Checkout
      - name: Checkout
        uses: actions/checkout@v2
        with:
          lfs: true

      # Cache
      - name: Cache
        uses: actions/cache@v2
        with:
          path: ${{ matrix.projectPath }}/Library
          key: Library-${{ matrix.projectPath }}
          restore-keys: |
            Library-

      # Tests
      - name: Tests
        uses: game-ci/unity-test-runner@v2
        id: tests
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
        with:
          projectPath: ${{ matrix.projectPath }}
          unityVersion: ${{ matrix.unityVersion }}
          testMode: ${{ matrix.testMode }}
          artifactsPath: ${{ matrix.testMode }}-artifacts
          checkName: ${{ matrix.testMode }} Test Results

      # Upload Artifact
      - name: Upload Artifact
        uses: actions/upload-artifact@v2
        if: always()
        with:
          name: Test results for ${{ matrix.testMode }}
          path: ${{ steps.tests.outputs.artifactsPath }}
          
  reportSlack:
    name: ${{ matrix.testMode }} report
    runs-on: ubuntu-latest
    strategy:
      matrix:
        testMode:
          - playmode
          - editmode
    needs: test
    steps:
    
      # Download Artifact
      - name: Download Artifact
        uses: actions/download-artifact@main
        with:
          name: Test results for ${{ matrix.testMode }}
          path: ${{ matrix.testMode }}-artifacts

      # Clone NUnitXmlReporter
      - name: Clone NUnitXmlReporter
        run: git clone https://github.com/pCYSl5EDgo/NUnitXmlReporter.git

      # Setup .NET
      - name: Setup .NET
        uses: actions/setup-dotnet@v1.7.2
        with:
          dotnet-version: '3.0.100'
      
      # Report Result to Slack
      - name: Report Result to Slack
        env:
          SLACK: ${{ secrets.SLACK_HOOK }}
        run: |
          cd NUnitXmlReporter
          dotnet run ../${{ matrix.testMode }}-artifacts/${{ matrix.testMode }}-results.xml ../slackJson --slack-block  $GITHUB_REPOSITORY $GITHUB_SHA || INPUT_RESULT=$?
          cd ..
          curl -X POST -H 'ContentX-type:application/json' --data "$(cat slackJson)" $SLACK
          exit $INPUT_RESULT

元記事からの変更

  • actions/setup-dotnetのバージョンをv1.0.2からv1.7.2にした。(v1.0.2だとエラーを吐いて動かなかった)
  • パスの記述をこちら側のものに合わせた。

4. テストしてみる

  1. リポジトリにPushまたはPull Requestを行い、テストのワークフローを走らせます。
  2. ワークフローが完了するまで待つ。

以下の画像のように、テストの結果がSlackに報告されていれば成功です。

SlackTestReport

さいごに

最終的に仕上がったリポジトリです。詰まったときは参考にしてみてください。

CIは初めてであまり詳しくないので、間違えているところがあれば教えてもらえると幸いです。

(ワークフローが動くようには書きましたが、いかんせんYAML構文がよく分からず、一部ベタ書きがあるのでYAMLが分かる人がいれば教えてもらえると助かります!)

参考

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?