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