Skip to content

ci: Automated project builders #3557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: develop-2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .yamato/project-builders/builder.metafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# https://internaldocs.unity.com/yamato_continuous_integration/usage/templating/

NetcodeProjects:
# Note that we are using internal Unity repo. This means that we may test with newest changes that are not yet released to our users (there are also public versions)
# Note that for BossRoom 'main' branch supports NGOv1.X and 'develop' branch supports NGOv2.X
BossRoom:
GithubRepo: "https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop.git"
branch: develop
manifestPath: Packages/manifest.json
projectPath: '.'
Asteroids:
GithubRepo: "https://github.cds.internal.unity3d.com/unity/Asteroids-CMB-NGO-Sample.git"
branch: main
manifestPath: Packages/manifest.json
projectPath: '.'
SocialHub:
GithubRepo: "https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize.git"
branch: main
manifestPath: Basic/DistributedAuthoritySocialHub/Packages/manifest.json
projectPath: 'Basic/DistributedAuthoritySocialHub'
96 changes: 96 additions & 0 deletions .yamato/project-builders/project-builders.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{% metadata_file .yamato/project-builders/builder.metafile %}
---
# The above line corresponds to https://internaldocs.unity.com/yamato_continuous_integration/usage/templating/

# Yamato paths disclaimer:
# Note that artifacts can get only local paths and since cloned project is in different location, you need to be careful with paths
# All artifact paths are defined relative to the Yamato source directory (YAMATO_SOURCE_DIR) and are case-sensitive.
# !!! important “Artifact directory locations” All artifact globs are defined relative to the source directory (YAMATO_SOURCE_DIR). Yamato can’t capture artifacts from outside this directory, so if you need files from elsewhere, you should copy them into your source directory as part of job commands.

# Those jobs were created in order to (in most cases) speed up playtesting and development time.
# The aim is to collect all possible projects that use Netcode for Entities and create a job that will build the project with the given version of N4E package.
# The package is taken directly from the branch from which the job was triggered. So image triggering the job from release/2.0.0 and release/2.1.0 branch to compare differences
# Example use case would be to trigger the build job on Sunday so by Monday morning all builds are ready for playtesting. This limits the time a dev/QA has to spend building projects for different configurations (win, mac, Android, scripting backends, burst etc) or simply time necessary for building huge projects (Megacity).

# This job takes parameters as scriptable backend configuration, burst compilation, unity editor version and platform.
# Since Yamato variables can't be restricted to only specific values, the job will validate the parameters passed to it and fail quickly if those are incorrect. In order to see what options are available, please look at the variable name.

# Note that for now all of those builds are being made on Windows machine (so for example combination of macOS + il2cpp is expected to fail)
# TODO: for now all builds are being made on Windows machine, but it would be nice to have a Mac build as well.
# TODO: add iOS support
{% for netcodeProject in NetcodeProjects -%}
build_{{ netcodeProject[0] }}_project:
name: {{ netcodeProject[0] }}
agent:
type: Unity::VM
image: package-ci/win10:v4
flavor: b1.xlarge
variables:
UNITY_VERSION: trunk
SCRIPTING_BACKEND_IL2CPP_MONO: il2cpp
BURST_ON_OFF: on
PLATFORM_WIN64_MAC_ANDROID: win64
commands:
# Validate inputs passed via Yamato variables
- python Tools/CI/scripts/BuildAutomation/validate_params.py
- echo Building {{ netcodeProject[0] }} project with Unity version of %UNITY_VERSION%, Scripting backend %SCRIPTING_BACKEND_IL2CPP_MONO%, Burst %BURST_ON_OFF% for platform %PLATFORM_WIN64_MAC_ANDROID%

# Clone the external project repository into a specific directory. Notice that branch is also specified.
- git clone --single-branch --branch {{ netcodeProject[1].branch }} {{ netcodeProject[1].GithubRepo }} C:/ClonedProject

# Modify the external project's manifest to use the local N4E package from current branch on which this Yamato job is running. (requires python that should be preinstalled in the image)
- python Tools/CI/scripts/BuildAutomation/manifest_update.py --manifest-path C:/ClonedProject/{{ netcodeProject[1].manifestPath }} --local-package-path %YAMATO_SOURCE_DIR%/com.unity.netcode.gameobjects

