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

如何使用Google Cloud Deployment Manager设置自定义环境变量?

Google Cloud Deployment Manager 自定义环境变量实现方案

内置部署环境变量说明

Google Cloud Deployment Manager会提供部署特定环境变量,比如项目ID、部署名称等,可在模板中直接通过context.env调用,示例代码如下:

def GenerateConfig(context):
  resources = []
  resources.append ({
    'name': 'vm-' + context.env["deployment"],
    'type': 'compute.v1.instance',
    'properties': {
       'serviceAccounts': [{
         'email': context.env['project_number'] + '-compute@developer.gserviceaccount.com',
         'scopes': [...]
       }]
    }
    ...}]
  return {'resources': resources}

自定义环境变量的变通方案

目前Deployment Manager原生不支持直接设置模板可用的自定义环境变量,可通过以下几种方式实现类似效果:

  • 通过配置文件properties传递参数:在部署的YAML配置文件中定义自定义参数,模板中通过context.properties读取。示例:
    部署配置文件(config.yaml):

    imports:
    - path: my-template.py
    resources:
    - name: my-deployment
      type: my-template.py
      properties:
        custom_env_var: "my-custom-value"
        another_var: "another-value"
    

    模板文件(my-template.py):

    def GenerateConfig(context):
      custom_value = context.properties.get('custom_env_var')
      another_value = context.properties.get('another_var')
      # 使用参数创建资源
      resources = [{
        'name': 'vm-' + custom_value,
        'type': 'compute.v1.instance',
        ...
      }]
      return {'resources': resources}
    
  • 使用资源标签传递元数据:给部署的资源添加自定义标签,后续可通过标签获取自定义信息,或在模板逻辑中直接引用标签值关联资源。

  • 结合Cloud Storage存储配置:将自定义变量写入Cloud Storage的配置文件,模板中通过HTTP请求读取该文件内容获取参数(需确保模板拥有访问Cloud Storage的权限)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 19:40:26