0

我的“拓扑”是:

-- /config/
-- /config/conf1/stage1/config.cfg
-- /config/conf1/stage2/config.cfg
-- /config/conf2/stage1/config.cfg
-- /config/conf2/stage2/config.cfg
-- /lib/
...
-- app.js
-- lambda_function_one.js
-- lambda_function_two.js
   ...
-- config.cfg

我有一个非常适合无服务器的项目——node/lambda。根据需要管理设置阶段和配置,但有一个例外。

我们在项目根目录中使用 config.cfg 进行本地测试,其中包含阶段内的 app.js 和配置文件,用于那些尊重的配置。

一种方法是将本地配置移动到另一个文件中,然后在打包之前使用 shell 脚本将目标配置复制到项目根目录中。

是否可以指定路径,以便无服务器从目录中获取 config.cfg 并以某种方式将其打包到“项目根目录”中?

谢谢你。

4

1 回答 1

0

您可能想看看serverless-plugin-write-env-vars.env此插件将在部署时创建一个文件,其中包含您在serverless.yml. 您仍然可以将配置保存在单独的文件.yml.json)中并从serverless.yml.

例如:

假设/config/conf1/stage1/config.yml包括:

# config.yml

url: https://foo.com

你可以参考它serverless.yml

# serverless.yml

custom:
  myStage: ${opt:stage, self:provider.stage}
  writeEnvVars:
    MY_URL: ${file(./config/conf1/${self:custom.myStage}/config.yml):url}

plugins:
   - serverless-plugin-write-env-vars

对于本地测试,您需要有一个脚本来.env自己创建文件。一个python示例:

# test.py

import os.path
import sys
import shutil

here = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(here, ".."))


def before():
    # copy temp .env to root folder
    dotenv_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '.env_stub')
    global temp_file_path
    temp_file_path = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, '.env'))
    print('Creating ' + temp_file_path + '...')
    shutil.copyfile(dotenv_path, temp_file_path)


def after():
    # remove temp .env from root folder
    print('Removing ' + temp_file_path + '...')
    os.remove(temp_file_path)


temp_file_path = None
before()    
# test ...
after()
于 2016-11-07T12:31:18.177 回答