# Run python script to update ProjectSettings.asset in order to connect the project to Unity Services/set proper values.
# Notice that if a project has this already set up then in theory we don't need to run this script.
- python Tools/CI/scripts/BuildAutomation/connect_services.py --project-settings-path C:/ClonedProject/{{ netcodeProject[1].projectPath }}/ProjectSettings/ProjectSettings.asset

# Enable or disable Burst compilation. This step is specific to Netcode package (or any package that uses Burst)
- IF "%BURST_ON_OFF%"=="on" (python Tools/CI/scripts/BuildAutomation/disable-enable-burst.py --enable-burst --project-path C:/ClonedProject/{{ netcodeProject[1].projectPath }})
ELSE (python Tools/CI/scripts/BuildAutomation/disable-enable-burst.py --disable-burst --project-path C:/ClonedProject/{{ netcodeProject[1].projectPath }})

# Download the Unity Editor version specified in the UNITY_VERSION variable. Il2cpp component is downloaded only if the SCRIPTING_BACKEND_IL2CPP_MONO is set to "il2cpp".
# TODO: we could download components only if needed
- unity-downloader-cli --fast --wait -u %UNITY_VERSION% -p C:/TestingEditor -c Editor -c il2cpp -c Android -c macOS

# Add BuilderScript.cs to the project so we can modify and build the project using Unity Editor.
# This is a bit tricky step, notice that we also need to include proper assembly definition in order for those scripts to compile properly.
# TODO: the asmdef file can be simplified
- python Tools/CI/scripts/BuildAutomation/FileCopy.py "C:/ClonedProject/{{ netcodeProject[1].projectPath }}"

# Build the project using Unity Editor. This will call the given static BuilderScripts method.
# Ideally, it would be nice to parametrize the BuilderScripts (for example to pass scripting backend as parameter) but -executeMethod only calls static methods without parameters so for now we will have multiple configurations
# Notice that for Android platform even if mono is selected, il2cpp will be used since mono is not supported for Android builds.
- IF "%PLATFORM_WIN64_MAC_ANDROID%"=="win64" (
IF "%SCRIPTING_BACKEND_IL2CPP_MONO%"=="il2cpp" (
C:/TestingEditor/Unity.exe -projectPath C:/ClonedProject/{{ netcodeProject[1].projectPath }} -buildTarget win64 -executeMethod BuilderScripts.BuildWinIl2cpp -batchmode -logFile ./artifacts/UnityLog.txt -automated -crash-report-folder ./artifacts/CrashArtifacts -quit
) ELSE (
C:/TestingEditor/Unity.exe -projectPath C:/ClonedProject/{{ netcodeProject[1].projectPath }} -buildTarget win64 -executeMethod BuilderScripts.BuildWinMono -batchmode -logFile ./artifacts/UnityLog.txt -automated -crash-report-folder ./artifacts/CrashArtifacts -quit
)
)
ELSE IF "%PLATFORM_WIN64_MAC_ANDROID%"=="mac" (
IF "%SCRIPTING_BACKEND_IL2CPP_MONO%"=="il2cpp" (
C:/TestingEditor/Unity.exe -projectPath C:/ClonedProject/{{ netcodeProject[1].projectPath }} -buildTarget osx -executeMethod BuilderScripts.BuildMacIl2cpp -batchmode -logFile ./artifacts/UnityLog.txt -automated -crash-report-folder ./artifacts/CrashArtifacts -quit
) ELSE (
C:/TestingEditor/Unity.exe -projectPath C:/ClonedProject/{{ netcodeProject[1].projectPath }} -buildTarget osx -executeMethod BuilderScripts.BuildMacMono -batchmode -logFile ./artifacts/UnityLog.txt -automated -crash-report-folder ./artifacts/CrashArtifacts -quit
)
)
ELSE IF "%PLATFORM_WIN64_MAC_ANDROID%"=="android" (
C:/TestingEditor/Unity.exe -projectPath C:/ClonedProject/{{ netcodeProject[1].projectPath }} -buildTarget android -executeMethod BuilderScripts.BuildAndroidIl2cpp -batchmode -logFile ./artifacts/UnityLog.txt -automated -crash-report-folder ./artifacts/CrashArtifacts -quit
)

