Skip to content

Commit

Permalink
feat: add dotnet project support (#704)
Browse files Browse the repository at this point in the history
* add csproject support

* update dockerfile base version

* remove trimmed publish
  • Loading branch information
HomelessDinosaur authored Mar 18, 2024
1 parent 8e4861d commit b8e7e08
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions pkg/project/runtime/csharp.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

# https://github.com/dotnet/runtime/issues/94909
ENV DOTNET_EnableWriteXorExecute=0

ARG HANDLER

Expand All @@ -8,11 +11,13 @@ WORKDIR /app
COPY . ./

# Build and publish a release
RUN dotnet publish -c Release -o out --self-contained -p:PublishSingleFile=true -p:PublishTrimmed=true
RUN dotnet publish -c Release -o out --self-contained -p:PublishSingleFile=true

# Build runtime image
FROM mcr.microsoft.com/dotnet/runtime-deps:7.0
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0

ARG HANDLER

COPY --from=build /app/out/hello /usr/bin/handler
COPY --from=build /app/out/${HANDLER} /usr/bin/handler

ENTRYPOINT ["handler"]
7 changes: 5 additions & 2 deletions pkg/project/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ var csharpDockerfile string
var csharpIgnores = append([]string{"obj/", "bin/"}, commonIgnore...)

func csharpBuildContext(entrypointFilePath string, additionalIgnores []string) (*RuntimeBuildContext, error) {
// Convert the service name to the name of the binary produced. i.e. services/hello.csproj -> hello
handler := strings.ReplaceAll(filepath.Base(entrypointFilePath), ".csproj", "")

return &RuntimeBuildContext{
DockerfileContents: csharpDockerfile,
BaseDirectory: ".", // use the nitric project directory
BuildArguments: map[string]string{
"HANDLER": filepath.ToSlash(entrypointFilePath),
"HANDLER": handler,
},
IgnoreFileContents: strings.Join(append(additionalIgnores, csharpIgnores...), "\n"),
}, nil
Expand Down Expand Up @@ -182,7 +185,7 @@ func NewBuildContext(entrypointFilePath string, dockerfilePath string, buildArgs
ext := filepath.Ext(entrypointFilePath)

switch ext {
case ".cs":
case ".csproj":
return csharpBuildContext(entrypointFilePath, additionalIgnores)
case ".go":
return golangBuildContext(entrypointFilePath, additionalIgnores)
Expand Down

0 comments on commit b8e7e08

Please sign in to comment.