【问题标题】:Disable sls plugin on production (different plugins per stage?)在生产中禁用 sls 插件(每个阶段使用不同的插件?)
【发布时间】:2018-01-26 11:46:27
【问题描述】:

serverless-offline 插件有问题。我希望它只能在本地开发(开发阶段)时访问,但不能在任何其他阶段访问

我的serverless.yml 看起来像这样:

service: foo
provider:
  # ...
  stage: dev

plugins:
  - serverless-offline

custom:
  stage: "${opt:stage, self:provider.stage}"
  # ...

它工作正常(在我的本地机器上)

我试过this solution(将插件作为自定义变量传递),但它不起作用

service: foo
provider:
  # ...
  stage: dev

custom:
  stage: "${opt:stage, self:provider.stage}"
  plugins:
    dev:
      - serverless-offline
  # ...

plugins: ${self:custom.plugins.${self:custom.stage}}

当运行sls offline start 时,它给了我一个错误Serverless command "offline" not found


我已将 serverless-offline 包含为 devDependencies (package.json) - 在生产中,当插件包含在 serverless.yml 中时,它会给出错误 Serverless command "offline" not found

如何解决这种问题(现在我必须在部署之前将其注释掉)?

【问题讨论】:

    标签: amazon-web-services lambda serverless


    【解决方案1】:

    https://forum.serverless.com/t/separate-plugins-for-different-environments/2043/7 声明你不能让插件动态工作。

    在解析 serverless.yml 之前加载插件,因此您不能拥有包含或排除基于阶段的插件的动态变量。

    我最终编写了一个小脚本来从配置文件中删除 plugins 属性。我们的配置是 JSON,不是 YAML,但是你可以使用 yamljs 包来解析和编写 YAML 文件。这样您就可以在版本控制系统中保持配置文件完整,并且只在构建期间对其进行修改。

    它并不优雅,因为它会删除所有插件而不是特定插件,但它确实有效。如果您只需要删除特定插件,也很容易适应。

    我已将此代码放入名为 remove-serverless-offline.js 的文件中。

    const jsonfile = require('jsonfile');
    const file = './serverless.json';
    jsonfile.readFile(file, function(err, obj){
    
      if(!obj.hasOwnProperty('plugins')){
    
        console.log('serverless.json: could not find serverless-offline in plugin list.');
        return;
      }
    
      delete obj.plugins;
    
      jsonfile.writeFile(file, obj, {spaces: 4}, function(err){
    
        console.error(err);
      });
    
      console.log('serverless.json: removed serverless-offline from plugin list.')
    });
    

    在我的package.jsonscripts 下,我添加了这个:

    "remove-serverless-offline": "node remove-serverless-offline.js",
    

    在 buildspec.yml 中,只需将此行添加到 pre_build 命令。

    - npm run remove-serverless-offline
    

    【讨论】:

      猜你喜欢
      • 2019-07-17
      • 2015-03-02
      • 2011-01-02
      • 1970-01-01
      • 2013-11-17
      • 2018-04-07
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多