Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot resolve local dependencies from mono repo in Cloud Build docker image creation #253

Closed
blaueeiner opened this issue Jun 30, 2021 · 6 comments

Comments

@blaueeiner
Copy link

blaueeiner commented Jun 30, 2021

I am using functions-framwork-dart in a mono repo. It has its own dedicated project. I am sharing code between projects by using other projects which include the shared code. I am referencing the project as a dependency like this in the pubspec.yaml of the functions project:

dependencies:
  domain:
    path: ./../../domain

Locally everything can be resolved properly but as soon as I deploy with gcloud beta run deploy it fails executing the docker file.

---> 61f398c71af6
Step 2/12 : WORKDIR /app
 ---> Running in 4eea5b3c3ec4
Removing intermediate container 4eea5b3c3ec4
 ---> 652a0d1dd253
Step 3/12 : COPY pubspec.yaml /app/pubspec.yaml
 ---> 0825cd012304
Step 4/12 : RUN dart pub get
 ---> Running in 05d05694046c
Resolving dependencies...
Because functions depends on domain from path which doesn't exist (could not find package domain at "../../domain"), version solving failed.
The command '/bin/sh -c dart pub get' returned a non-zero code: 66
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 66

I assume I need to also upload the dependency project folders. But how to do this? And how to then specify the Dockerfile path?

@blaueeiner blaueeiner changed the title Cannot resolve local dependencies from mono repo Cannot resolve local dependencies from mono repo in Cloud Build docker image creation Jun 30, 2021
@nickmeinhold
Copy link
Contributor

Hi @blaueeiner! Just to clarify - you're aware you can switch to a git dependency before deploying but you want to keep the path dependency and copy over during the build?

@blaueeiner
Copy link
Author

blaueeiner commented Jul 1, 2021

Hi @blaueeiner! Just to clarify - you're aware you can switch to a git dependency before deploying but you want to keep the path dependency and copy over during the build?

@nickmeinhold Yes I am aware that I can use a git dependency. Ideally I would like to keep the path.

Yesterday I came to at least one solution for now that looks like this:

I use gcloud builds submit to upload all of my resources and build a new Docker image via Cloud Build. I needed to specify all the resources that I do not want to upload in the .gcloudignore file in my root folder which is not really ideally in my opinion. Furthermore I needed to customize the Dockerfile and specify a cloudbuild.yaml file custom step to properly dockerize and be able to specify where to find the Dockerfile.
After a successful build I use the tag that I specified in the command to run the gcloud run deploy command to deploy to Cloud Run.

I would appreciate to here about different solutions since I think mine is not that ideal.

@jimmyff
Copy link

jimmyff commented Jul 2, 2021

I have a rather convoluted solution to this problem too, I have a build script that copies the local project to temp/ in my main project file, then my Dockerfile copies both the current project (os_cloud_functions in my case) and the local project (os_core in my case) to the /app/ dir in container. After deploying the build scripts removes the temp directory.

Here is my Dockerfile:

FROM dart:stable AS build

# Resolve app dependencies.
WORKDIR /app/os_cloud_functions

COPY temp/os_core/. /app/os_core
COPY pubspec.* ./
RUN dart pub get

# Copy app source code and AOT compile it.
COPY . .
# Ensure packages are still up-to-date if anything has changed
RUN dart pub get --offline
RUN dart pub run build_runner build --delete-conflicting-outputs
RUN dart compile exe bin/server.dart -o bin/server

# Build minimal serving image from AOT-compiled `/server` and required system
# libraries and configuration files stored in `/runtime/` from the build stage.
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/os_cloud_functions/bin/server /app/os_cloud_functions/bin/

# Start server.
EXPOSE 8080
CMD ["/app/os_cloud_functions/bin/server"]

@mtwichel
Copy link

mtwichel commented Jul 13, 2021

Here's our solution - simply copy our local packages (in the dart_packages folder), then change the WORKDIR to app and continue as normal.

################
FROM google/dart:2.12

WORKDIR /dart_packages
COPY /dart_packages /dart_packages
WORKDIR /app
COPY /cb_serverless_functions/pubspec.yaml /app/pubspec.yaml
RUN dart pub get
COPY /cb_serverless_functions .
RUN dart pub get --offline

RUN dart pub run build_runner build --delete-conflicting-outputs
RUN dart compile exe bin/server.dart -o bin/server

########################
FROM subfuzion/dart:slim
COPY --from=0 /app/bin/server /app/bin/server
EXPOSE 8080
ENTRYPOINT ["/app/bin/server"]

Then in your cloud_build.yaml

steps:
  # Build the container image
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "gcr.io/$PROJECT_ID/cb_serverless_functions",
        "-f",
        "cb_serverless_functions/Dockerfile",
        ".",
      ]

Then just make sure this runs from the root of your repository and not in your directory with the cloud functions. The -f cloud_functions/Dockerfile will make sure it builds correctly

@kevmoo
Copy link
Collaborator

kevmoo commented Jul 29, 2021

@blaueeiner – are you unblocked here?

@blaueeiner
Copy link
Author

@kevmoo Yes thx :)

@kevmoo kevmoo closed this as completed Aug 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants