Jenkins自动构建Unity

时间:2024-01-09 15:32:32

1.下载安装Jenkins

  链接:https://jenkins.io/

2.安装推荐plugins

  NOTE:安装失败的话,后面进入管理plugins的高级选项中,将更新站点设置为:http://mirror.xmission.com/jenkins/updates/current/update-center.json。

  2.1.安装Unity3d plugin。

3.创建新任务

  3.1.在Configure中新增build step,选择Unity,设置命令行参数-quit -batchmode -projectPath "E:/Project Files/Unity Project/2018.3/jenkins" -executeMethod Jenkins.AutoBuild -logFile build.log Version-$Version。

  3.2.build environment中勾选Add timestamps to the console output,为每个命令输出添加时间戳,方便后期查找。

  3.3.设置SVN版本控制

Jenkins自动构建Unity

  3.4.新增命令行参数,其中参数以$Version的形式存在

Jenkins自动构建Unity

  3.5.丢弃旧的构建,保证磁盘不会出现占满的情况。

4.添加Unity Editor脚本。

public class Jenkins : Editor
{
private static string[] SCENES = FindEnabledEditorScenes(); private static string APP_NAME = "Jenkins";
private static string TARGET_DIR = "E:/Project Files/Unity Project/2018.3/jenkins/Build";
private static string JDK_DIR = "F:/Program Files/Java/jdk1.8.0_25";
private static string ANDROID_SDK_DIR = "F:/AndroidSDK"; private static string[] FindEnabledEditorScenes()
{
List<string> EditorScenes = new List<string>();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (!scene.enabled) continue;
EditorScenes.Add(scene.path);
}
return EditorScenes.ToArray();
} [MenuItem("Jenkins/Auto Build")]
public static void AutoBuild()
{
//Jenkins使用system账号,读取注册表路径为HKEY_USER;手动打开Unity使用user账号,读取注册表路径为HKEY_CURRENT_USER;
EditorPrefs.SetString("jdkPath", JDK_DIR);
EditorPrefs.SetString("AndroidSdkRoot", ANDROID_SDK_DIR); string version = GetJenkinsParameter("Version"); string suffix = string.Empty;
#if UNITY_STANDALONE_WIN
suffix = ".exe";
#elif UNITY_ANDROID
suffix = ".apk";
#elif UNITY_IOS
suffix = ".ipa";
#endif string targetFilePath = TARGET_DIR + "/" + APP_NAME + "_" + version + suffix; GenericBuild(SCENES, targetFilePath, EditorUserBuildSettings.activeBuildTarget, BuildOptions.None);
} public static void GenericBuild(string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
{
BuildReport report = BuildPipeline.BuildPlayer(scenes, target_dir, build_target, build_options);
Debug.Log(report);
} private static string GetJenkinsParameter(string paramName)
{
foreach (var command in Environment.GetCommandLineArgs())
{
if (command.StartsWith(paramName) == true)
{
return command.Split('-')[1];
}
}
return string.Empty;
}
}