Google Cloud Builder 为什么不使用 gsutil rsync -x 排除文件?

How come file is not excluded with gsutil rsync -x by the Google Cloud Builder?

我目前运行 gsutil rsync 云构建命令:

gcr.io/cloud-builders/gsutil
-m rsync -r -c -d -x "\.gitignore" . gs://mybucket/ 

我在这里使用 -x "\.gitignore" 参数来尝试不复制 .gitignore 文件,如此处所述:

https://cloud.google.com/storage/docs/gsutil/commands/rsync

然而,当查看存储桶和日志时,它仍然说:

2021-04-23T13:29:37.870382893Z Step #1: Copying file://./.gitignore [Content-Type=application/octet-stream]...

所以尽管有 -x "\.gitignore" 参数,rsync 仍在复制文件。

根据文档 -x 是一个 Python 正则表达式,因此 //./.gitignore 应该被 \.gitignore

捕获

有谁知道为什么这不起作用以及为什么仍在复制文件?

查看rsync.py源代码:

if cls.exclude_pattern.match(str_to_check):

在 Python 中,re.match 只有 returns 出现在字符串开头的匹配项。

因此,为了使用 -x 参数在任何地方找到匹配项,您需要在需要查找的模式前加上 .*(?s).*:

gcr.io/cloud-builders/gsutil
-m rsync -r -c -d -x ".*\.gitignore" . gs://mybucket/ 

请注意,要确保 .gitignore 出现在字符串的末尾,您需要附加 $-x ".*\.gitignore$".