MacOS部署Jenkins,打包unity

准备Jenkins

  • 终端输入brew install jenkins,用这个方式安装本质还是个war包,但是不用再安装Tomcat
  • 安装好后直接在终端输入jenkins启动应用,浏览器输入localhost:8080可以打开。
  • 网上有8080端口被占用的处理办法
  • brew安装失败大概率是因为githubusercontent被墙了,要么梯子,要么修改Mac上的hosts,方法如下:
    • sudo vi /etc/hosts
    • https://www.ipaddress.com/查询raw.githubusercontent.com的当前地址
    • 然后在hosts里面加入,当前的IP是199.232.28.133 raw.githubusercontent.com
  • 网页打开后第一次要注册,然后会提示安装插件,最后就可以进入Jenkins。基本上准备工作没啥难度,除了网速。

准备自动打包

Jenkins中配置

  • 创建一个任务后,点击任务的配置
  • 构建部分,这些暴露的参数就是给下面脚本用的,而脚本中调用unity命令时的参数又是在C#中通过GetCommandArgValue函数获得。

image

1
2
3
4
5
6
7
export Project_Path=$WORKSPACE
export PRODUCT_NAME="Test"
export PROJ_OUTPUT="$Project_Path/build/android"
export UNITY_PATH="/Applications/Unity/Unity.app/Contents/MacOS/Unity"
export PROJ_PATH="$Project_Path/HelloUnity/"
export UNITY3D_BUILD_METHOD="PerformBuild.CommandLineBuildAndroid"
sh /Users/m/work/JenkinsTest/MacBuildAndroid.sh

准备sh脚本

  • 脚本主体就是调用unity的命令,其中大量的参数都来自jenkins的设置。
1
2
3
4
5
6
7
8
9
#!/bin/bash
echo "Delete build/android"
rm -rf $Project_Path/build/android
echo "Delete Success"

echo "Build Android"

${UNITY_PATH} -quit -batchmode -nographics -projectPath $PROJ_PATH -executeMethod ${UNITY3D_BUILD_METHOD} -logFile "log.txt" -PROJ_OUTPUT $PROJ_OUTPUT -PRODUCT_NAME $PRODUCT_NAME
echo "Build Success!"

准备C#脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
static readonly string OUTPUT_PATH_NAME = "-PROJ_OUTPUT";
static readonly string PRODUCTT_NAME = "-PRODUCT_NAME"; //包名称

...

static string GetCommandArgValue(string key)
{
string defaultValue = string.Empty;
// 获取参数的值
string[] args = System.Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length - 1; i++)
{
if (args[i].Equals(key))
{
defaultValue = args[i + 1];
break;
}
}
return defaultValue;
}

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();
}

static string GetBuildPathAndroid()
{
string dirPath = GetCommandArgValue(OUTPUT_PATH_NAME) + "/" + GetCommandArgValue(PRODUCTT_NAME) + ".apk";
if (!System.IO.Directory.Exists(dirPath))
{
System.IO.Directory.CreateDirectory(dirPath);
}
return dirPath;
}


static void CommandLineBuildAndroid()
{
Debug.Log("Command line build android version\n------------------\n------------------");

string[] scenes = GetBuildScenes();
string path = GetBuildPathAndroid();
if (scenes == null || scenes.Length == 0 || path == null)
{
Debug.LogError("Please add scene to buildsetting...");
return;
}

Debug.Log(string.Format("Path: \"{0}\"", path));
for (int i = 0; i < scenes.Length; ++i)
{
Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
}

Debug.Log("Starting Android Build!");
SetUnityBuildArgs(BuildTargetGroup.Android);
BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);
}

各种

删除构建历史记录

  • jenkins–系统管理—-脚本命令行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// NOTE: uncomment parameters below if not using Scriptler >= 2.0, or if you're just pasting
// the script in manually.

// The name of the job.
def jobName = "Test"

// The range of build numbers to delete.
def buildRange = "1-30"

import jenkins.model.*;
import hudson.model.Fingerprint.RangeSet;
def j = jenkins.model.Jenkins.instance.getItem(jobName);

def r = RangeSet.fromString(buildRange, true);

j.getBuilds(r).each { it.delete() }

修改时区

  • jenkins–系统管理—-脚本命令行
1
System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', 'Asia/Shanghai')
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×