Skip to content

Instantly share code, notes, and snippets.

@cbandy
Last active November 13, 2023 10:17
Show Gist options
  • Save cbandy/b58d32fe44dcbaa91f35e821efb72066 to your computer and use it in GitHub Desktop.
Save cbandy/b58d32fe44dcbaa91f35e821efb72066 to your computer and use it in GitHub Desktop.
Ignore everything except allowed files in .gcloudignore
# Ignore everything
/[!.]*
/.?*
# Except the Cloud Function files we want to deploy
!/package.json
!/index.js
!/lib/**
#!include:.gitignore
import os
from googlecloudsdk.command_lib.util import gcloudignore
chooser = gcloudignore.GetFileChooserForDir('.')
predicate = chooser.IsIncluded
for root, _, filelist in os.walk('.'):
path = os.path.normpath(os.path.relpath(root, '.'))
if not predicate(path):
continue
for f in filelist:
filename = os.path.normpath(os.path.join(root, f))
if not predicate(filename):
continue
print(filename)
@jake-tulip
Copy link

@cbandy do you know how to also exclude everything in a subdirectory except one directory.

So if we have:

index.js
package.json
dir/index.js
dir/.gitignore
dir/config/config.yaml
dir/config/config2.yaml

What would be the pattern to only deploy:

  1. package.json
  2. index.js
  3. dir/config/*

@cbandy
Copy link
Author

cbandy commented Mar 23, 2019

@jake-tulip this is late, and I don't know that you'll get a notification... I've tested the following with the above Python script:

!/dir/
!/dir/config/**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment