Unity2018.3 + Jenkins 自动化打包方案(Windows)


项目需要自动打包.策划想要以出包形式及时看到主干提交内容.(漏提交报错/少资源等问题仍然无法避免)

 

1,你了解Jenkins吗?

  Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。主要功能包括:1、持续的软件版本发布/测试项目。2、监控外部调用执行的工作。这么说比较官方,说白了,它就是一种集承了多种常用的插件于一身的工具平台,通过这个平台你能很方便的管控你的项目!它的强大之处在于它能直接调用外部的shell指令和bat,那么今天我们一起去解开一点点它的什么面纱,为什么说是一点点呢?因为它太强大了,太深了!
 
 
本次使用Jenkins版本2.222.4
安装完毕后 会自动打开浏览器 http://localhost:8080/login?from=%2F
提示输入以下内容的密码
D:\Program Files (x86)\Jenkins\secrets\initialAdminPassword

 

 

 

输入密码后等待自己加载.安装插件
先安装推荐的插件

 

可见. 其中包含了 汉化,git,svn,ant,gradle等必要插件

 

创建管理员(不能使用默认名admin)

 

安装完成后,打开系统设置。

 

 

 

 

(似乎是因为安装目录有空格  变成了一个大坑 导致问题1 2)
要构架Unity脚本先要安装Unity插件,在插件管理–寻找插件–Unity 下载安装

 

 

 

 

 

增加全局变量 支持中文UTF8

 

 

 

 

 

 

 

 

配置Unity安装目录

 

 创建一个自由风格的任务

 

 

 

配置源码管理 SVN地址和本地项目位置 SVN用户


 

这里疑似发生SVN目录的层级问题.实际项目目录并未跟Unity根目录同级别 .尝试编译.无法通过并创建了空的Assets 等目录

可能是是Jenkins SVN插件和本地SVN同时参与了工作目录导致的冲突

 

 

 

 

 

 

 

配置构建工具

写入构建命令行参数

 

1 使用Unity插件构建

 

 

2 使用bat命令行来构建

在Editor目录增加对应的Jenkis打包脚本 接受构建命令行参数来控制打包流程

 

@echo off
set UnityPath="D:\Program Files\Unity\Editor\Unity.exe"

echo build = %dev%

REM 接受Jenkins传入的参数
REM set parameter=%1
REM echo parameter:%parameter%

REM 将Jenkins传入的参数写到本地,Editor下读取在jenkins中设置的版本号和渠道名称
REM echo %parameter%>parameter.txt


echo Start Build Package
REM BuildAssetBundle
%UnityPath% -projectPath -quit -batchmode -logFile D:\tmp\build.log -executeMethod PerformBuild.CommandLineBuild paramdev-%dev% ab-%AB% ver-%SVN_REVISION% wpath-%WORKSPACE% ScritpsOnly-%ScritpsOnly%
echo Build Package Finished

if not %errorlevel%==0 ( goto fail ) else ( goto success )

:success
echo Build Windows OK
REM Copr Dir
goto end

:fail
echo Build Windows Fail
echo %errorlevel%
goto end

:end

 

#移动目录和文件

  

  对应的PerformBuild打包脚本

using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Jenkis打包工具配置脚本
/// </summary>

public class PerformBuild
{
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();
}


/// <summary>
    /// 此方法是从jienkins上接受  数据的 方法
    /// </summary>
public static void CommandLineBuild()
{
try
{

Debug.Log("Command line build\n------------------\n------------------");

string[] scenes = GetBuildScenes();
string buildOption = GetJenkinsParameter("paramdev");//参数定义开发版?
Debug.Log(buildOption);

string ab = GetJenkinsParameter("ab");// AB包编译选项
Debug.Log(ab);
//稍后处理ab打包选项
if (ab == "全部编译")
ExportAssetBundle.BuildAllAssetBundlesWindows();


string ver = GetJenkinsParameter("ver");//svn版本号
Debug.Log(ver);

string path = "D:/Program Files (x86)/Jenkins/workspace/Test3/target";//workSpace位置

//清理生成目录
if (Directory.Exists(path))
{
Directory.Delete(path, true);
Directory.CreateDirectory(path);
}

Debug.Log(path);

BuildOptions option = BuildOptions.None;
if (buildOption == "dev")
option = BuildOptions.Development;

string bScriptOnly = GetJenkinsParameter("ScritpsOnly");
if(bScriptOnly == "true")
{
option |= BuildOptions.BuildScriptsOnly;
}

for (int i = 0; i < scenes.Length; ++i)
{
Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
}
//ProjectPackageEditor.BuildByJenkins(GetJenkinsParameter("Platform"), GetJenkinsParameter("AppID"), GetJenkinsParameter("Version"), GetJenkinsParameter("IPAddress"));
Debug.Log("Starting Build!");

string appurl = path + "/依盖之书_" + buildOption + "_" + ver + ".exe";
Debug.Log(appurl);
BuildPipeline.BuildPlayer(scenes, appurl, BuildTarget.StandaloneWindows64, option);
Debug.Log("Starting End!");
}
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;
}
}

 

  

增加构建后的操作

File Operations 插件 将存档内容打包

Output Folder Path 配置总是丢失 只能采用多一步的copy指令

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

Archive the artifacts 复制哪些文件进入存档目录

