GitHub自動化部署(CD) asp.net core 5.0 專案(免費空間)

阿新發表於2021-08-24

這裡我簡單介紹一下使用Github自動化部署自己專案到Heroku雲伺服器上,Heroku竟然是一個很非常老牌的雲平臺服務商,竟然還沒聽說過,網上一查2010被Salesforce收購,網上有很多關於asp.net core 使用(Docker和CircleCI)部署的文章,都比較舊了,也比較麻煩,必須先配置Dockerfile再Build成功映象再部署,但現在都是2021年了.net core又那麼火什麼專案都再講CI/CD,沒有理由不支援直接部署的,所以經過自己的嘗試成功了,那就寫一篇blog和大家分享一下。讓我們開始吧。

Heroku 註冊賬號

Heroku: Cloud Application Platform
免費註冊,這裡我就不詳細說明了,郵箱不要用國內廠商的就行

新建 Application

Click [New] button create new app

Input App name and Click [Create app]

配置 Application Deploy/Settings

Deployment method select GitHub(Connect to GitHub)
Search project name

Click [Connect]
你可以選擇 [Enable Automatic Deploys] 如果不勾選 [wait for CI to pass before deploy] 預設 main分支一有Push或是Merge PR就會自動部署, [wait for CI to pass before deploy]的意思就是先執行整合測試指令碼通過了在部署。

這是如果想 Click [Deploy Branch],是無法部署成功,會提示錯誤,原因就是.net core 需要.net framework編譯

切換到[Settings], Click [Add buildpack]

輸入: [https://github.com/jincod/dotnetcore-buildpack]

現在你再次Click [Deploy Branch] 部署就可以完成了

可以訪問你的網站了[http://razorapp.herokuapp.com/authorization/profile]

回到我們Github專案中你就會看到你配置自動部署的application

在這個過程中還遇到一個錯誤

System.IO.DirectoryNotFoundException: /app/heroku_output/Files/

2021-08-24T12:12:01.321889+00:00 heroku[web.1]: Starting process with command `cd /app/heroku_output && ./SmartAdmin.WebUI`
2021-08-24T12:12:05.471825+00:00 app[web.1]: [12:12:05 WRN] No XML encryptor configured. Key {094ab54d-7f72-4c6f-90b3-ba4e1d9a0e49} may be persisted to storage in unencrypted form.
2021-08-24T12:12:05.512813+00:00 app[web.1]: [12:12:05 FTL] Application startup exception
2021-08-24T12:12:05.512814+00:00 app[web.1]: System.IO.DirectoryNotFoundException: /app/heroku_output/Files/
2021-08-24T12:12:05.512825+00:00 app[web.1]:    at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
2021-08-24T12:12:05.512829+00:00 app[web.1]:    at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
2021-08-24T12:12:05.512830+00:00 app[web.1]:    at SmartAdmin.WebUI.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /tmp/build_08828386/src/SmartAdmin.WebUI/Startup.cs:line 104
2021-08-24T12:12:05.512831+00:00 app[web.1]:    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
2021-08-24T12:12:05.512831+00:00 app[web.1]:    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
2021-08-24T12:12:05.512832+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
2021-08-24T12:12:05.512832+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
2021-08-24T12:12:05.512832+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
2021-08-24T12:12:05.512836+00:00 app[web.1]:    at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
2021-08-24T12:12:05.512836+00:00 app[web.1]:    at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
2021-08-24T12:12:05.512836+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
2021-08-24T12:12:05.516757+00:00 app[web.1]: Unhandled exception. System.IO.DirectoryNotFoundException: /app/heroku_output/Files/
2021-08-24T12:12:05.516757+00:00 app[web.1]:    at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
2021-08-24T12:12:05.516757+00:00 app[web.1]:    at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
2021-08-24T12:12:05.516758+00:00 app[web.1]:    at SmartAdmin.WebUI.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /tmp/build_08828386/src/SmartAdmin.WebUI/Startup.cs:line 104
2021-08-24T12:12:05.516758+00:00 app[web.1]:    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
2021-08-24T12:12:05.516758+00:00 app[web.1]:    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
2021-08-24T12:12:05.516758+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
2021-08-24T12:12:05.516759+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
2021-08-24T12:12:05.516759+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
2021-08-24T12:12:05.516760+00:00 app[web.1]:    at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
2021-08-24T12:12:05.516760+00:00 app[web.1]:    at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
2021-08-24T12:12:05.516760+00:00 app[web.1]:    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
2021-08-24T12:12:05.516760+00:00 app[web.1]:    at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
2021-08-24T12:12:05.516761+00:00 app[web.1]:    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
2021-08-24T12:12:05.516761+00:00 app[web.1]:    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
2021-08-24T12:12:05.516761+00:00 app[web.1]:    at SmartAdmin.WebUI.Program.Main(String[] args) in /tmp/build_08828386/src/SmartAdmin.WebUI/Program.cs:line 57
2021-08-24T12:12:05.516762+00:00 app[web.1]:    at SmartAdmin.WebUI.Program.<Main>(String[] args)

沒有找Files這個目錄
對應這段程式碼

 app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Files")),
                RequestPath = new PathString("/Files")
            });

我的解決方案是
在啟動時新建這個目錄

最後

Keeping Coding, Enjoy Coding.
如果覺得有幫助請點個贊,
我在推一下這個專案真的非常不錯,喜歡輕量級開發的朋友和關注瞭解一下
neozhu/RazorPageCleanArchitecture

相關文章