Google cloud build, referencing parent folder in a monorepo

Viewed 966

We have a monorepo setup in which each folder has a typescript / node service. There is also a shared /types folder which we pull into each project using typescript project references.

Since you cannot COPY a parent folder, locally we use:

docker build -f Dockerfile ..

Which works fine, but trying the same thing using cloudbuild.yaml and the docker builder throws the following error, about a missing file descriptor?

docker.io/library/docker:latest                                         
error checking context: 'file ('/proc/1/fd/5') not found or excluded by .dockerignore'.                                                         
ERROR                                                                                                                                           
ERROR: build step 0 "docker" failed: step exited with non-zero status: 1                                                                        
------------------------------------------------------------------------------------------------------------------------------------------------
ERROR: (gcloud.builds.submit) build dc31344a-b836-4089-87ab-e6ce7c69cab3 completed with status "FAILURE"

So how can we use gcloud build with cloud run services in a mono repo and still pull in our shared types?

1 Answers

When you run a Cloud Build job, it copy all the file from the current directory. If you are in a subdirectory, all the sibling or parents directory aren't uploaded and thus known on Cloud Build.

For this, you have to run your Cloud Build job at the parent directory level, and reference the CloudBuild.yaml file in your command (and point to the correct subdirectory).

Think to also change your cloudbuild.yaml step definition, because your root folder is no longer the subdirectory, but the root directory. You can use the directory parameter to specify the working directory at each step.

EDIT 1

If you don't want to upload the whole monorepo on each component build, you can ignore files when you trigger the gcloud builds summit command manually.

However you can't do this with the triggers. You can ignore files but this time, it means that files have to be ignored in the the build jobs trigger condition (for example, the readme file, when you push it, it's useless to trigger a new build). But, if the build is triggered by other files, these 'ignored file' will still be present in the run job.

Related