如何在 .gcloudignore 中包含在 .gitignore 中被忽略的文件

How to include files in .gcloudignore that are ignored in .gitignore

我在 .gitignore 中忽略了某些文件夹,因为我不希望它在 Github 上发布。

但是,这些文件对于部署到 Google 云(本地包)是必不可少的。

如果我从 .gcloudignore 中取出 .gitignore,那么我不想忽略的文件(如 venv, .idea, .pyc)会被上传,这是我不想要的。

我怎样才能在 .gcloudignore 中只包含 .gitgnore 的一部分?


.gcloudignore

.gcloudignore
.git
.gitignore

node_modules
#!include:.gitignore

.gitignore

# This I want to ignore in .gcloudignore
.Python
env/
build/

# This I want to keep in .gcloudignore
module_data_import
module_calendar

这 2 个文件大部分是正交的,它们用于不同的目的并由不同的工具使用。

它们之间的唯一关系(除了 .gcloudignore 大量继承自 .gitignore 语法和行为之外)是能够将 .gitignore 文件包含在 .gcloudignore文件,这是你所做的:

#!include:.gitignore

其副作用当然是 .gitignore 中的所有内容 也将在 GAE 部署期间被忽略,正如您观察到的那样。来自 gcloud topic gcloudignore:

This will include the contents of a .gitignore-style file at that path at that point in the file. It does not recurse (that is, the included file cannot #!include another file) and cannot be anywhere but the top-level directory to be uploaded.

但是您不必包括.gitignore。您需要做的就是放弃该包含,并仅指定 .gitignore 中您希望在部署中忽略的模式。如果我正确理解了你的目标描述,.gcloudignore 看起来像这样:

.gcloudignore
.git
.gitignore

node_modules
module_data_import
module_calendar

如果在两个文件中复制这些模式困扰您一个潜在的替代方案(恕我直言相当复杂)可能利用git忽略基于文件的能力从多个来源收集的模式(虽然范围不同,所以要注意这一点)。参见 https://git-scm.com/docs/gitignore and Can I include other .gitignore file in a .gitignore file? (like #include in c-like languages)

如果您能找到适合您应用的合适拆分,您可以:

  • 只在 .gitignore 中保留您在部署时也希望忽略的文件模式(当然,在 .gcloudignore 中保留 .gitignore 包含)
  • 将其他模式放在 git 用来确定忽略规则的另一个文件中 - 因此它们会被 git 忽略,但由于此文件未包含在 .gcloudignore 中他们不会在部署时被忽略。

更新以下评论:

在最初的回答中,我没有考虑 gcloud 命令本身执行直接或间接目标源代码存储库操作的情况,如果使用 git 作为版本控制系统, 当然会受到 .gitignore 文件的存在和内容的影响。在这种情况下,可以认为 gcloud 命令本身使用 .gitignore 文件(无论 .gcloudignore 文件如何)。

如果某个文件之前已被 .gcloudignore 文件中的模式(例如继承您的 .gitignore 设置或排除的目录)排除,您可以明确包含该文件。

要包含名为 foo 的文件,请确保在排除它的规则之后出现以下内容:

!foo

要包含名为 'bar' 的文件夹,请确保在排除它的规则之后出现以下内容:

!bar/

来源:https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore

要包含在 .gitignore 中被忽略的文件夹,我必须使用以下语法:

!bar/**

没有 ** 是行不通的。