# Because of this we need to ensure that all files are copied to the source directory.
# TODO: this can be omitted if I can somehow build the project in the source directory (YAMATO_SOURCE_DIR) instead of C:/CompetitiveAction
- python -c "import os; os.makedirs('./build', exist_ok=True)" # --> Create the build directory if it doesn't exist
- python -c "import shutil; shutil.copytree('C:/ClonedProject/{{ netcodeProject[1].projectPath }}/build', './build', dirs_exist_ok=True)" # --> Copy the build directory to the source directory (YAMATO_SOURCE_DIR). Remember to copy entire directory and not only exe file

artifacts:
logs:
paths:
- '*.log'
- '*.xml'
- artifacts/**/*
players:
paths:
- build/**/*
{% endfor -%}
181 changes: 181 additions & 0 deletions Tools/CI/scripts/BuildAutomation/BuilderScripts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;

// This script is used to automate the build process for different platforms.
// When included in the project, it can be triggered from the script the game for teh given configuration.
// Note that it's possible to have those as a button in the editor (see https://github.cds.internal.unity3d.com/unity/Megacity-Metro/blob/c3b1b16ff1f04f96fbfbcc3267696679ad4e8396/Megacity-Metro/Assets/Scripts/Utils/Editor/BuilderScript.cs)
// Ideally we would like to pass scripting backend and platform as parameters instead of having different script per each combintation but the nature of calling this nmethod via script (via -executeMethod) is that it needs to be static and the parameters are not passed in a way that we can use them.
// TODO: add iOS support
public class BuilderScripts : MonoBehaviour
{
[MenuItem("Tools/Builder/Build Development Windows Il2cpp")]
static void BuildWinIl2cpp()
{
// This part modifies Player Settings. We only use it (for our case) to modify scripting backend
PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.IL2CPP);
PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.StandaloneWindows64, false); // disable auto graphic to use our own custom list
PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneWindows64, new []{GraphicsDeviceType.Vulkan, GraphicsDeviceType.Direct3D11, GraphicsDeviceType.Direct3D12}); // We need to specify the graphics API for Android builds to ensure proper shader compilation. Vulkan is recommended for modern devices.

// Below you can see additional settings that you can apply to the build:
//PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneWindows64, new []{GraphicsDeviceType.Direct3D12});
//PlayerSettings.SetArchitecture(NamedBuildTarget.Standalone,0);

// The settings that we applied above need to be saved.
AssetDatabase.SaveAssets();

// We want to build all scenes in build settings, so we collect them here.
// If you want to build only specific scenes, then you could just use something like buildPlayerOptions.scenes = new[] { "Assets/Scenes/Menu.unity","Assets/Scenes/Main.unity" }; below.
List<string> scenesToAdd = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
Debug.Log("Adding scene to build: " + scene.path);
scenesToAdd.Add(scene.path);
}
}

// This is an equivalent of BuildPlayerOptions in the Unity Editor.
// We want to choose development build, what platform are we targetting, where to save the build and which scenes to include.
// Some of those options can be omitted when triggering this script from withing GUI since more implicit context is provided (targetGroup, subtarget)
// subtarget = (int)StandaloneBuildSubtarget.Player
// targetGroup = BuildTargetGroup.Standalone
// extraScriptingDefines = new[] { "NETCODE_DEBUG", "UNITY_CLIENT" },
var buildPlayerOptions = new BuildPlayerOptions
{
locationPathName = "./build/Windows_Il2cpp/PlaytestBuild.exe",
target = BuildTarget.StandaloneWindows64,
options = BuildOptions.Development,
scenes = scenesToAdd.ToArray()
};

BuildPipeline.BuildPlayer(buildPlayerOptions);
}

[MenuItem("Tools/Builder/Build Development Windows Mono")]
static void BuildWinMono()
{
PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.Mono2x);
PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.StandaloneWindows64, false); // disable auto graphic to use our own custom list
PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneWindows64, new []{GraphicsDeviceType.Vulkan, GraphicsDeviceType.Direct3D11, GraphicsDeviceType.Direct3D12}); // We need to specify the graphics API for Android builds to ensure proper shader compilation. Vulkan is recommended for modern devices.

