Jenkins 游戏打包部署流程(以 Unity 游戏为例)
以下是使用 Jenkins 自动化构建和部署 Unity 游戏的完整流程,涵盖 代码拉取、Unity 打包、测试、部署到服务器 等关键步骤。
1. 环境准备
工具/环境 |
说明 |
Jenkins |
主 CI/CD 服务器 |
Unity |
安装 Unity Hub 和指定版本 Unity Editor |
Git |
版本控制(GitHub/GitLab) |
构建节点 |
Windows/Linux 代理机(用于 Unity 打包) |
部署目标 |
FTP/SSH/云存储(如阿里云 OSS) |
2. Jenkins 配置
(1) 安装必要插件
- Git Plugin(拉取代码)
- Unity3d Plugin(Unity 构建支持)
- Pipeline(定义 CI/CD 流程)
- Publish Over SSH(远程部署)
- Email Extension(构建通知)
(2) 配置 Unity 环境
在 Jenkins 全局工具中配置 Unity:
- Manage Jenkins → Global Tool Configuration
- 指定 Unity 安装路径(如
C:\Program Files\Unity\Hub\Editor\2021.3.0f1\Editor\Unity.exe
)
添加 Unity License:
- 在构建节点上提前激活 Unity 许可证(命令行或手动激活)。
3. 创建 Jenkins Pipeline
在项目根目录创建 **Jenkinsfile
**,定义构建流程:
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| pipeline { agent any
environment { UNITY_PATH = "C:/Program Files/Unity/Hub/Editor/2021.3.0f1/Editor/Unity.exe" BUILD_TARGET = "Win64" OUTPUT_DIR = "build/${BUILD_TARGET}" }
stages { stage('Checkout') { steps { git branch: 'main', url: 'https://github.com/your-game-repo.git' } }
stage('Build') { steps { script { bat "rmdir /s /q ${OUTPUT_DIR}"
bat """ "${UNITY_PATH}" \ -batchmode \ -nographics \ -silent-crashes \ -logFile "unity-build.log" \ -projectPath "." \ -buildTarget ${BUILD_TARGET} \ -executeMethod BuildScript.PerformBuild \ -quit """ } } post { success { archiveArtifacts artifacts: "${OUTPUT_DIR}/**/*" } failure { emailext body: 'Unity 构建失败,请检查日志!', subject: 'Jenkins 构建失败', to: 'team@example.com' } } }
stage('Test') { steps { bat "nunit-console Tests/bin/Debug/Tests.dll" } }
stage('Deploy') { steps { script { sshPublisher( publishers: [ sshPublisherDesc( configName: 'game-server', transfers: [ sshTransfer( sourceFiles: "${OUTPUT_DIR}/**", remoteDirectory: "/var/www/game", execCommand: "chmod -R 755 /var/www/game" ) ] ) ] )
withCredentials([usernamePassword(credentialsId: 'oss-credentials', usernameVariable: 'OSS_KEY', passwordVariable: 'OSS_SECRET')]) { sh """ aliyun oss cp ${OUTPUT_DIR} oss://your-bucket/game/ --recursive \ --access-key-id ${OSS_KEY} \ --access-key-secret ${OSS_SECRET} """ } } } } } }
|
4. Unity 构建脚本
在 Unity 项目中创建 **BuildScript.cs
**(放在 Assets/Editor/
目录下):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| using UnityEditor; using System.IO;
public static class BuildScript { public static void PerformBuild() { string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "build/" + EditorUserBuildSettings.activeBuildTarget); if (!Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir);
BuildPipeline.BuildPlayer( scenes: EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray(), locationPathName: Path.Combine(outputDir, "Game.exe"), target: EditorUserBuildSettings.activeBuildTarget, options: BuildOptions.None ); } }
|
5. 触发构建
(1) 手动触发
- 在 Jenkins 界面点击 Build Now。
(2) 自动触发(Git Webhook)
- 在 GitHub/GitLab 设置 Webhook:
- Payload URL:
http://<JENKINS_IP>/github-webhook/
- 在 Jenkins 配置触发器:
- Build Triggers → GitHub hook trigger for GITScm polling。
6. 常见问题
问题 |
解决方案 |
Unity 打包失败 |
检查 unity-build.log ,确保 License 已激活 |
构建节点无 Unity |
在 Agent 节点安装 Unity Editor |
SSH 部署失败 |
检查 Jenkins 的 Publish Over SSH 插件配置 |
Android/iOS 打包问题 |
配置 JDK/NDK/Xcode 环境 |
7. 优化建议
- 使用 Docker 镜像:
- 预装 Unity 的 Docker 镜像(如
unityci/editor
)可避免环境问题。1 2 3 4 5 6
| agent { docker { image 'unityci/editor:2021.3.0f1-android-1.0.0' args '-v /tmp:/tmp' } }
|
- 并行构建多平台:
- 使用
parallel
阶段同时构建 Win/Android/iOS。
- 存档 APK/IPA:
- 使用
archiveArtifacts
保存构建产物。
8. 最终效果
- 代码提交 → 自动触发 Jenkins → Unity 打包 → 测试 → 部署到服务器
- 团队收到构建成功/失败邮件通知
如果有具体需求(如 Android 签名、iOS 证书配置),可以进一步调整流程! 🎮