.NET Core 3.0 預覽版發佈已經好些時日了,博客園也已將其用於生產環境中,可見 .NET Core 日趨成熟git
迴歸正題,你想蓋大樓嗎?想 GitHub 首頁一片綠嗎?今天拿她玩玩自動提交代碼至 GitHub。github
dotnet new --install "Microsoft.DotNet.Web.ProjectTemplates.3.0" dotnet new worker
直接使用 .NET CLI 建立一個 Work Service 的項目shell
dotnet new worker -o AutomaticPush
用 Visual Studio 2019 打開項目能夠看到如下代碼json
// Program.cs public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); }); }
CreateHostBuilder
建立 Host 並在ConfigureServices
中調用.AddHostedService<Worker>()
// Worker.cs public class Worker : BackgroundService { private readonly ILogger<Worker> _logger; public Worker(ILogger<Worker> logger) { _logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(1000, stoppingToken); } } }
Worker 繼承了 BackgroundService,在 override ExecuteAsync 方法中完成自動提交的代碼async
.NET 下操做 Git 須要用到一個庫 LibGit2Sharp
,同時支持 .NET Framework 及 .NET Coreide
在項目中安裝使用ui
Install-Package LibGit2Sharp
Repository.Init(@"D:\Work")
在指定路徑建立一個新的 Git 倉庫,至關於 git init
url
Repository.Clone("https://github.com/Meowv/Blog.git", @"D:\Work")
拉取一個遠程倉庫到本地,至關於 git clone
code
using (var repo = new Repository(@"D:\Blog")){}
打開本地存在的 Git 倉庫orm
獲取 Branch
using (var repo = new Repository(@"D:\Blog")) { var branches = repo.Branches; foreach (var item in branches) { } }
獲取 Commits
using (var repo = new Repository(@"D:\Blog")) { foreach (var commit in repo.Commits) { } }
獲取 Tags
using (var repo = new Repository(@"D:\Blog")) { foreach (var commit in repo.Tags) { } }
更多操做請移步 https://github.com/libgit2/libgit2sharp
有了以上基礎,就能夠實現自動生成文件,push 代碼到 GitHub 了。
新建一個配置文件,存放咱們 GitHub 倉庫以及帳號密碼等重要信息
{ "repository": "本地git倉庫絕對路徑", "username": "GitHub帳號", "password": "GitHub密碼", "name": "提交人", "email": "郵箱" }
在ExecuteAsync中讀取配置文件信息
var configurationRoot = new ConfigurationBuilder().AddJsonFile("config.json").Build(); var path = configurationRoot["repository"]; var username = configurationRoot["username"]; var password = configurationRoot["password"]; var name = configurationRoot["name"]; var email = configurationRoot["email"];
git會自動檢測文件變化,因此就先自動按日期建立.log文件,不斷生成內容而後提交
while (!stoppingToken.IsCancellationRequested) { var fileName = $"{DateTime.Now.ToString("dd")}.log"; var content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // 寫入內容 WriteText(path, fileName, content); using (var repo = new Repository(path)) { // Stage the file Commands.Stage(repo, "*"); // Create the committer's signature and commit var author = new Signature(name, email, DateTime.Now); var committer = author; // Commit to the repository var commit = repo.Commit(content, author, committer); // git push var options = new PushOptions { CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => { return new UsernamePasswordCredentials() { Username = username, Password = password }; }) }; repo.Network.Push(repo.Branches["master"], options); } Console.WriteLine(content); // 等待60秒繼續執行... await Task.Delay(60000, stoppingToken); } private static void WriteText(string path, string fileName, string content) { path = Path.Combine(path, DateTime.Now.ToString(@"yyyy\\MM")); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var filePath = Path.Combine(path, fileName); using var fs = new FileStream(filePath, FileMode.Append); using var sw = new StreamWriter(fs); sw.WriteLine(content); }
至此,整個代碼編寫部分結束,項目發佈後能夠選擇sc.exe註冊爲Windows服務,在這裏推薦使用nssm(一個服務封裝程序),好了,趕忙蓋大樓去吧~~