Jenkins 调用 Shell 脚本,在Shell脚本中调用 Unity 类方法,传递参数给Unity
还是以 Jenkins 调用 Pipeline 方式 调用 Shell
Pipeline 脚本如下
pipeline {
agent any
stages {
stage('Test Parameter') {
steps {
script {
// shell 脚本目录
SHELL_UNITY_PATH="${env.WORKSPACE}/shell/testShellUnity.sh"
// 给 shell 脚本添加权限并执行
sh "chmod +x ${SHELL_UNITY_PATH} && ${SHELL_UNITY_PATH}"
}
}
}
}
}
shell 脚本如下
#!/bin/sh
#!/bin/bash
echo "this is testShellUnity.sh"
# 输出工作目录
echo "WORKSPACE=${WORKSPACE}"
# Unity 安装目录
UNITY_PATH=/Applications/Unity/Hub/Editor/2022.3.26f1/Unity.app/Contents/MacOS/Unity
# Unity 项目目录 Assets、Library、ProjectSettings 文件夹在 PROJECT_PATH 路径下
PROJECT_PATH="${WORKSPACE}/Project"
echo "PROJECT_PATH=${PROJECT_PATH}"
# 通过 export 将变量导出给 Unity 使用
# 导出到 Unity 后都是 字符串
# 在 Unity 中通过 string value = Environment.GetEnvironmentVariable(key); 获取
# bool 类型的传递过去是字符串 "true" 和 "false"
export MY_PARAM_1="ttttttAAAAAA"
export MY_PARAM_2=1000
export MY_PARAM_3=5.1
export MY_PARAM_4=true
export MY_PARAM_5=false
# 下面是调用 Unity 的命令
# 在 Assets 文件夹下任意目录 创建文件夹 Editor
# 新建 PojectExport.cs 删除继承 MonoBehaviour
# 添加一个 public static void Export() 方法
# 下面命令通过 PojectExport.Export 调用
$UNITY_PATH -projectPath $PROJECT_PATH \
-buildTarget android \
-executeMethod PojectExport.Export \
-logfile - \
-batchMode -quit \
-GMMode
echo "this is testShellUnity.sh end"
PojectExport.cs 目录如下 Assets\Editor\PojectExport.cs
代码如下
using UnityEngine;
public class PojectExport
{
public static void Export()
{
Debug.LogError("PojectExport Export");
// 获取环境变量
string myParam_1 = EnvironmentUtil.GetString("MY_PARAM_1", string.Empty);
Debug.LogError("myParam_1=" + myParam_1);
int myParam_2 = EnvironmentUtil.GetInt("MY_PARAM_2", 0);
Debug.LogError("myParam_2=" + myParam_2);
float myParam_3 = EnvironmentUtil.GetFloat("MY_PARAM_3", 0);
Debug.LogError("myParam_3=" + myParam_3);
bool myParam_4 = EnvironmentUtil.GetBool("MY_PARAM_4", false);
Debug.LogError("myParam_4=" + myParam_4);
bool myParam_5 = EnvironmentUtil.GetBool("MY_PARAM_5", false);
Debug.LogError("myParam_5=" + myParam_5);
string myParam_6 = EnvironmentUtil.GetString("MY_PARAM_6", string.Empty);
Debug.LogError("myParam_6=" + myParam_6);
}
}
Shell 传递给 Unity 的参数,通过 Environment.GetEnvironmentVariable(string key); 方法获取
添加一个 EnvironmentUtil.cs,封装几个方法
using System;
public class EnvironmentUtil
{
public static string GetString(string key, string defaultValue = "")
{
string value = Environment.GetEnvironmentVariable(key);
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
return value;
}
public static int GetInt(string key, int defaultValue = 0)
{
string value = GetString(key, string.Empty);
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
return int.Parse(value);
}
public static float GetFloat(string key, int defaultValue = 0)
{
string value = GetString(key, string.Empty);
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
return float.Parse(value);
}
public static bool GetBool(string key, bool defaultValue = false)
{
string value = GetString(key);
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
return value.CompareTo("true") == 0;
}
}
在Jenkins 中执行构建
输出日志如下
Console Output
Download
Copy
View as plain text
Started by user admin
Lightweight checkout support not available, falling back to full checkout.
Checking out git https://github.com/LIQIANGEASTSUN/JenkinsTest.git into /Users/liqiang/.jenkins/workspace/TestProject1@script/a82089facc9f5b750180801ee6442a8ef978414d304ab5cc580f6728c566c0a8 to read Pipeline/testPipelineShell
The recommended git tool is: NONE
Warning: CredentialId "xxx_credentials" could not be found.
> git rev-parse --resolve-git-dir /Users/liqiang/.jenkins/workspace/TestProject1@script/a82089facc9f5b750180801ee6442a8ef978414d304ab5cc580f6728c566c0a8/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/LIQIANGEASTSUN/JenkinsTest.git # timeout=10
Fetching upstream changes from https://github.com/LIQIANGEASTSUN/JenkinsTest.git
> git --version # timeout=10
> git --version # 'git version 2.39.5 (Apple Git-154)'
> git fetch --tags --force --progress -- https://github.com/LIQIANGEASTSUN/JenkinsTest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master_2^{
commit} # timeout=10
Checking out Revision 7a1f6a55772e538c0f6384dd6e0c7373f327baa3 (refs/remotes/origin/master_2)
> git config core.sparsecheckout # timeout=10
> git checkout -f 7a1f6a55772e538c0f6384dd6e0c7373f327baa3 # timeout=10
Commit message: "modify"
> git rev-list --no-walk dd4c9239923e866adea0894541f88219ec76ef3a # timeout=10
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/liqiang/.jenkins/workspace/TestProject1
[Pipeline] {
[Pipeline] stage
[Pipeline] {
(Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
Warning: CredentialId "xxx_credentials" could not be found.
> git rev-parse --resolve-git-dir /Users/liqiang/.jenkins/workspace/TestProject1/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/LIQIANGEASTSUN/JenkinsTest.git # timeout=10
Fetching upstream changes from https://github.com/LIQIANGEASTSUN/JenkinsTest.git
> git --version # timeout=10
> git --version # 'git version 2.39.5 (Apple Git-154)'
> git fetch --tags --force --progress -- https://github.com/LIQIANGEASTSUN/JenkinsTest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master_2^{
commit} # timeout=10
Checking out Revision 7a1f6a55772e538c0f6384dd6e0c7373f327baa3 (refs/remotes/origin/master_2)
> git config core.sparsecheckout # timeout=10
> git checkout -f 7a1f6a55772e538c0f6384dd6e0c7373f327baa3 # timeout=10
Commit message: "modify"
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] {
(Test Parameter)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ chmod +x /Users/liqiang/.jenkins/workspace/TestProject1/shell/testShellUnity.sh
+ /Users/liqiang/.jenkins/workspace/TestProject1/shell/testShellUnity.sh
this is testShellUnity.sh
WORKSPACE=/Users/liqiang/.jenkins/workspace/TestProject1
PROJECT_PATH=/Users/liqiang/.jenkins/workspace/TestProject1/Project
Unity Editor version: 2022.3.26f1 (ec6cd8118806)
Branch: 2022.3/staging
Build type: Release
Batch mode: YES
macOS version: Version 14.5 (Build 23F79)
Darwin version: 23.5.0
Architecture: x86_64
Running under Rosetta: NO
Available memory: 16384 MB
[Licensing::Module] Trying to connect to existing licensing client channel...
[Licensing::IpcConnector] Successfully connected to the License Client on channel: "LicenseClient-liqiang" at "2025-02-18T02:11:52.515186Z"
[Licensing::Client] Handshaking with LicensingClient:
Version: 1.15.4+eee609c
Session Id: 0f943bb231324e8abaedbf89b92287db
Correlation Id: 792bf8b516b3ea4b0c85f69d5788be18
External correlation Id: 1305950576279334833
Machine Id: VKC2otBD0Cn/2amvMR8I7ecUq24=
[Licensing::Module] Successfully connected to LicensingClient on channel: "LicenseClient-liqiang" (connect: 0.00s, validation: 0.01s, handshake: 0.60s)
[Licensing::IpcConnector] Successfully connected to the License Notification on channel: "LicenseClient-liqiang-notifications" at "2025-02-18T02:11:53.12367Z"
[Licensing::Module] Error: Access token is unavailable; failed to update
[Licensing::Client] Successfully updated license
[Licensing::Client] Successfully resolved entitlements
[Licensing::Module] Serial number assigned to: "F4-S9H8-F62K-MHMM-2792-XXXX"
Pro License: NO
Launching external process: /Applications/Unity/Hub/Editor/2022.3.26f1/Unity.app/Contents/Resources/PackageManager/Server/UnityPackageManager
COMMAND LINE ARGUMENTS:
/Applications/Unity/Hub/Editor/2022.3.26f1/Unity.app/Contents/MacOS/Unity
-projectPath
/Users/liqiang/.jenkins/workspace/TestProject1/Project
-buildTarget
android
-executeMethod
PojectExport.Export
-logfile
-
-batchMode
-quit
-GMMode
Successfully changed project path to: /Users/liqiang/.jenkins/workspace/TestProject1/Project
/Users/liqiang/.jenkins/workspace/TestProject1/Project
[UnityMemory] Configuration Parameters - Can be set up in boot.config
"memorysetup-bucket-allocator-granularity=16"
"memorysetup-bucket-allocator-bucket-count=8"
"memorysetup-bucket-allocator-block-size=33554432"
"memorysetup-bucket-allocator-block-count=8"
"memorysetup-main-allocator-block-size=16777216"
"memorysetup-thread-allocator-block-size=16777216"
"memorysetup-gfx-main-allocator-block-size=16777216"
"memorysetup-gfx-thread-allocator-block-size=16777216"
"memorysetup-cache-allocator-block-size=4194304"
"memorysetup-typetree-allocator-block-size=2097152"
"memorysetup-profiler-bucket-allocator-granularity=16"
"memorysetup-profiler-bucket-allocator-bucket-count=8"
"memorysetup-profiler-bucket-allocator-block-size=33554432"
"memorysetup-profiler-bucket-allocator-block-count=8"
"memorysetup-profiler-allocator-block-size=16777216"
"memorysetup-profiler-editor-allocator-block-size=1048576"
"memorysetup-temp-allocator-size-main=16777216"
"memorysetup-job-temp-allocator-block-size=2097152"
"memorysetup-job-temp-allocator-block-size-background=1048576"
"memorysetup-job-temp-allocator-reduction-small-platforms=262144"
"memorysetup-allocator-temp-initial-block-size-main=262144"
"memorysetup-allocator-temp-initial-block-size-worker=262144"
"memorysetup-temp-allocator-size-background-worker=32768"
"memorysetup-temp-allocator-size-job-worker=262144"
"memorysetup-temp-allocator-size-preload-manager=33554432"
"memorysetup-temp-allocator-size-nav-mesh-worker=65536"
"memorysetup-temp-allocator-size-audio-worker=65536"
"memorysetup-temp-allocator-size-cloud-worker=32768"
"memorysetup-temp-allocator-size-gi-baking-worker=262144"
"memorysetup-temp-allocator-size-gfx=262144"
Player connection [140704533184448] Host "[IP] 10.0.179.83 [Port] 55504 [Flags] 2 [Guid] 2195203405 [EditorId] 2195203405 [Version] 1048832 [Id] OSXEditor(0,liqiangs-Mac-mini.local) [Debug] 1 [PackageName] OSXEditor [ProjectName] Editor" joined multi-casting on [225.0.0.222:54997]...
Player connection [140704533184448] Host "[IP] 10.0.179.83 [Port] 55504 [Flags] 2 [Guid] 2195203405 [EditorId] 2195203405 [Version] 1048832 [Id] OSXEditor(0,liqiangs-Mac-mini.local) [Debug] 1 [PackageName] OSXEditor [ProjectName] Editor" joined alternative multi-casting on [225.0.0.222:34997]...
[Physics::Module] Initialized MultithreadedJobDispatcher with 11 workers.
[Package Manager] UpmClient::Connect -- Connected to IPC stream "Upm-1387" after 0.3 seconds.
[Licensing::Client] Successfully resolved entitlements
Packages were changed.
Update Mode: updateDependencies
The following packages were added:
[email protected]
The following packages were updated:
com.unity.collab-proxy from version 2.2.0 to 2.3.1
com.unity.ide.rider from version 3.0.27 to 3.0.28
com.unity.timeline from version 1.6.5 to 1.7.6
com.unity.visualscripting from version 1.9.1 to 1.9.4
[Package Manager] Restoring resolved packages state from cache
[Licensing::Client] Successfully resolved entitlement details
[Package Manager] Registered 48 packages:
Packages from [https://packages.unity.com]:
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
Built-in packages:
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
[email protected] (location: /Users/liqiang/.jenkins/workspace/TestProject1/Project/Library/PackageCache/[email protected])
com.unity.modules.ai@1.