Skip to content

Instantly share code, notes, and snippets.

@aprius
Last active September 27, 2024 07:21
Show Gist options
  • Save aprius/ac19880ff2f91df41c174a8dd704afdf to your computer and use it in GitHub Desktop.
Save aprius/ac19880ff2f91df41c174a8dd704afdf to your computer and use it in GitHub Desktop.
#if UNITY_EDITOR
using System;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEditor.Build.Reporting;
using System.IO;
namespace JENKINS
{
public class AutoBuilder : ScriptableObject
{
private static readonly string[] Scenes = FindEnabledEditorScenes();
private static string appName;
private static string targetDir;
private const string LOG_FILE_PATH = "C:/Users/yenmoc/Desktop/JenkinsBuildLog.txt";
public static void PerformWindowsMixedRealityBuild()
{
Application.logMessageReceived += HandleLog;
appName = GetArg("-appName");
targetDir = GetArg("-buildFolder");
Debug.Log("Jenkins-Build: APP_NAME: " + appName + " TARGET_DIR: " + targetDir);
PlayerSettings.Android.buildApkPerCpuArchitecture = false; // Disable APK per architecture
EditorUserBuildSettings.buildAppBundle = false; // Enable AAB building
GenericBuild(Scenes,
targetDir + "/" + appName,
BuildTargetGroup.Android,
BuildTarget.Android,
BuildOptions.None);
Application.logMessageReceived -= HandleLog;
}
public static void PerformAndroidAppBundleBuild()
{
Application.logMessageReceived += HandleLog;
appName = GetArg("-appName");
targetDir = GetArg("-buildFolder");
Debug.Log("Jenkins-Build: APP_NAME: " + appName + " TARGET_DIR: " + targetDir);
// Set PlayerSettings to build AAB
PlayerSettings.Android.buildApkPerCpuArchitecture = false; // Disable APK per architecture
EditorUserBuildSettings.buildAppBundle = true; // Enable AAB building
GenericBuild(Scenes,
targetDir + "/" + appName,
BuildTargetGroup.Android,
BuildTarget.Android,
BuildOptions.None);
Application.logMessageReceived -= HandleLog;
}
private static string[] FindEnabledEditorScenes()
{
var editorScenes = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (!scene.enabled) continue;
editorScenes.Add(scene.path);
}
return editorScenes.ToArray();
}
private static void GenericBuild(string[] scenes, string appTarget, BuildTargetGroup buildTargetGroup, BuildTarget buildTarget, BuildOptions buildOptions)
{
//EditorUserBuildSettings.SwitchActiveBuildTarget(build_target_group, BuildTarget.Android);
PlayerSettings.Android.keyaliasPass = GetArg("-keyaliasPass");
PlayerSettings.Android.keystorePass = GetArg("-keystorePass");
Debug.LogFormat("**** keyaliasPass : {0}", PlayerSettings.Android.keyaliasPass);
Debug.LogFormat("**** keystorePass : {0}", PlayerSettings.Android.keystorePass);
var buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = scenes;
buildPlayerOptions.locationPathName = appTarget;
buildPlayerOptions.target = BuildTarget.Android;
buildPlayerOptions.options = BuildOptions.None;
Debug.LogFormat("**** app_target : {0}", appTarget);
var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
Debug.LogFormat("**** player : {0}", report);
var summary = report.summary;
Debug.LogFormat("**** summary.result : {0}", summary.result);
if (summary.result == BuildResult.Succeeded)
{
Debug.Log("**** Succeeded!");
}
else if (summary.result == BuildResult.Failed)
{
Debug.Log("**** Failed!");
foreach (var step in report.steps)
{
foreach (var message in step.messages)
{
Debug.Log("****" + message);
}
}
}
else if (summary.result == BuildResult.Cancelled)
{
Debug.Log("**** Cancelled!");
}
else
{
// Unknown
Debug.Log("**** Unknown!");
}
EditorApplication.Exit(0);
}
/**
* Get Arguments from the command line by name
*/
private static string GetArg(string name)
{
var args = Environment.GetCommandLineArgs();
for (var i = 0; i < args.Length; i++)
{
if (args[i] == name && args.Length > i + 1)
{
return args[i + 1];
}
}
return null;
}
private static void HandleLog(string logString, string stackTrace, LogType type)
{
using (var writer = new StreamWriter(LOG_FILE_PATH, true))
{
writer.WriteLine(logString);
if (type == LogType.Exception || type == LogType.Error)
{
writer.WriteLine(stackTrace);
}
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment