Skip to content

Commit

Permalink
在目标进程环境变量中设置BasePath
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 7, 2024
1 parent 447f861 commit 2ee4cda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 17 additions & 1 deletion Stardust/Deployment/ZipDeploy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Xml.Serialization;
using NewLife;
using NewLife.Log;

Expand Down Expand Up @@ -30,6 +31,9 @@ public class ZipDeploy
/// <summary>用户。以该用户执行应用</summary>
public String? UserName { get; set; }

/// <summary>环境变量。启动应用前设置的环境变量</summary>
public String? Environments { get; set; }

/// <summary>覆盖文件。需要拷贝覆盖已存在的文件或子目录,支持*模糊匹配,多文件分号隔开。如果目标文件不存在,配置文件等自动拷贝</summary>
public String? Overwrite { get; set; }

Expand Down Expand Up @@ -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);
Expand All @@ -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";
Expand All @@ -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())
{
Expand Down
4 changes: 3 additions & 1 deletion Stardust/Managers/ServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -270,6 +270,7 @@ public Boolean Start()
FileName = file,
WorkingDirectory = workDir,
UserName = service.UserName,
Environments = service.Environments,
Overwrite = DeployInfo?.Overwrite,

Tracer = Tracer,
Expand Down Expand Up @@ -327,6 +328,7 @@ public Boolean Start()
// true时目标控制台独立窗口,不会一起退出;
UseShellExecute = false,
};
si.EnvironmentVariables["BasePath"] = workDir;

// 环境变量。不能用于ShellExecute
if (service.Environments.IsNullOrEmpty() && !si.UseShellExecute)
Expand Down

0 comments on commit 2ee4cda

Please sign in to comment.