diff --git a/PublishSite/PublishForm.cs b/PublishSite/PublishForm.cs index a87b63de..17a40a0c 100644 --- a/PublishSite/PublishForm.cs +++ b/PublishSite/PublishForm.cs @@ -20,6 +20,7 @@ using Renci.SshNet.Messages; using System.Management; using System.IO; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; +using System.Management.Automation.Host; namespace PublishSite { @@ -676,14 +677,99 @@ namespace PublishSite } } - private void sshVuePublishBtn_Click(object sender, EventArgs e) + private async void sshVuePublishBtn_Click(object sender, EventArgs e) { + sshVuePublishBtn.Enabled = false; + var filePath = publishFileTBox.Text; + + var realName = Path.GetFileName(filePath); + + var desTinationFoder = jObjectCofig["BackUp"]["VuePublishFolder"].ToString(); + + var storeTempName=$"{ Guid.NewGuid()}_{realName}" ; + + if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) + { + WinformLog($"请选择存在的文件才能开始发布", Color.Red); + return; + } + + WinformLog($"开始上传前端部署文件{realName}..."); + + + await Task.Run(() => + { + + + using (var client = new SshClient(Host, UserName, Password)) + { + client.ConnectionInfo.Encoding = System.Text.Encoding.UTF8; + // 连接到远程服务器 + client.Connect(); + + using (var scpClient = new ScpClient(Host, UserName, Password)) + { + scpClient.Connect(); + + using (var fileStream = new FileStream(filePath, FileMode.Open)) + { + scpClient.Upload(fileStream, @$"/{storeTempName}"); + } + + scpClient.Disconnect(); + + WinformLog($"上传{realName}结束"); + } + + string command = $@"xcopy /Y /Q ""C:\{storeTempName}"" ""{desTinationFoder}"""; + + //拷贝文件到发布文件夹 + using (var cmd = client.CreateCommand(command)) + { + WinformLog(command); + + var output = cmd.Execute(); + + WinformLog(output); + } + + //删除临时文件 + + var delCommand = $"move /Y \"{Path.Combine(desTinationFoder,storeTempName)}\" \"{Path.Combine(desTinationFoder, realName)}\" && del C:\\{storeTempName}"; + using (var cmd = client.CreateCommand(delCommand)) + { + WinformLog(delCommand); + + var output = cmd.Execute(); + + WinformLog(output); + } + + WinformLog($"清除零时文件{storeTempName}完成"); + + // 断开SSH连接 + client.Disconnect(); + + WinformLog($"前端发布成功"); + } + + + }); + + + sshVuePublishBtn.Enabled = true; + } private void sshNetCorePublishBtn_Click(object sender, EventArgs e) { - + var filePath = publishFileTBox.Text; + if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) + { + WinformLog($"请选择存在的文件才能开始发布", Color.Red); + return; + } } private void sshNetCorePublishBtn2_Click(object sender, EventArgs e) diff --git a/PublishSite/appsettings.Development.json b/PublishSite/appsettings.Development.json index d898247d..8c1fecdf 100644 --- a/PublishSite/appsettings.Development.json +++ b/PublishSite/appsettings.Development.json @@ -4,6 +4,7 @@ "DBUser": "sa", "DBPwd": "dev123456DEV", "BackPath": "D:\\Develop\\PublishSite\\发布备份", + "VuePublishFolder": "D:\\Develop\\PublishSite\\IRaCIS.Vue.Web\\dist", "ConnectionStrings": "Server=.;Database=IRaCIS;Trusted_Connection=True;TrustServerCertificate=true" }, "Credential": { @@ -17,5 +18,6 @@ "ServicePort": "7100", "BinPath": "D:\\Develop\\PublishSite\\IRaCIS.NetCore.API\\IRaCIS.Core.API.exe", "Env": "Development" - } + }, + }