Search code examples
gcloudgoogle-cloud-rungoogle-cloud-build

Including a subfolder from an excluded folder in .gcloudignore


I'm trying to deploy a Node project with a Dockerfile to Google Cloud Run with the gcloud beta run deploy command.

Long story short, I would like to copy my local node_modules/my-module after running RUN npm install in the Dockerfile:

COPY node_modules/my-module /app/node_modules/my-module/

(I only do this while in development, to avoid committing and pushing every change from my-module for testing).

Unfortunately, Docker cannot copy this directory since, apparently, node_modules is not uploaded to Cloud Build by default.

So I created this .gcloudignore file to override the default:

.gcloudignore
.git
.gitignore

node_modules/
!node_modules/my-module/

I've tried a lot of other syntaxes but none allowed me to exclude node_modules while including node_modules/my-module.

However, I can include the whole node_modules directory by omitting it from the .gcloudignore file, but this obviously takes forever to upload.

Do you know how I could upload my local module to Cloud Build?


Solution

  • After reading this Gist's comments, I realized you have to also include the parent directories, like so:

    node_modules/**
    
    !node_modules/
    !node_modules/my-module/**
    

    This will exclude all subfolders from node_modules except my-module and its content (the ** is important, otherwise only the empty folder would be included).