GitHub自动化部署(CD) asp.net core 5.0 项目(免费空间)

这里我简单介绍一下使用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]

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

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

可以访问你的网站了[//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