前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity-Jenkins打包部署工具(二)

Unity-Jenkins打包部署工具(二)

原创
作者头像
x-Alex
修改2020-07-14 15:12:01
1.5K0
修改2020-07-14 15:12:01
举报
文章被收录于专栏:AlexAlex

================================================================================================

本编主要介绍如何将工程进行jenkins的代理

  • 新建项目
  • 项目配置
    • 源码管理(SVN 关联)
    • 构建触发器
    • Unity编辑器脚本

================================================================================================

1、新建项目

1.1 新建Item
1.1 新建Item
1.2 选择自由风格工程进行构建,其他风格自行尝试
1.2 选择自由风格工程进行构建,其他风格自行尝试

2、项目配置

2.1 新建完成进行项目配置

2.2 因为工作需要使用svn进行版本同步,所以这里贴一下Svn的配置

接上张图:【注意】:Svn的四种更新方式,一般将开发工程和打包工程进行区分,所以选第四种最好

【附】:四种更新方式注解

2.3 构建触发 -- 任务定时计划

具体字符可以点击?进行查看,这里不细说了
具体字符可以点击?进行查看,这里不细说了

2.3 构建

【注】如果你没有使用svn去做代码管理,使用本地开发的项目进行打包,这里就可以定义项目路径

-quit -batchmode -projectpath xxx/xx -executeMethod YourEditorScript.YourBuildMethod

【补】通常情况下,我们进行打包需要针对不同平台,不同版本进行构建,所以传参是很有必要的。可以参考下面补充:

2.4 构建传参(可选)

2.4.1 参数定义

Jenkins提供9中类型参数模板,常用bool、choice、string

2.4.1 参数使用

回到构建2.3步骤,我们能看到在参数定义后面我们可以附加自定义的参数设置

举例:-quit -batchmode -executeMethod PerformBuild.CommandLineBuild Version-$Version ResVersion-$ResVersion PublishMode-$PublishMode Channel-$Channel -logFile "$WORKSPACE/unity3d_editor.log"

【注】:参数需要参数定义相匹配

2.5 Unity编辑器脚本

public class JenkinsBuild
{
    /// <summary>
    /// 此方法是从jienkins上接受数据的方法
    /// </summary>
    static void CommandLineBuild()
    {
        try
        {
            Debug.Log("Command line build\n------------------\n------------------");
            string[] scenes = GetBuildScenes();
            //这里的路径是打包的路径,自定义
            string path = "E:/Publish/cloudRes/version/Temp"; 
            Debug.Log(path);
            for (int i = 0; i < scenes.Length; ++i)
            {    
                Debug.Log($"Scene[{i}]: \"{scenes[i]}\"");
            }
            Debug.Log("Starting Build!");
            Debug.Log(GetJenkinsParameter("Platform"));
            Build(path);
        }
        catch (Exception err)
        {
            Console.WriteLine("方法F中捕捉到:" + err.Message);
            throw; //重新抛出当前正在由catch块处理的异常err
        }
        finally
        {
            Debug.Log("---------->  I am copying!   <--------------");
        }
    }
    /// <summary>
    ///解释jekins 传输的参数
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    static string GetJenkinsParameter( string name )
    {
        foreach (string arg in Environment.GetCommandLineArgs())
        {
            if (arg.StartsWith(name))
            {
                return arg.Split("-"[0])[1];
            }
        }
        return null;
    }
    
    /// <summary>
    /// 构建
    /// </summary>
    /// <param name="targetPath"></param>
    static void Build(string targetPath )
    {
        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }
        string version = GetJenkinsParameter("Version");
        string resVersion = GetJenkinsParameter("Version");
        string buildTarget = GetJenkinsParameter("BuildTarget");
        
        string curPath = $"{targetPath}/{PlayerSettings.productName}_{version}_{DateTime.Now:yyyy_MM_dd_HH_mm}.apk";
    
        if (Enum.TryParse(buildTarget,true,out BuildTarget target))
        {
            EditorUserBuildSettings.SwitchActiveBuildTarget(BuildPipeline.GetBuildTargetGroup(target),target);
        }
        BuildPipeline.BuildPlayer(GetBuildScenes(), curPath, target, BuildOptions.None);
        Debug.Log("Build Success");
    }
    
    /// <summary>
    /// 获取编辑器构建场景列表
    /// </summary>
    /// <returns></returns>
    static string[] GetBuildScenes()
    {
        List<string> names = new List<string>();

        foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes)
        {
            if (e == null)
                continue;

            if (e.enabled)
                names.Add(e.path);
        }
        return names.ToArray();
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、新建项目
  • 2、项目配置
    • 2.1 新建完成进行项目配置
      • 2.2 因为工作需要使用svn进行版本同步,所以这里贴一下Svn的配置
        • 2.3 构建触发 -- 任务定时计划
          • 2.3 构建
            • 2.4 构建传参(可选)
              • 2.4.1 参数定义
              • 2.4.1 参数使用
            • 2.5 Unity编辑器脚本
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档