paketo-buildpacks / dotnet-execute

Apache License 2.0
3 stars 5 forks source link

Building sub-path that references another sub-path #168

Closed ryanmoran closed 3 years ago

ryanmoran commented 3 years ago

I have a quick question that if I have 2 .NET Core projects and shared the same contract DTOs (such as Blazored Client and Server-side, or Microservices with the shared building blocks), and could I use gcr.io/paketo-buildpacks/dotnet-core to build these kinds of projects? I have struggled to make it work, but no luck, look like the `buildpacks only scan and check all dependencies inside the folder it belongs to (if I referenced into another project folder, not in this project, that mean, it's outside of the current project folder then it wasn't working and couldn't make the build succeed)

I have the demo repo at https://github.com/thangchung/practical-dotnet-with-cna

In this repo, I have the shared library with named shared/WeatherLib, and the API with named service1 which referenced to shared/WeatherLib

I tried to build service1 as

$ pack build webapi --buildpack gcr.io/paketo-buildpacks/dotnet-core

But I have got the error message that the service1 couldn't find the shared/WeatherLib

Hope this description will make you clear about what I have struggled with.

Originally posted by @thangchung in https://github.com/paketo-buildpacks/dotnet-execute/issues/151#issuecomment-817744769

fg-j commented 3 years ago

Hi there @thangchung. I managed to build your service.

First, how I got the build to complete:

  1. Starting with your repo, relocated service1/buildpack.yml to ./buildpack.yml.

  2. Replaced buildpack.yml contents with:

    ---
    dotnet-build:
    project-path: "service1/src/WebApi"

    because this is the path from the root of the repo to the directory where the service's project file is located. This will allow our dotnet-publish buildpack to find the right project to build.

  3. Then, ran

    pack build webapi --buildpack gcr.io/paketo-buildpacks/dotnet-core

    from the root of the repo. This means that the entire root directory's contents (including that shared projfile) will be present in the build container. This is what resolves your "file not found" error.

With this setup, I found that the container builds successfully! I wasn't sure how I should validate the app's behaviour beyond that.

One caveat: I noticed that in your README you specify that the project should be built with the -self-contained=true option. Currently, the buildpack does not support building in this way. You'll see in the build output that the buildpack explicitly runs dotnet publishwith the --self-contained false flag right now. But, we have an issue open (paketo-buildpacks/dotnet-publish#145) to address this. If the self-contained build is important to you, please throw some feedback/information about your use-case on that issue!

thangchung commented 3 years ago

@fg-j Thank you for your suggestion, I could handle building it

But if we move buildpack.yml to the root folder, then if we have another service (normally we have many projects in monorepo approach), let's say service2 on the same level of service1, and with the content of buildpack.yml file as

---
dotnet-build:
  project-path: "service1/src/WebApi"

And my question is how can we describe the build script for service2?

I have committed the fixed and what I have concerned into https://github.com/thangchung/practical-dotnet-with-cna

image

fg-j commented 3 years ago

So in this case, for building service 2, you'd want to use a buildpack.yml that contains:

---
dotnet-build:
  project-path: "service2/src/WebApi2"

This clearly isn't ideal though, because it requires changing the checked-in code in order to build each service. With the upcoming release of the Paketo .NET Core language family buildpack, we'll be rolling out the ability to configure project path via an environment variable that can be set at build time.

We have an issue open to track the progress on this initiative: paketo-buildpacks/dotnet-core#466

thangchung commented 3 years ago

@fg-j It makes sense to me. Thanks for the clarification above 👍

fg-j commented 3 years ago

Configuring the project path using an environment variable (BP_DOTNET_PROJECT_PATH) is possible as of Paketo .NET Core Buildpack v.0.2.0. The Paketo .NET docs explain how to use the env var instead of buildpack.yml.

thangchung commented 3 years ago

It works perfectly after I ran

$ pack build vietnamdevsgroup/webapi --env BP_DOTNET_PROJECT_PATH=./service1/src/WebApi 
--buildpack gcr.io/paketo-buildpacks/dotnet-core
$ pack build vietnamdevsgroup/webapi2 --env BP_DOTNET_PROJECT_PATH=./service2/src/WebApi2 
--buildpack gcr.io/paketo-buildpacks/dotnet-core

Thanks for your great effort to make it work with environment injection.