Jenkins自动打AB,自动上传资源支持单打单传,一键所有
刚好公司项目需要这个东西,然后就搞了一个,主要是为了以后交给运营去管理这些东西,还是挺方便的不需要自己在开个工程等着打完上传了,这样的话在家里也能打,随时随地都能打,很是方便。
下面介绍下配置参数:
1.创建项目 填写项目介绍,这个无所谓
2.配置参数 用来打包时设置 和传给Unity用
参数到这里就配置完成了,下面配置下命令:
调用Unity的打包方法 传入参数进行打包
打包完成 启动FXP 传入账号密码 进行自动上传资源 期间需要在FXP中设置下自动覆盖 不然会弹出提示框
D:\flashftpmfb_ttrar\flashfxp.exe -min -c2 -upload ftp://ftpuser:fd88rBlDab736qAi@%UpLoadIP% -localpath="%WorkPath%\UpLoadTemp\" -remotepath="\%UpLoadPlace%\"
exit
看下Jenkins参数图:
**上一半结束:
开始下一半**
下面附上Unity BuildAssetsBundle打包方法:
/*------------------------------------------------------------------------------------------------------------------------------
*
* Title: Jenkins自动化打包工具
*
* Description: 只需登录Jenkins进行选择性打包 打包完成后可选择自动上传的服务器
*
* Author: 壹叶成名
*
* Date: 2020.4.11
*
* Modify:
*
* 注意:改脚本代码及参数为Jenkins平台打包使用,切勿随意更改,如需更改请于Jenkins同步更改。
-----------------------------------------------------------------------------------------------------------------------------------*/
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
public class BuildAssetsBundle
{
static string TAG = "BuildAssetBundle";
static string AbRootPath { get { return Application.dataPath + "/../../AssetBundle/"; } }
static string UpLodaTemp = Application.dataPath + "/../UpLoadTemp";
static string ABPath = "";
[MenuItem("Build/上传资源")]
public static void CopyAssestBundle()
{
Debug.Log(TAG + "Begin Copy AssetsBundle UpLoad !");
//清理原始文件
if (Directory.Exists(UpLodaTemp))
FileUtil.DeleteFileOrDirectory(UpLodaTemp);
//拷贝需要上传的资源
JenkinsTools.Copy(ABPath, UpLodaTemp);
Debug.Log(TAG + " UpLoad AssetsBundle Finish !");
}
public static void BuildAssetBundle()
{
Debug.Log(TAG + " Start >>>>>");
AssetsBundleSeting bundleSeting = GetBundleSetring();
BundleSeting(bundleSeting);
if (bundleSeting.bundleType != BundleType.None)
{
if (bundleSeting.bundleType == BundleType.All) //打所有的资源
{
for (int i = 0; i < BundleDefine.AllGames.Length; i++)
BundleEditor.StartAssetsBundle(BundleDefine.AllGames[i]);
}
else
{
Debug.Log(TAG+" Build Sing AssetsBundle !"+bundleSeting.bundleType.ToString());
BundleEditor.StartAssetsBundle(bundleSeting.bundleType.ToString()); //单个
}
}
//是否自动上传到服务器
if (bundleSeting.AotoUpLoad)
{
//拷贝AB包 供上传使用
CopyAssestBundle();
}
Debug.Log(TAG + "Build AssetsBundle Finish !!! ");
}
/// 根据读取的数据 在Unity中设置对应的参数
static void BundleSeting(AssetsBundleSeting seting)
{
if (!string.IsNullOrEmpty(seting.Version))
{
PlayerSettings.bundleVersion = seting.Version;
Legend.Game.ConstValue.LocalVersion = seting.Version;
}
if (seting.bundleType == BundleType.All)
ABPath = AbRootPath + Legend.Game.ConstValue.Platform + "/" + seting.Version; //上传整个版本文件夹
else
{
ABPath = AbRootPath + Legend.Game.ConstValue.Platform + "/" + seting.Version + "/" + seting.bundleType.ToString();//上传单个子游戏文件夹
UpLodaTemp += "/" + seting.Version;
}
//设置资源地点
Legend.Game.ConstValue.Platform = seting.upLoadPlace.ToString();
}
//获取Jenkins写入的unity参数配置
static AssetsBundleSeting GetBundleSetring()
{
string[] parameters = Environment.GetCommandLineArgs();
AssetsBundleSeting bundleSeting = new AssetsBundleSeting();
foreach (string str in parameters)
{
//解析Jenkins设置的渠道
if (str.StartsWith("BundleType"))
{
var tempParm = str.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (tempParm.Length == 2)
{
bundleSeting.bundleType = (BundleType)Enum.Parse(typeof(BundleType), tempParm[1], true);
}
}
if (str.StartsWith("UpLoadPlace"))
{
var tempParm = str.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (tempParm.Length == 2)
{
bundleSeting.upLoadPlace = (UpLoadPlace)Enum.Parse(typeof(UpLoadPlace), tempParm[1], true);
}
}
//解析版本号
if (str.StartsWith("Version"))
{
var tempParm = str.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (tempParm.Length == 2)
{
bundleSeting.Version = tempParm[1].Trim();
}
}
else if (str.StartsWith("AotoUpLoad"))
{
var tempParm = str.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (tempParm.Length == 2)
{
bool.TryParse(tempParm[1], out bundleSeting.AotoUpLoad);
}
}
}
return bundleSeting;
}
}
/// <summary>
/// 资源设置
/// </summary>
public class AssetsBundleSeting
{
//AB资源类型
public BundleType bundleType = BundleType.GameHall;
//资源版本号,指向生成和上传 的最终目录
public string Version = "";
//自动上传资源
public bool AotoUpLoad = false;
//上传的地点
public UpLoadPlace upLoadPlace;
}
//资源类型 接收Jenkins传回来的参数 一定要和Jenkins上完全对应
public enum BundleType
{
None,
GameHall,
******,
******,
SHZP,
MarshHero,
All,
}
//上传地点
public enum UpLoadPlace
{
AotoUpLoad_Android,
Fish3D_Android,
Comon_Android,
HuaWei_Android,
JiuYou_Android,
JuXiangWan_Android,
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class JenkinsTools
{
public static void Copy(string srcPath, string targetPath)
{
try
{
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
string scrdir = Path.Combine(targetPath, Path.GetFileName(srcPath));
if (Directory.Exists(srcPath))
scrdir += Path.DirectorySeparatorChar;
if (!Directory.Exists(scrdir))
{
Directory.CreateDirectory(scrdir);
}
string[] files = Directory.GetFileSystemEntries(srcPath);
foreach (string file in files)
{
if (Directory.Exists(file))
{
Copy(file, scrdir);
}
else
{
File.Copy(file, scrdir + Path.GetFileName(file), true);
}
}
}
catch
{
Debug.LogError("无法复制:" + srcPath + " 到" + targetPath);
}
}
public static void DeleteDir(string scrPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(scrPath);
FileSystemInfo[] fileInfo = dir.GetFileSystemInfos();
foreach (FileSystemInfo info in fileInfo)
{
if (info is DirectoryInfo)
{
DirectoryInfo subdir = new DirectoryInfo(info.FullName);
subdir.Delete(true);
}
else
{
File.Delete(info.FullName);
}
}
}
catch (Exception e)
{
Debug.LogError(e);
}
}
public static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "")
{
var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd);
pStartInfo.Arguments = args;
pStartInfo.CreateNoWindow = false;
pStartInfo.UseShellExecute = true;
pStartInfo.RedirectStandardError = false;
pStartInfo.RedirectStandardInput = false;
pStartInfo.RedirectStandardOutput = false;
if (!string.IsNullOrEmpty(workingDir))
pStartInfo.WorkingDirectory = workingDir;
return System.Diagnostics.Process.Start(pStartInfo);
}
public static void RunBat(string batfile, string args, string workingDir = "")
{
var p = CreateShellExProcess(batfile, args, workingDir);
p.Close();
}
}
全部配置好之后你就能进行打包了
流程就是Jenkins选择参数,调用Unity BuildAssetBundle方法 Unity根据参数进行打包,打包完成后 Jenkins会执行上传命令,把最终打出的AB包上传到服务器。
此文章并非一步一步小白教程,但一定可用,部分代码或配置需要根据自己的项目进行调整,如有不爽请跳过此文。
努力积才能,壹叶便成名!