{"id":17337289,"url":"https://github.com/anycodes/serverless-global","last_synced_at":"2025-10-28T09:38:22.734Z","repository":{"id":143908295,"uuid":"233520976","full_name":"anycodes/serverless-global","owner":"anycodes","description":"Serverless Framework Components全局变量配置方案","archived":false,"fork":false,"pushed_at":"2020-03-18T03:18:53.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-06T00:17:05.404Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anycodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-13T05:48:35.000Z","updated_at":"2020-03-18T03:18:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"276e5e03-6ae6-4196-b29e-c84682cb2a89","html_url":"https://github.com/anycodes/serverless-global","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2Fserverless-global","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2Fserverless-global/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2Fserverless-global/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anycodes%2Fserverless-global/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anycodes","download_url":"https://codeload.github.com/anycodes/serverless-global/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245806458,"owners_count":20675298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T15:34:35.369Z","updated_at":"2025-10-28T09:38:17.701Z","avatar_url":"https://github.com/anycodes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Components全局变量配置\n\n在使用Serverless Framework Components的时候，会遇到配置一些全局变量，例如我有多个函数，需要配置数据库等信息，但是在Serverless Framework Components中貌似并不能支持全局变量，所以做了这个非常简单的组件来实现。\n\n使用方法很简单，在Yaml中增加全局配置的字段：\n\n```yaml\nConf:\n  component: \"serverless-global\"\n  inputs:\n    mysql_host: gz-cdb-mytest.sql.tencentcdb.com\n    mysql_user: mytest\n    mysql_password: mytest\n    mysql_port: 62580\n    mysql_db: mytest\n    mini_program_app_id: mytest\n    mini_program_app_secret: mytest\n    tst:  ${file(./serverless_1.yaml)}\n```\n\n然后在项目部署的时候可以直接放到对应的组件内，例如：`${Conf.mysql_host}`\n\n如果需要引用其他yaml，可以使用上面的`tst:  ${file(./serverless_1.yaml)}`写法，其他程序引用只需要：\n`mysql_host: ${Conf.tst.mysql_hosta}`即可。此处路径支持绝对路径和相对路径\n\n整体Yaml可以这样：\n\n```yaml\nConf:\n  component: \"serverless-global\"\n  inputs:\n    mysql_host: gz-cdb-mytest.sql.tencentcdb.com\n    mysql_user: mytest\n    mysql_password: mytest\n    mysql_port: 62580\n    mysql_db: mytest\n    mini_program_app_id: mytest\n    mini_program_app_secret: mytest\n\n\nAlbum_Login:\n  component: \"@serverless/tencent-scf\"\n  inputs:\n    name: Album_Login\n    codeUri: ./album/login\n    handler: index.main_handler\n    runtime: Python3.6\n    region: ap-shanghai\n    environment:\n      variables:\n        mysql_host: ${Conf.mysql_host}\n        mysql_port: ${Conf.mysql_port}\n        mysql_user: ${Conf.mysql_user}\n        mysql_password: ${Conf.mysql_password}\n        mysql_db: ${Conf.mysql_db}\n```\n\n这样就可以设置全局变量了。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanycodes%2Fserverless-global","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanycodes%2Fserverless-global","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanycodes%2Fserverless-global/lists"}