Github ActionsでGameCIを使ったUnityのCI/CD環境構築方法 【Unity,Github Actions(GameCI)】

TechTrainさんのほうでゲームの課題?をやらせていただいたときにGithub Actionsを使ったUnityのCI/CDを取り組みました(特に課題内容とかではないです)

Github Actionsを使ったUnityのCI/CDはいろいろ詰まるところがあったので,今後使う方/未来の自分に向けてまとめています

成果

  • UnityTestからのUnityビルドの実行(1枚目画像) : テストが成功しないとビルドが実行されない
  • テスト結果とBuildファイルをアーティストに保存
  • Unity TestのPlayModeTestの実行と結果の表示(2枚目画像)

リポジトリは公開していますので,詳細はリポジトリをご確認ください また,テスト用にシンプルな実装でも同じことを行っています.こちらのリポジトリもご参考ください.

環境

CI/CDの実行の流れ

  1. テストを実行するマシーンの作成
  2. リポジトリをcheckout
  3. テストの実行
  4. テスト結果をアーティストをしてアップロード
  5. ビルドを実行するマシーンの作成
  6. リポジトリをcheckout
  7. cacheの取得(これがないと初回と同じ時間毎回かかるらしい)
  8. ビルドの実行
  9. ビルドファイルをアーティストにアップロード

Unityのシリアルキーを取得する

詳しい説明はこちらの記事を参考にさせていただきました

注意ポイント 公式のライセンスを取得できるサイトからダウンロードできるファイルの中身は全部そのまま Github secretのvalueに入れましょう!

license.unity3d.com

TestをActions上で実行する

Unityのテストを作成する

テストフレームのライブラリを導入

今回はUnityでUIテストを簡単に作成するために,Unity UI Test Automation Frameworkを導入しました 詳細は,こちらで詳しく説明されています

baba-s.hatenablog.com

unityUITestライブラリから,DependencyInjector.csUITest.cs を導入します

テストの作成

テストファイルと asmdefファイルを作成します

実行されるようにymlファイルを設定する

test:
    name: Run EditMode and PlayMode Test
    runs-on: ubuntu-latest
    steps:
      - name: Check out my unity project.
        uses: actions/checkout@v2
      - name: Run EditMode and PlayMode Test
        uses: game-ci/unity-test-runner@v2
        with:
          projectPath: .
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          unityVersion: 2020.3.9f1
      # テストの実行結果をアーティファクトにアップロードして後から参照可能にする
      - uses: actions/upload-artifact@v2
        if: always()
        with:
          name: Test results
          path: artifacts

テストの結果を保存/簡単に確認できるようにする

テスト結果の表示

githubToken: ${{ secrets.GITHUB_TOKEN }} を設定すると,以下のようにテストの結果が簡単に見れます. 詳細はこちらに記載されています

テスト結果の保存

- uses: actions/upload-artifact@v2
        if: always()
        with:
          name: Test results
          path: artifacts

を入れることで,テスト結果をアーティストとして保存することができます

BuildをActions上で実行する

ビルドのプラットフォームは,以下のものが設定できるみたいです(公式サイトより)

targetPlatform:
          - StandaloneOSX # Build a macOS standalone (Intel 64-bit).
          - StandaloneWindows # Build a Windows standalone.
          - StandaloneWindows64 # Build a Windows 64-bit standalone.
          - StandaloneLinux64 # Build a Linux 64-bit standalone.
          - iOS # Build an iOS player.
          - Android # Build an Android .apk standalone app.
          - WebGL # WebGL.

ビルドの設定は以下になります.

build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        projectPath:
          - .
        unityVersion:
          - 2020.3.9f1
        targetPlatform:
         - Android # Build an Android player.
    needs: test
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        lfs: false
        clean: false
        
    # Cache
    - uses: actions/cache@v2
      with:
        path: Library
        key: Library

    # Build
    - name: Build project
      uses: game-ci/unity-builder@v2.0-alpha-6
      with:
        unityVersion: ${{ matrix.unityVersion }}
        targetPlatform: ${{ matrix.targetPlatform }}

結論

game-ciのライブラリを使うことですごく楽にUnityのCI/CDを構築できることができました~ 他にもいろいろ機能があるみたいなので,使っていきたいです

ymlファイル全体の構成

# This is a basic workflow to help you get started with Actions

name: Test and Build,Release APK

env:
  UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}

# Controls when the action will run. 
on: [push]
  # Triggers the workflow on push or pull request events but only for the main branch

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:  
  test:
    name: Run EditMode and PlayMode Test
    runs-on: ubuntu-latest
    steps:
      - name: Check out my unity project.
        uses: actions/checkout@v2
      - name: Run EditMode and PlayMode Test
        uses: game-ci/unity-test-runner@v2
        with:
          projectPath: .
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          unityVersion: 2020.3.9f1
      # テストの実行結果をアーティファクトにアップロードして後から参照可能にする
      - uses: actions/upload-artifact@v2
        if: always()
        with:
          name: Test results
          path: artifacts
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        projectPath:
          - .
        unityVersion:
          - 2020.3.9f1
        targetPlatform:
         - Android # Build an Android player.
    needs: test
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        lfs: false
        clean: false
        
    # Cache
    - uses: actions/cache@v2
      with:
        path: Library
        key: Library

    # Build
    - name: Build project
      uses: game-ci/unity-builder@v2.0-alpha-6
      with:
        unityVersion: ${{ matrix.unityVersion }}
        targetPlatform: ${{ matrix.targetPlatform }}

    # Output
    - uses: actions/upload-artifact@v2
      with:
        name: Build-${{ matrix.targetPlatform }}
        path: build/${{ matrix.targetPlatform }}