You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gcloud Functions部署触发TypeError崩溃问题求助

解决Gcloud Functions命令行部署崩溃问题

问题现象

  • 命令行部署时触发错误:
    ERROR: gcloud crashed (TypeError): expected string or bytes-like object
    
  • 手动上传相同代码压缩包无异常。
  • 使用的部署命令:
    gcloud functions deploy myfunction \
      --runtime python39 \
      --trigger-http \
      --entry-point main \
      --source . \
      --region europe-west1 \
      --project myproject \
      --env-vars-file env.json
    
  • 调试模式下的报错栈:
    DEBUG: expected string or bytes-like object
    Traceback (most recent call last):
      File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 987, in Execute
        resources = calliope_command.Run(cli=self, args=args)
      File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 807, in Run
        resources = command_instance.Run(args)
      File "/usr/lib/google-cloud-sdk/lib/surface/functions/deploy.py", line 104, in Run
        return command_v2.Run(args, self.ReleaseTrack())
      File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/functions/v2/deploy/command.py", line 1065, in Run
        _SetInvokerPermissions(args, function, is_new_function)
      File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/command_lib/functions/v2/deploy/command.py", line 866, in _SetInvokerPermissions
        service_ref_one_platform = resources.REGISTRY.ParseRelativeName(
      File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/core/resources.py", line 1082, in ParseRelativeName
        return parser.ParseRelativeName(
      File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/core/resources.py", line 203, in ParseRelativeName
        match = re.match(path_template, relative_name)
      File "/usr/lib/google-cloud-sdk/platform/bundledpythonunix/lib/python3.9/re.py", line 191, in match
        return _compile(pattern, flags).match(string)
    TypeError: expected string or bytes-like object
    ERROR: gcloud crashed (TypeError): expected string or bytes-like object
    

排查与解决方案

从报错栈来看,问题出在设置调用者权限的_SetInvokerPermissions方法中,正则匹配时传入了非字符串类型参数。结合手动上传正常的情况,可按以下步骤排查:

1. 检查环境变量文件env.json

  • 确保文件格式完全合法,无语法错误(如缺少逗号、引号不匹配)。
  • 环境变量值必须为字符串类型,若有数字、布尔值需用引号包裹,避免空值或非字符串内容。

2. 升级Google Cloud SDK

该报错可能是SDK版本bug导致,执行命令升级到最新版本:

gcloud components update

3. 临时调整部署参数(测试用)

  • 若不需要公开访问,添加--no-allow-unauthenticated参数跳过权限自动设置;
  • 改用--set-env-vars直接设置环境变量,替代--env-vars-file,测试是否绕过报错:
    gcloud functions deploy myfunction \
      --runtime python39 \
      --trigger-http \
      --entry-point main \
      --source . \
      --region europe-west1 \
      --project myproject \
      --set-env-vars KEY1=VALUE1,KEY2=VALUE2
    

4. 核对项目与区域配置

确认当前gcloud配置的项目、区域与命令一致:

gcloud config get-value project
gcloud config get-value region

若不一致,先设置正确配置再部署:

gcloud config set project myproject
gcloud config set region europe-west1

5. 清理SDK缓存

清理本地缓存文件,避免缓存数据异常:

rm -rf ~/.config/gcloud/cache

内容的提问来源于stack exchange,提问作者blob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:44:55