13

Gcloudignore works like gitignore in that you can exclude certain files from uploading to GCF. Sometimes when you have really large projects with lots of generated files, it can be useful to exclude all files except a few.

.gcloudignore

# Ignore everything
# Or /*
*

# Except the Cloud Function files we want to deploy
!/package.json
!/index.js

The following gcloudignore file gives us: File index.js or function.js that is expected to define function doesn't exist in the root directory. meaning index.js is ignored and can not be read.

However the following ignore file syntax works just fine to deploy:

# Ignore everything
/[!.]*
/.?*

# Except the Cloud Function files we want to deploy
!/package.json
!/index.js

I tried peering into the gcloud program code, but I was wondering if anyone knows why this is the case?

kaya3
  • 47,440
  • 4
  • 68
  • 97
yellowarchangel
  • 374
  • 4
  • 14
  • The solution I found was to ignore the root-level files manually, and then ignore everything in the subdirectories using proper wildcard /dir/* !/dir/dir2/** Not a solution, but a work around for others – yellowarchangel Oct 24 '18 at 21:32
  • 1
    You can run this command from a dir containing .gcloudignore to list files that would be selected for upload by gcloudignore: gcloud meta list-files-for-upload; if you catch it failing post a small example. I tried it with a dir containing package.json and index.js and it worked. – Glenn Fowler Oct 26 '18 at 16:51
  • 1
    This command works, but not exactly what I mean. This command just tells us what files the .gcloudignore allows through. In the use case: `* !/react/dist/** !/index.js !/package.json` `react` directory will be ignored entirely. If you include it in the unignore, EVERY file in the directory will be included, not just dist – yellowarchangel Oct 29 '18 at 18:55
  • Otherwise, the errors mentioned above happen. The problem is in the gcloudignore parsing code treating root in a nonstandard way (compared to gitignore parsing) – yellowarchangel Oct 29 '18 at 18:56

2 Answers2

16

I ran into this problem as well.

I found a workaround -- explicitly allow the current dir:

# Ignore everything by default
*

# Allow files explicitly
!foo.bar
!*.baz

# Explicitly allow current dir. `gcloud deploy` fails without it.
!.

Works for me. Don't know why.

Arne Jørgensen
  • 161
  • 1
  • 6
  • 3
    It's worth noting that the implementation of .gcloudignore seems to be subtly different between appengine and cloud functions - appengine .gcloudignore doesn't seem to require the explicit allow of current dir (!.), whereas cloud function deploy does. – John Carter Jan 29 '20 at 23:24
  • That's with Google Cloud SDK 278.0.0. – John Carter Jan 29 '20 at 23:51
  • 4
    Yeah, I have been using rules like the OP fine in app engine, but stumbled across this bug in functions' implementation of this. Even more annoying, `gcloud meta list-files-for-upload` does handle it correctly, and so doesn't match the behaviour of the actual deploy. – El Yobo Jan 08 '21 at 05:23
4
OperationError: code=13, message=Error setting up the execution environment for your function. Please try deploying again after a few minutes.

I just got this problem in recent. The error code 13 means, in my view point, the code/function failed to initialize (with in time).

In my case, the problem is mostly caused by either Provided module can't be loaded. (Dependencies failed) or Provided code is not a loadable module. (Main module is missing) after checking Stackdriver Logs, which point to the deploy isn't complete.

Below is how I got it to work:

# Ignore everything by default
/*

# Allow files explicitly
!config.js
!index.js

# Tried !app/ but does not work
!app/**

# Explicitly allow current folder, as Arne Jørgensen said.
!.

Be care the !app/ isn't enough. I have to use !app/** to including all files.

If it's your case, check the source tab from function console to see which files uploaded in actual.

Pin Gu
  • 133
  • 1
  • 3
  • 9