jincod / dotnetcore-buildpack

Heroku .NET Core Buildpack
MIT License
751 stars 366 forks source link
dotnetcore heroku-buildpack

Heroku .NET Core Buildpack

This is the Heroku buildpack for ASP.NET Core.

Actions Status

The Buildpack supports C# and F# projects. It searchs through the repository's folders to locate a Startup.* or Program.* file. If found, the .csproj or .fsproj in the containing folder will be used in the dotnet publish <project> command.

If repository contains multiple Web Applications (multiple Startup.* or Program.*), PROJECT_FILE and PROJECT_NAME environment variables allow to choose project for publishing.

Usage

.NET Core latest stable

heroku buildpacks:set jincod/dotnetcore

.NET Core edge

heroku buildpacks:set https://github.com/jincod/dotnetcore-buildpack

.NET Core Preview release

heroku buildpacks:set https://github.com/jincod/dotnetcore-buildpack#preview

Previous releases

heroku buildpacks:set https://github.com/jincod/dotnetcore-buildpack#version

Available releases

More info

Entity Framework Core Migrations

You cannot run migrations with the dotnet ef commands using .NET Local Tools once the app is built. Alternatives include:

Enabling Automatic Migrations

dotnet new tool-manifest
dotnet tool install dotnet-ef
<Target Name="PrePublishTarget" AfterTargets="Publish">
  <Exec Command="dotnet ef database update" />
</Target>

Manually Running Migration Scripts on the Database

Setting Connection String for heroku PostgreSQL

If you are using Heroku Postgres addon, then Heroku provides the database connection string as environment variable DATABASE_URL. In order to use this in an Npgsql connection string, do something like following:

using System.Text.RegularExpressions;

...

// Replace AppContext with your own DbContext Class
builder.Services.AddDbContext<AppContext>(options =>
{
  if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
  {
    var m = Regex.Match(Environment.GetEnvironmentVariable("DATABASE_URL")!, @"postgres://(.*):(.*)@(.*):(.*)/(.*)");
    options.UseNpgsql($"Server={m.Groups[3]};Port={m.Groups[4]};User Id={m.Groups[1]};Password={m.Groups[2]};Database={m.Groups[5]};sslmode=Prefer;Trust Server Certificate=true");
  } 
  else // In Development Environment
  {
    // So, use a local Connection
    options.UseNpgsql(builder.Configuration.GetValue<string>("CONNECTION_STRING"));
  }
});

Node.js and NPM

heroku buildpacks:set jincod/dotnetcore
heroku buildpacks:add --index 1 heroku/nodejs

Using Multiple Buildpacks for an App

herokuish support

heroku config:set HEROKUISH=true

Example

ASP.NET Core Demo App

Donation

If this project help you, you can give me a cup of coffee ☕

Donate