目录默认位置是workspace项目目录 .

存放完成后其他用户可以直接从构建结果里下载打包文件 (也可以直接点下面的下载所有内容到zip)

 

 

 

 

 

配置定时构建触发器

 

定时构建语法

* * * * *

(五颗星,中间用空格隔开)

第一颗*表示分钟,取值0~59
第二颗*表示小时,取值0~23
第三颗*表示一个月的第几天,取值1~31
第四颗*表示第几月,取值1~12
第五颗*表示一周中的第几天,取值0~7,其中0和7代表的都是周日

1.每30分钟构建一次:

H/30 * * * *

2.每2个小时构建一次

H H/2 * * *

3.每天早上8点构建一次

0 8 * * *

4.每天的8点,12点,22点,一天构建3次

0 8,12,22 * * *

(多个时间点,中间用逗号隔开)

 

 

 

 

 

修改Jenkins 主目录  (失败,会导致jenkins配置失效无法启动的问题)

一、Windows环境更改Jenkins的主目录

Windows环境中,Jenkins主目录默认在C:\Documents and Settings\AAA\.jenkins 。

可以通过设置环境变量来修改,例如: JENKINS_HOME=C:\jenkins,然后重新启动jenkins。

Jenkins.xml 修改
env name=“JENKINS_HOME” value="%BASE%"

env name=“JENKINS_HOME” value="%JENKINS_HOME%"

 

 重启指令API

http://localhost:8080/restart

 

jenkins 启动指令

cmd 命令行  到jenkins目录下启动服务  net start jenkins

 

问题汇总

1 Windows环境中,Jenkins可能会找不到绝对路径的目录.SVN配置的local moudle directory 会提示找不到路径.....改为使用默认路径 D:\Program Files (x86)\Jenkins\workspace\TestUnity(项目名)

傻傻的路径 拼接问题 " Couldn't set project path to: D:/Program Files (x86)/Jenkins/workspace/TestUnity/D:/鐗堟湰搴�/jenkins_out"

"Couldn't set project path to: D:/Program Files (x86)/Jenkins/workspace/TestUnity/jenkins_out"  路径名还不能有空格 使用 路径最好使用引号"D:\jenkins_out"

您需要格式化 -projectPath="D:\jenkins_out" 选项,并且在 -projectPath 之间使用等号和路径,而不是文档中指出的空格

 

2 Unity3d插件导致目录权限问题   改用bat命令行来进行打包

executeMethod class 'PerformBuild' could not be found.
Argument was -executeMethod PerformBuild.CommandLineBuild

折腾一大圈最后确认时-projectPath配置应该是项目Assets上一级目录 .如果目录不正确则找不到PerformBuild脚本 

 

3 Archiving artifacts  生成打包zip并存档内容用法错误

‘packout/*.zip’没有匹配到任何内容:甚至‘packout’也不存在

ERROR: Step ‘Archive the artifacts’ failed: No artifacts found that match the file pattern "packout/*.zip". Configuration error?

 

ERROR: Step ‘Archive the artifacts’ failed: No artifacts found that match the file pattern "target/". Configuration error?
Finished: FAILURE 

使用正则来选择打包哪些目录和文件 .默认整个目录打包的话参数** 即可

配合File Operations 插件进行压缩存档
 

4 多个项目 使用SVN不同分支的时候回导致冲突和全删重拉取 。疑似因为本地安装了SVN 1.10 跟jenkinsSVN插件有冲突。

Building in workspace D:\Program Files (x86)\Jenkins\workspace\BranchQHS
Updating svn://xxxxxxxxx/qhs2svn/STS_branch/qhs1.0 at revision '2020-06-10T00:21:44.493 +0800'
svn: E155000: Failed to add directory 'Library/PackageCache/com.unity.purchasing@2.0.3': a versioned directory of the same name already exists
Updated failed due to local files. Getting a fresh workspace
Cleaning local Directory .

Building in workspace D:\Program Files (x86)\Jenkins\workspace\MainBuild
Updating svn://xxxxxxxxx/qhs2svn/STS_Unity at revision '2020-06-10T04:49:57.306 +0800'
svn: E155000: Failed to add directory 'Library/PackageCache/com.unity.postprocessing@2.0.15-preview': a versioned directory of the same name already exists
Updated failed due to local files. Getting a fresh workspace
Cleaning local Directory .
Checking out svn://xxxxxxxxx/qhs2svn/STS_Unity at revision '2020-06-10T04:49:57.306 +0800'


正确的应该是 把所在的目录进行work update 升级 由SVN 1.10较高版本控制 后解决
Building in workspace D:\Program Files (x86)\Jenkins\workspace\testSVNBranch
Reverting D:\Program Files (x86)\Jenkins\workspace\testSVNBranch\. to depth infinity with ignoreExternals: true
Updating svn://xxxxxxx/t2svn/branches/qhs1.0 at revision '2020-06-10T10:00:04.089 +0800' --quiet
Using sole credentials dashi/****** (大师svn) in realm ‘<svn://xxxxxxxxx:3690> d5746346-b193-4201-90f7-46f29c26adcb’
At revision 146

No changes for svn://xxxxxxxxx/t2svn/branches/qhs1.0 since the previous build
Finished: SUCCESS


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM