我一直试图让GitLab CI运行一个构建Unity Player的shell文件.它在我做的时候有效,但在GitLab



问题

我用了一个教程来创建一个shell文件,为我构建一个Unity播放器。但当我试图让GitLab的CI调用这个shell脚本时,它似乎只是清理目录,而忽略了Unity构建命令。控制台输出

我的.yml文件看起来是这样的:(重要的是构建工作:部分所有其他东西都可以工作(

stages:          # List of stages for jobs, and their order of execution
- build
- test
build-job:       # This job runs in the build stage, which runs first.
stage: build
script:
- echo "Compiling the code..."
- ./Build/build.sh
- echo "Compile complete."
unit-test-job:   # This job runs in the test stage.
stage: test    # It only starts when the job in the build stage completes successfully.
script:
- echo "Running unit tests... This will take about 60 seconds."
- C:/"Program Files"/Unity/Hub/Editor/2020.3.20f1/Editor/Unity.exe 
-batchmode 
-projectPath C:/Users/joshu/Desktop/Gamedev/Testing/testing-asignment-2/"Testing_Asignment 2" 
-runTests -testPlatform editmode 
-logFile . 
-testResults ./unit-tests.xml 
-quit
- echo "Code is tested"
lint-test-job:   # This job also runs in the test stage.
stage: test    # It can run at the same time as unit-test-job (in parallel).
script:
- echo "Linting code... This will take about 10 seconds."
- echo "No lint issues found."

我的shell脚本包含以下内容:

echo Cleaning Up Build Folder
rm -rf ./Build/Builds
echo Starting Build Process
C:/"Program Files"/Unity/Hub/Editor/2020.3.20f1/Editor/Unity.exe -quit -projectPath ../"Testing Asignment 2" -executeMethod Building.MyBuildScript.PreformBuild
echo Ended Build Process

我的构建脚本包含以下内容:

using System;
using System.IO;
using UnityEditor;
using UnityEditor.Build.Reporting;
namespace Building
{
public class MyBuildScript
{
public static void PreformBuild()
{
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = new[] { "Assets/Scenes/SampleScene.unity" };
buildPlayerOptions.locationPathName = "../Build/Builds/WindowsBuild/Windows64Build.x86_64";
buildPlayerOptions.target = BuildTarget.StandaloneWindows64;
buildPlayerOptions.options = BuildOptions.None;
BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
using StreamWriter writer = File.CreateText("../Build/Builds/WindowsBuild/results.txt");
writer.Write(
$"Build result: {report.summary.result} n" +
$"Process time: n" +
$"              start: {report.summary.buildStartedAt} n" +
$"              end: {report.summary.buildEndedAt} n" +
$"              total: {report.summary.totalTime} n" +
$"{report.summary.totalErrors} Errors found{(report.summary.totalErrors > 0 ? "!" : "")}");
}
}
}

我认为我的Unity.exe的访问有一些问题,但当我打开windows上的安全设置时,会启用写入/执行过量。

我尝试过的东西

我还试着不使用shell脚本,而是直接从.yml调用MyBuildScript。

build-job:       # This job runs in the build stage, which runs first.
stage: build
script:
- echo "Compiling the code..."
- C:/"Program Files"/Unity/Hub/Editor/2020.3.20f1/Editor/Unity.exe -quit -projectPath ./"Testing Asignment 2" -executeMethod Building.MyBuildScript.PreformBuild
- echo "Compile complete."

但这似乎也没有执行Unity,只是跳过了命令。

此外,我尝试使用-buildWindows64Player而不是-executeMethod。

build-job:       # This job runs in the build stage, which runs first.
stage: build
script:
- echo "Compiling the code..."
- C:/"Program Files"/Unity/Hub/Editor/2020.3.20f1/Editor/Unity.exe -quit -projectPath ./"Testing Asignment 2" -buildWindows64Player ./Build/Builds/WindowsBuild
- echo "Compile complete."

但这似乎也跳过了团结。然而,这种尝试确实花了最长的时间才完成。所有其他选项都在几秒钟内完成,但这样做大约需要一两分钟。我不知道为什么,但如果非要我猜的话,那是因为它确实创办了Unity,但在途中的某个地方失败了。

备注

我知道我应该在shell命令中添加-batchmode,但首先我想看到Unity打开,这样我就知道它在做什么。

截至12-29-2021 23:48,几乎没有更新。我注意到单元测试也不起作用,但我通过将Unity版本更改为2021.2.7f1、更改Unity项目文件夹结构并将单元测试部分更新为以下内容来解决这个问题:

unit-test-job:   # This job runs in the test stage.
stage: test    # It only starts when the job in the build stage completes successfully.
script:
- echo "Running unit tests... This will take about 60 seconds."
- C:/"Program Files"/Unity/Hub/Editor/2021.2.7f1/Editor/Unity.exe -batchmode -projectPath C:/Users/joshu/Desktop/Gamedev/Testing/testing-asignment-2/"Testing Asignment 2" -runTests -testPlatfrom editmode -logFile -testResults ./unit-tests.xml | Out-Default

事实证明我做了一些愚蠢的事情。我在笔记本电脑上安装了我的runner,并不断尝试从我的电脑上推送新的更改。然而,我使用的是指向笔记本电脑上我的本地项目的projectPath,而不是runner从GitLab克隆的项目。因此-projectPath现在指向/"测试组件2";。这也是我认为更改Unity版本有帮助的原因,因为我在安装新版本时将该项目拉到了我的笔记本电脑上。

我还发现我的测试需要有"|输出默认值";以实际读取测试数据。有助于此的StackOverflow页面

我现在真的不再使用shell脚本了,但我只是想让它构建Unity,这很管用!我现在使用的.yml文件如下所示:

stages:          # List of stages for jobs, and their order of execution
- build
- test
build-job:       # This job runs in the build stage, which runs first.
stage: build
script:
- echo "Compiling the code..."
- C:/"Program Files"/Unity/Hub/Editor/2021.2.7f1/Editor/Unity.exe -batchmode -quit -projectPath ./"Testing Asignment 2" -executeMethod Building.MyBuildScript.PreformBuild | Out-Default
- echo "Compile complete."
unit-test-job:   # This job runs in the test stage.
stage: test    # It only starts when the job in the build stage completes successfully.
script:
- echo "Running unit tests... This will take about 60 seconds."
- C:/"Program Files"/Unity/Hub/Editor/2021.2.7f1/Editor/Unity.exe -batchmode -quit -projectPath ./"Testing Asignment 2" -runTests -testPlatfrom editmode -logFile -testResults ./unit-tests.xml | Out-Default

(我还将gitignore更改为不忽略Unity的Library\ScriptAssemblies。但我不知道是否需要这样做。(

最新更新