From 2ee4cda0f4a811d3308fd609cfc76eedd7c6fae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Mon, 8 Jul 2024 00:09:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=9B=AE=E6=A0=87=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E4=B8=AD=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?BasePath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stardust/Deployment/ZipDeploy.cs | 18 +++++++++++++++++- Stardust/Managers/ServiceController.cs | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Stardust/Deployment/ZipDeploy.cs b/Stardust/Deployment/ZipDeploy.cs index 5c672572..2df06485 100644 --- a/Stardust/Deployment/ZipDeploy.cs +++ b/Stardust/Deployment/ZipDeploy.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Xml.Serialization; using NewLife; using NewLife.Log; @@ -30,6 +31,9 @@ public class ZipDeploy /// 用户。以该用户执行应用 public String? UserName { get; set; } + /// 环境变量。启动应用前设置的环境变量 + public String? Environments { get; set; } + /// 覆盖文件。需要拷贝覆盖已存在的文件或子目录,支持*模糊匹配,多文件分号隔开。如果目标文件不存在,配置文件等自动拷贝 public String? Overwrite { get; set; } @@ -212,7 +216,7 @@ public Boolean Execute(Int32 msWait = 3_000) } // 在环境变量中设置BasePath,不用担心影响当前进程,因为PathHelper仅读取一次 - Environment.SetEnvironmentVariable("BasePath", rundir.FullName); + //Environment.SetEnvironmentVariable("BasePath", rundir.FullName); ExecuteFile = runfile.FullName; WriteLog("运行文件 {0}", runfile); @@ -227,6 +231,8 @@ public Boolean Execute(Int32 msWait = 3_000) // true时目标控制台独立窗口,不会一起退出; UseShellExecute = false, }; + si.EnvironmentVariables["BasePath"] = rundir.FullName; + if (runfile.Extension.EqualIgnoreCase(".dll")) { si.FileName = "dotnet"; @@ -243,6 +249,16 @@ public Boolean Execute(Int32 msWait = 3_000) Process.Start("chmod", $"+x {runfile.FullName}").WaitForExit(5_000); } + // 环境变量。不能用于ShellExecute + if (Environments.IsNullOrEmpty() && !si.UseShellExecute) + { + foreach (var item in Environments.SplitAsDictionary("=", ";")) + { + if (!item.Key.IsNullOrEmpty()) + si.EnvironmentVariables[item.Key] = item.Value; + } + } + // 指定用户时,以特定用户启动进程 if (!UserName.IsNullOrEmpty()) { diff --git a/Stardust/Managers/ServiceController.cs b/Stardust/Managers/ServiceController.cs index b2db3c3e..fad2c91e 100644 --- a/Stardust/Managers/ServiceController.cs +++ b/Stardust/Managers/ServiceController.cs @@ -138,7 +138,7 @@ public Boolean Start() var isZip = src.EndsWithIgnoreCase(".zip"); // 在环境变量中设置BasePath,不用担心影响当前进程,因为PathHelper仅读取一次 - Environment.SetEnvironmentVariable("BasePath", workDir); + //Environment.SetEnvironmentVariable("BasePath", workDir); // 工作模式 switch (service.Mode) @@ -270,6 +270,7 @@ public Boolean Start() FileName = file, WorkingDirectory = workDir, UserName = service.UserName, + Environments = service.Environments, Overwrite = DeployInfo?.Overwrite, Tracer = Tracer, @@ -327,6 +328,7 @@ public Boolean Start() // true时目标控制台独立窗口,不会一起退出; UseShellExecute = false, }; + si.EnvironmentVariables["BasePath"] = workDir; // 环境变量。不能用于ShellExecute if (service.Environments.IsNullOrEmpty() && !si.UseShellExecute)