AssetDatabase.SaveAssets();

List<string> scenesToAdd = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
Debug.Log("Adding scene to build: " + scene.path);
scenesToAdd.Add(scene.path);
}
}

var buildPlayerOptions = new BuildPlayerOptions
{
locationPathName = "./build/Windows_Mono/PlaytestBuild.exe",
target = BuildTarget.StandaloneWindows64,
options = BuildOptions.Development,
scenes = scenesToAdd.ToArray()
};

BuildPipeline.BuildPlayer(buildPlayerOptions);
}

[MenuItem("Tools/Builder/Build Development Mac Mono")]
static void BuildMacMono()
{
PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.Mono2x);
PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.StandaloneOSX, false); // disable auto graphic to use our own custom list
PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneOSX, new []{GraphicsDeviceType.Metal}); // enforcing Metal Graphics API. Without this there will be shader errors in the final build.

AssetDatabase.SaveAssets();

List<string> scenesToAdd = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
Debug.Log("Adding scene to build: " + scene.path);
scenesToAdd.Add(scene.path);
}
}

var buildPlayerOptions = new BuildPlayerOptions
{
locationPathName = "./build/macOS_Mono/PlaytestBuild.app",
target = BuildTarget.StandaloneOSX,
options = BuildOptions.Development,
scenes = scenesToAdd.ToArray()
};

BuildPipeline.BuildPlayer(buildPlayerOptions);
}

[MenuItem("Tools/Builder/Build Development Mac Il2cpp")]
static void BuildMacIl2cpp()
{
PlayerSettings.SetScriptingBackend(NamedBuildTarget.Standalone, ScriptingImplementation.IL2CPP);
PlayerSettings.SetGraphicsAPIs(BuildTarget.StandaloneOSX, new []{GraphicsDeviceType.Metal}); // enforcing Metal Graphics API. Without this there will be shader errors in the final build.

AssetDatabase.SaveAssets();

List<string> scenesToAdd = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
Debug.Log("Adding scene to build: " + scene.path);
scenesToAdd.Add(scene.path);
}
}

var buildPlayerOptions = new BuildPlayerOptions
{
locationPathName = "./build/macOS_Il2cpp/PlaytestBuild.app",
target = BuildTarget.StandaloneOSX,
options = BuildOptions.Development,
scenes = scenesToAdd.ToArray()
};

BuildPipeline.BuildPlayer(buildPlayerOptions);
}

[MenuItem("Tools/Builder/Build Development Android Il2cpp")]
static void BuildAndroidIl2cpp()
{
PlayerSettings.SetScriptingBackend(NamedBuildTarget.Android, ScriptingImplementation.IL2CPP);
PlayerSettings.SetApplicationIdentifier(NamedBuildTarget.Android, "com.UnityTestRunner.UnityTestRunner"); // This is needed only for mobiles since by default the application identifier quite often contains invalid characters like spaces so we wan't to make sure that this has a valid value. It's needed only for mobile since that's an app store requirement
PlayerSettings.SetUseDefaultGraphicsAPIs(BuildTarget.Android, false); // disable auto graphic to use our own custom list
PlayerSettings.SetGraphicsAPIs(BuildTarget.Android, new []{GraphicsDeviceType.Vulkan}); // We need to specify the graphics API for Android builds to ensure proper shader compilation. Vulkan is recommended for modern devices.
PlayerSettings.SetArchitecture(BuildTargetGroup.Android,1); // An integer value associated with the architecture of the build target. 0 - None, 1 - ARM64, 2 - Universal. most modern Android devices use the ARM64 architecture

AssetDatabase.SaveAssets();

List<string> scenesToAdd = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
Debug.Log("Adding scene to build: " + scene.path);
scenesToAdd.Add(scene.path);
}
}

var buildPlayerOptions = new BuildPlayerOptions
{
locationPathName = "./build/Android_Il2cpp_Vulkan/PlaytestBuild.apk",
target = BuildTarget.Android,
options = BuildOptions.Development,
scenes = scenesToAdd.ToArray()
};

BuildPipeline.BuildPlayer(buildPlayerOptions);
}
}
Loading