Strapi - 配置环境变量

Strapi - Configure with environment variables

使用strapi 1.5.4

可以用环境变量配置strapi吗? 如果没有,您如何在没有 committing/exposing 您的数据库凭据和其他机密的情况下配置 strapi?

module.exports = {
  "orm": {
    "adapters": {
      "disk": "sails-disk",
      "mysql": "sails-mysql"
    },
    "defaultConnection": "default",
    "connections": {
      "default": {
        "adapter": "disk",
        "filePath": ".tmp/",
        "fileName": "default.db",
        "migrate": "alter"
      },
      "permanent": {
        "adapter": "mysql",
"user": process.env.DB_USER,
"password": process.env.DB_PASSWORD,
        "migrate": "alter"
      }
    }
  }
};

看来唯一的办法就是使用钩子了。 在我的 server.js 文件中(我会将配置移动到它自己的文件中并清理它)

const orm = {
  "adapters": {
    "disk": "sails-disk",
    "mysql": "sails-mysql"
  },
  "defaultConnection": "default",
  "connections": {
    "default": {
      "adapter": "disk",
      "filePath": ".tmp/",
      "fileName": "default.db",
      "migrate": "alter"
    },
    "permanent": {
      "adapter": "mysql",
      "user": process.env.DB_USER || 'root',
      "password": process.env.DB_PASSWORD || 'password',
      "database": process.env.DB_NAME || 'test',
      "host": "127.0.0.1",
      "migrate": "alter"
    }
  }
};

(function () {
  const strapi = require('strapi');
  // Use a hook to override the config
  strapi.on('hook:_config:loaded', () => {
    strapi.config.orm = orm;
  });
  strapi.start();
})();

您可以使用这个插件来管理您的秘密:https://github.com/cyberark/summon 上面的插件将提供更多关于你的秘密值的抽象,它们也得到了 providers 的支持。

2021 年,Strapi 提供了一个开箱即用的解决方案。

https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/configurations.html#configuration-using-environment-variables