基于DotNetty实现自动发布 - 自动检测代码变化

前言

很抱歉没有实现上一篇的目标:一键发布,因为工作量超出了预期,本次只实现了 Git 代码变化检测

已完成的功能

  • 解决方案的项目发现与配置
  • 首次发布需要手动处理
  • 自动检测代码变化并解析出待发布的文件



简要说明

  • 只需要填写解决方案的 Git 仓储路径即可自动发现项目 (通过查找 .csproj 文件实现)

  • 自动发现 Web 项目 (通过判断项目根目录是否包含 Web.config 实现) PS: 只支持 .NET Framework

  • 需要配置 Web 项目的发布目录, 编译还需要手动执行

  • 首次发布需要手动执行, 然后保存此次发布对应的 Git 提交 ID

  • 后续发布,可以根据上次发布记录,自动解析出待待发布的文件

部分代码

发现解决方案

C# 复制代码
private static Solution DetectSolution(string gitRepoPath)
{
    string[] solutionFilePaths = Directory.GetFiles(gitRepoPath, "*.sln", SearchOption.AllDirectories);
    if (solutionFilePaths == null || solutionFilePaths.Length == 0)
    {
        throw new Exception("未找到解决方案");
    }
    string[] projectFilePaths = Directory.GetFiles(gitRepoPath, "*.csproj", SearchOption.AllDirectories);
    if (projectFilePaths == null || projectFilePaths.Length == 0)
    {
        throw new Exception("未找到项目");
    }

    var solutionFilePath = solutionFilePaths[0];
    var solutionDir = Path.GetDirectoryName(solutionFilePath);
    var solutionName = Path.GetFileNameWithoutExtension(solutionFilePath);

    var solution = new Solution
    {
        GitRepositoryPath = gitRepoPath,
        SolutionDir = solutionDir!,
        SolutionName = solutionName
    };

    foreach (var projectFilePath in projectFilePaths)
    {
        var projectDir = Path.GetDirectoryName(projectFilePath);
        var projectName = Path.GetFileNameWithoutExtension(projectFilePath);
        var webConfigFiles = Directory.GetFiles(projectDir!, "web.config", SearchOption.TopDirectoryOnly);
        var project = new Project
        {
            ProjectDir = projectDir!,
            ProjectName = projectName,
            IsWeb = webConfigFiles != null && webConfigFiles.Length > 0,
            SolutionName = solutionName,
            ReleaseDir = string.Empty
        };
        solution.Projects.Add(project);
    }
    return solution;
}

获取自上次发布以来的改动

C# 复制代码
public static List<PatchEntryChanges> GetChangesSinceLastPublish(string repoPath, string? lastCommitId = null)
{
    var repo = GetRepo(repoPath);

    //获取上次发布的提交
    Commit? lastCommit = null;
    if (!string.IsNullOrEmpty(lastCommitId))
    {
        lastCommit = repo.Lookup<Commit>(lastCommitId);
        if (lastCommit == null)
        {
            throw new Exception("无法获取上次发布的提交记录");
        }
    }

    //获取自上次提交以来的改动
    var diff = repo.Diff.Compare<Patch>(lastCommit?.Tree, DiffTargets.Index);
    return [.. diff];
}

从 Git 修改记录提取出待发布文件

C# 复制代码
private List<DeployFileInfo> GetPublishFiles(IEnumerable<string> changedFilePaths)
{
    var fileInfos = new List<DeployFileInfo>(changedFilePaths.Count());
    foreach (string changedPath in changedFilePaths)
    {
        var fi = DeployFileInfo.Create(changedPath);
        if (fi.IsUnKnown) continue;
        fileInfos.Add(fi);
    }
    foreach (var fi in fileInfos)
    {
        fi.ChangedFileAbsolutePath = Path.Combine(GitRepositoryPath, fi.ChangedFileRelativePath);

        //所属项目
        var project = Projects
            .Where(a => fi.ChangedFileRelativePath.Contains(a.ProjectName, StringComparison.OrdinalIgnoreCase))
            .FirstOrDefault();
        if (project == null) continue;

        fi.ProjectName = project.ProjectName;
        if (fi.IsDLL)
        {
            fi.FileName = $"{project.ProjectName}.dll";
            fi.PublishFileRelativePath = $"bin\\{fi.FileName}";
        }
        else
        {
            fi.PublishFileRelativePath = fi.ChangedFileAbsolutePath.Replace(project.ProjectDir, "").TrimStart(Path.DirectorySeparatorChar);
        }
        fi.PublishFileAbsolutePath = Path.Combine(webProject!.ReleaseDir, fi.PublishFileRelativePath);

        //Logger.Info(fi.ToJsonString(true));
    }
    //按照 PublishFileAbsolutePath 去重
    return fileInfos.Distinct(new DeployFileInfoComparer()).ToList();
}

设置项目发布路径

C# 复制代码
private async Task OkSetProjectReleaseDir()
{
    if (string.IsNullOrEmpty(ReleaseDir) || !Directory.Exists(ReleaseDir))
    {
        Growl.ClearGlobal();
        Growl.ErrorGlobal("请正确设置项目发布路径");
        return;
    }

    var solutionRepo = Program.AppHost.Services.GetRequiredService<SolutionRepository>();
    await solutionRepo.UpdateProjectReleaseDir(Id, ReleaseDir);

    setProjectReleaseDirDialog?.Close();

    Growl.SuccessGlobal("操作成功");
}

总结

本篇主要实现了 Git 代码变化的自动检测

代码仓库

项目暂且就叫 OpenDeploy

欢迎大家拍砖,Star

下一步

计划下一步,实现一键发布,把待发布的文件一次性打包通过 DotNetty 发送到服务器

相关推荐
三千道应用题1 小时前
WPF学习笔记(25)MVVM框架与项目实例
wpf
江山如画,佳人北望1 小时前
C#程序入门
开发语言·windows·c#
与火星的孩子对话2 小时前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
future14122 小时前
C#每日学习日记
java·学习·c#
军训猫猫头5 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
厦门德仔8 小时前
【WPF】WPF(样式)
android·java·wpf
葬歌倾城21 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json
Eiceblue1 天前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
小小小小王王王1 天前
hello判断
开发语言·c#
金增辉1 天前
基于C#的OPCServer应用开发,引用WtOPCSvr.dll
c#