Jenkins自动化打包

Jenkins自动化打包

下载安装

我们直接从官网https://www.jenkins.io/download/ 下载所需的Jenkins文件

如上图所示, 选择Windows版本,下面就是一路安装即可,需要注意的是,选择作为系统服务选项, 不要自己设置账号密码登录.

Web配置

安装完根据提示在浏览器打开 http://localhost:8080/ 即可进入Jenkins部署界面

按照上图中的红色路径找到initialAdminPassword文件并打开 将文件内容粘贴进去, 点击继续

这里我们选择推荐的插件进行安装

等待进度条跑完即可

我们选择Skip, 跳过设置继续使用admin用户登录

选择Save and Finish

选择Start using Jenkins

Unity每日定时打包

就是Jenkins的web界面, 我们在里面配置一个自动打包流程, 比如一个定时任务, 每天凌晨自动打包. 下面就演示如何操作

我们选择左边的New Item创建一个任务

按照上图的步骤1,2,3 点击OK之后创建任务

在上图中添加上任务描述, 然后滚动到后面的BuildSteps里面选择Execute Windows batch command

在Command里面填写上要执行的python脚本

点击Save保存

创建打包C#脚本

c# 复制代码
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace Jenkins
{
    public class BuildScript
    {
        [MenuItem("Build/Build for Android")]
        public static void BuildForAndroid()
        {
            var buildPlayerOptions = new BuildPlayerOptions()
            {
                scenes = new[]
                {
                    "Assets/LemonFramework/Jenkins/Sample/Sample.unity"
                },
                locationPathName = "Jenkins.apk",
                target = BuildTarget.Android,
                options = BuildOptions.None
            };
            var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
            var summary = report.summary;
            if (summary.result == BuildResult.Succeeded)
            {
                Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
            }

            if (summary.result == BuildResult.Failed)
            {
                Debug.LogError("Build failed");
            }
        }
    }
}

创建打包Python脚本

python 复制代码
#coding:utf-8
import os
import sys
import subprocess
import time
from pathlib import Path
import subprocess
sys.path.append(os.path.abspath(os.path.join(os.path.realpath(__file__), "../")))

CUR_PATH = os.getcwd()
PROJ_PATH = os.path.join(CUR_PATH, "../")
BRANCH = 'gl-master'

# Unity的执行路径
unity_path = "C:/Program Files/Unity/Editor/Unity.exe"
# Unity项目的路径
project_path = "E:/Projects/Lemon/Lemon.Framework.Jenkins"
# 要执行的Unity编辑器自定义方法的名称,这个方法在Unity编辑器扩展脚本中定义
method_name = "Jenkins.BuildScript.BuildForAndroid"
# 打包后的APK文件路径
apk_output_path = "E:/Projects/Lemon/Lemon.Framework.Jenkins/Jenkins.apk"

# 拼接Unity命令行
cmd = [
    unity_path,
    "-quit",  # 表示Unity完成命令后关闭
    "-batchmode",  # 不显示界面和对话框
    "-nographics",  # 在支持的平台上不初始化图形设备
    "-silent-crashes",  # 自动处理崩溃情况
    "-projectPath", project_path,
    "-executeMethod", method_name,
    "-logFile",  # 可以指定日志文件路径,例如"-logFile", "unity.log"
    "-buildOutput", apk_output_path,
]

# 杀掉unity进程
def kill_unity():
    os.system('taskkill /IM Unity.exe /F')

def git_reset_pull():
	os.chdir(PROJ_PATH)
	cmd = 'git fetch --all' #git 拉取命令
	result = os.system(cmd)	
	cmd = 'git reset --hard HEAD' #git reset命令
	result = os.system(cmd)
	cmd = r"{0}{1}".format("git checkout ",BRANCH)
	result = os.system(cmd)
	cmd = 'git clean -fd' #git clean 命令
	result = os.system(cmd)
	cmd = 'git pull --rebase' #git pull命令
	result = os.system(cmd)

	if result == 0:
		print('git update succes')
	else:
		print('git update fail')

# 调用unity中我们封装的静态函数
def build():    
	# 执行命令行
	# subprocess.call(cmd) 注释掉这行,并替换为下面的代码,以阻塞直到命令完成并捕获输出
	process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	stdout, stderr = process.communicate()

	if process.returncode == 0:
		print("Build succeeded")
		print(stdout.decode("utf-8"))  # 显示标准输出
	else:
		print("Build failed")
		print(stderr.decode("utf-8"))  # 显示错误输出
		sys.exit(1)
 
if __name__ == '__main__':	
	now = time.time() 
	kill_unity()
	#git_reset_pull()
	build()
	print(f'total take time {time.time()-now} seconds')
	print("Done!")

点击左侧Build Now即可生成Android Apk

定时任务

在Configure里设定每天早上6点定时打包,这样一大早有有热乎的apk给QA做测试了

相关推荐
waterHBO2 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七3 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate5 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼5 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio7 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
寻爱的希斯克利夫8 小时前
tomcat 配置jenkins_home 目录
servlet·tomcat·jenkins
redcocal8 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali8 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ9 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.9 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python