Yaml 变量赋值

概述

Serverless Application模型对应的Yaml文件支持多种变量格式:

  • 获取当前机器中的环境变量:${env('环境变量')},例如 ${env('secretId')}, ${env('secretId', '默认值')}
  • 获取外部文档的变量:${file('路径')},例如 ${file('./path')}
  • 获取全局变量:${vars.*}
  • 获取Json字符串内容的变量:${json('json字符串')},例如 ${json(file('./a.json'))}
  • 获取路径的变量:${path('路径')},例如 ${path('../')}
  • 获取其他业务模块的变量:${resources.project_name.props.*}
  • 获取业务模块的结果变量:${resources.project_name.output.*}
  • 获取当前配置的config变量:${config('AccountID')}, 本质是获取 s config get中的变量值。
  • 获取当前模块的信息:${this.xx}

使用案例

使用${env('')}获取环境变量

以下面的Yaml为例:

resources:
  next_demo:
    component: v3test
    props: # 组件的属性值
      region: cn-hangzhou
      function:
        functionName: "next-start-hello"
        runtime: ${env('runtime', 'nodejs16')}
        code: ./code

next_demo中,${env('runtime')}将尝试获取当前计算机中runtime环境变量的值,如果获取不到,将使用默认值nodejs16

使用${file('')}获取外部文档内容

以下面的Yaml为例:

resources:
  framework: 
    component: fc 
    actions:
      pre-deploy:
        - plugin: website-fc
    props:
      service: ${file('./file.txt')}

若此时file.txt的内容为:

this is file fun test

则解析后结果为:

resources:
  framework: 
    component: fc 
    actions:
      pre-deploy:
        - plugin: website-fc
    props:
      service: this is file fun test

使用${vars.*}获取全局变量

以下面的Yaml为例:

vars: # 全局变量
  region: cn-hangzhou
  service:
    name: website
    description: Serverless Devs Website Service
    internetAccess: true
resources:
  framework: # 业务名称/模块名称
    component: fc3 # 组件名称
    props: # 组件的属性值
      region: ${vars.region}

framework中,${vars.region}将获取vars下的region参数,因此渲染结果为:

vars: # 全局变量
  region: cn-hangzhou
  service:
    name: website
    description: Serverless Devs Website Service
    internetAccess: true
resources:
  framework: # 业务名称/模块名称
    component: fc3 # 组件名称
    props: # 组件的属性值
      region: cn-hangzhou

使用${json('')}获取Json字符串内容

以下面的Yaml为例:

resources:
  framework: # 业务名称/模块名称
    component: fc3test # 组件名称
    props: # 组件的属性值
      region: cn-hangzhou
      function:
        name: vuepress
        description: ${json(file("./a.json"))}
        runtime: nodejs12

若其中a.json的内容为:

{
  "info": "this is a fun test"
}

则解析时,会将a.json中的内容加在description之下。渲染结果为:

resources:
  framework: # 业务名称/模块名称
    component: fc3test # 组件名称
    props: # 组件的属性值
      region: cn-hangzhou
      function:
        name: vuepress
        description: 
          info: this is a fun test
        runtime: nodejs12

使用${path('')}获取路径

以下面的Yaml为例:

vars: # 全局变量
  region: cn-hangzhou
  service:
    name: website
    description: Serverless Devs Website Service
    internetAccess: true
resources:
  framework: # 业务名称/模块名称
    component: ${path('./fc.js')} # 组件名称

framework中,${path('./fc.js')}将尝试获取fc.js文件的绝对路径。若路径为/Users/XXX/XXX/fc.js,则渲染结果为:

vars: # 全局变量
  region: cn-hangzhou
  service:
    name: website
    description: Serverless Devs Website Service
    internetAccess: true
resources:
  framework: # 业务名称/模块名称
    component: /Users/XXX/XXX/fc.js # 组件名称

使用${resources.project_name.props.*}获取其他业务模块的变量

以下面的Yaml为例:

vars: # 全局变量
  service:
    name: website-wof2
    description: Serverless Devs Website Service
    internetAccess: true

resources:
  framework: # 业务名称/模块名称
    component: fc3test # 组件名称
    props: # 组件的属性值
      region: cn-hangzhou
      service: ${vars.service}
      function:
        name: vuepress
        description: Serverless Devs Website vuepress Function
        codeUri: ./code/docs/.vuepress/dist
        runtime: nodejs12
        environmentVariables:
          region: cn-hangzhou
          functionName: ${resources.next_function.props.function.name}
  next_function: 
    component: fc3test
    props:
      region: cn-hangzhou
      service: ${vars.service} # 应用整体的服务配置
      function:
        name: next-function-example
        description: Serverless Devs Website vuepress Function
        codeUri: ./next-code
        runtime: nodejs12

framework中,${resources.next_function.props.function.name}会获取next_function中的function属性中的name值。因此,渲染结果为:

vars: # 全局变量
  service:
    name: website-wof2
    description: Serverless Devs Website Service
    internetAccess: true

resources:
  framework: # 业务名称/模块名称
    component: fc3test # 组件名称
    props: # 组件的属性值
    ...
      functionName: next-function-example
    ...

使用${resources.project_name.output.*}获取业务模块的结果变量

以下面的Yaml为例:

vars: # 全局变量
  region: cn-hangzhou
  service:
    name: website-wof2
    description: Serverless Devs Website Service
    internetAccess: true
resources:
  framework: # 业务名称/模块名称
    component: fc3test # 组件名称
    props: # 组件的属性值
      region: ${vars.region}
      service: ${vars.service}
      function:
        name: vuepress
        description: Serverless Devs Website vuepress Function
        codeUri: ./code/docs/.vuepress/dist
        timeout: 30
        memorySize: 512
        runtime: nodejs12
        environmentVariables:
          hello: ${resources.next_function.output.hello}
  next_function: # 第二个函数的案例,仅供参考
    component: fc3test
    props:
      region: ${vars.region}
      service: ${vars.service} # 应用整体的服务配置
      function:
        name: next-function-example
        description: Serverless Devs Website vuepress Function

framework中,${resources.next_function.output.hello}会等待next_function运行完后,获取输出的hello值。若next_function的输出的hello值为hello world,则渲染结果为:

vars: # 全局变量
  region: cn-hangzhou
  service:
    name: website-wof2
    description: Serverless Devs Website Service
    internetAccess: true
resources:
  framework: # 业务名称/模块名称
    component: fc3test
    props: # 组件的属性值
    ...
      hello: hello world
    ...

使用${config('')}获取当前配置的config变量

使用${this.xx}获取当前模块的信息

以下面的Yaml为例:

edition: 3.0.0
name: NextProject
access: default-access

resources:
  nextjs_portal:
    component: component
    actions:
      pre-deploy:
        - run: s invoke ${this.props.url}
          path: ./backend_src
    props:
      code: ./frontend_src
      url: url

nextjs_portal中:

  • 使用${this.name}将解析为nextjs_portal
  • 使用${this.props.code}将解析为 ./frontend_src
  • 使用${this.access}将解析为default-access
在 GitHub 上编辑本页面 更新时间: Fri, Nov 24, 2023