通过Github Actions实现Unity的持续集成构建,便捷构建的同时进行多平台构建。

参考资料:GameCI

总命令

我用的是个人许可证,因此在Github对应仓库的Secrets and Variables > Actions添加以下三个变量:

  • UNITY_LICENSE:Unity的.ulf文件内容
  • UNITY_EMAIL:Unity账号邮箱
  • UNITY_PASSWORD:Unity账号密码

其中UNITY_LICENSE是需要下载Unity Hub并登录创建一个许可证,创建好后将许可证内容作为UNITY_LICENSE的值。其路径为:

  • Windows: C:\ProgramData\Unity\Unity_lic.ulf
  • Mac: /Library/Application Support/Unity/Unity_lic.ulf
  • Linux: ~/.local/share/unity3d/Unity/Unity_lic.ulf

然后在.github/workflows/build.yml创建workflow文件。

name: Build project

on:
push:
branches:
- '**' # 所有分支
pull_request:
branches:
- '**' # 所有分支

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

jobs:
buildForSomePlatforms:
name: Build for ${{ matrix.targetPlatform }} on version ${{ matrix.unityVersion }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
buildName:
- Game
projectPath:
- ./
unityVersion:
- 2021.3.27f1
targetPlatform:
- StandaloneOSX # Build a macOS standalone (Intel 64-bit).
- 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.

steps:
- uses: actions/checkout@v2
with:
lfs: true

# 获取当前分支的提交哈希值
- name: Get Git Commit Hash
id: get_commit_hash
run: echo "::set-output name=hash::$(git rev-parse --short HEAD)"

- uses: actions/cache@v3
with:
path: Library
key: Library-${{ matrix.targetPlatform }}
restore-keys: Library-

- if: matrix.targetPlatform == 'Android'
uses: jlumbroso/free-disk-space@v1.3.1

- uses: game-ci/unity-builder@v4
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
with:
unityVersion: ${{ matrix.unityVersion }}
targetPlatform: ${{ matrix.targetPlatform }}
versioning: Custom
version: ${{ steps.get_commit_hash.outputs.hash }}

- name: Upload Build Artifact
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.buildName }}_${{ matrix.targetPlatform }}
path: build/${{ matrix.targetPlatform }}

上述代码是一个多平台构建的workflow文件,指定了Unity的版本,缓存文件,会构建多平台的Unity项目,在有任意修改提交时触发构建,版本号为提交哈希值。

关于unity-builder参数可参考GameCI Builder

构建平台的取值可参考Unity BuildTarget