通过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 - StandaloneWindows64 - StandaloneLinux64 - iOS - Android - 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
用Github Actions实现UnityCI构建