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