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包上傳到伺服器。
此文章并非一步一步小白教程,但一定可用,部分代碼或配置需要根據自己的項目進行調整,如有不爽請跳過此文。
努力積才能,壹葉便成名!