{"id":579,"date":"2019-10-04T17:30:00","date_gmt":"2019-10-04T08:30:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/10\/04\/gatsby_environment"},"modified":"2019-10-04T17:30:00","modified_gmt":"2019-10-04T08:30:00","slug":"gatsby_environment","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/10\/04\/gatsby_environment\/","title":{"rendered":"Gatsby.js\u3067.env.*\u3092\u4f7f\u3046"},"content":{"rendered":"
\u3053\u3093\u306b\u3061\u306f\u3002\u571f\u5c45\u3067\u3059\u3002\u3053\u306e\u5ea6\u3001\u5f0a\u793e\u30b3\u30fc\u30dd\u30ec\u30fc\u30c8\u30b5\u30a4\u30c8<\/a>\u3092\u30ea\u30cb\u30e5\u30fc\u30a2\u30eb\u3044\u305f\u3057\u307e\u3057\u305f\u3002 \u4eca\u56de\u3001\u69cb\u7bc9\u306fGatsby.js(React\u30d9\u30fc\u30b9\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af)\u3092\u4f7f\u7528\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u969b\u306b\u74b0\u5883\u5909\u6570\u306e\u6271\u3044\u3067\u5c11\u3057\u30cf\u30de\u3063\u3066\u3057\u307e\u3063\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u5f0a\u793e\u3067\u306fCircleCI\u306b\u3088\u3063\u3066push\u5148\u30d6\u30e9\u30f3\u30c1\u3054\u3068\u306b\u30c7\u30d7\u30ed\u30a4\u74b0\u5883\u3092\u5206\u3051\u3066\u3044\u307e\u3059\u304c\u3001\u305d\u306e\u969b\u306b\u3088\u304fdotenv\u3092\u7528\u3044\u3066\u305d\u308c\u305e\u308c\u306e\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n \u3000<\/p>\n \u306e\u3088\u3046\u306b\u3001\u30d3\u30eb\u30c9\u6642\u306bcross-env\u3067NODE_ENV=staging\u3068\u3044\u3063\u305f\u3088\u3046\u306b\u6e21\u3057\u3066\u3001\u74b0\u5883\u3054\u3068\u306b.env.*\u3092\u4f7f\u3044\u5206\u3051\u3066\u3044\u308b<\/p>\n Gatsby.js\u3067\u306f\u3001.env\u30d5\u30a1\u30a4\u30eb\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e2\u7a2e\u985e\u306e\u4f7f\u3044\u5206\u3051\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n \u3053\u3053\u3067NODE_ENV\u306bstaging\u306a\u3069\u3092\u6e21\u3057\u3066\u3042\u3052\u308c\u3070\u3001.env.staging\u3092\u8aad\u3093\u3067\u304f\u308c\u308b\u306e\u304b\u306a\u3001\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3002\u3002 \u4ee5\u4e0b\u306e\u3088\u3046\u306b.env.*\u306bGATSBY_ACTIVE_ENV\u3092\u5229\u7528\u3059\u308b\u3088\u3046\u8ffd\u8a18\u3057\u3001<\/p>\n cross-env\u3092\u4f7f\u3063\u3066"staging"\u3092\u6e21\u3059\u3002<\/p>\n \u3053\u3046\u3059\u308c\u3070\u3001\u81ea\u7531\u306b.env.*\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n
\n\u30af\u30e9\u30a6\u30c9\u5165\u9580\u8a18\u4e8b<\/a>\u3084\u4e8b\u4f8b\u7d39\u4ecb<\/a>\u306a\u3069\u306a\u3069\u3001\u5927\u304d\u304f\u5237\u65b0\u30fb\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u5145\u5b9f\u3055\u305b\u3066\u304a\u308a\u307e\u3059\u306e\u3067\u662f\u975e\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n\u30c7\u30d7\u30ed\u30a4\u74b0\u5883\u3054\u3068\u306e.env\u30d5\u30a1\u30a4\u30eb\u306e\u5229\u7528<\/h2>\n
API_URL='dev-api.mmmcorp.co.jp'<\/code><\/pre>\n
API_URL='stg-api.mmmcorp.co.jp'<\/code><\/pre>\n
Gatsby.js\u306e.env<\/h2>\n
\n
\ngatsby build\u3057\u305f\u5834\u5408\u306f\u5e38\u306bNODE_ENV=production<\/strong>\u3068\u306a\u308a\u3001\u74b0\u5883\u5909\u6570\u3082.env.production\u306e\u5024\u304c\u5229\u7528\u3055\u308c\u307e\u3059\u3002<\/p>\n\u4efb\u610f\u306e.env.*\u3092\u5229\u7528\u3059\u308b\u8a2d\u5b9a<\/h2>\n
let activeEnv =\n process.env.GATSBY_ACTIVE_ENV || process.env.NODE_ENV || "development"\nrequire("dotenv").config({\n path: `.env.${activeEnv}`,\n})\nmodule.exports = {\n siteMetadata: {\n...<\/code><\/pre>\n
"scripts": {\n "build:stg": "cross-env GATSBY_ACTIVE_ENV=staging gatsby build"\n ...<\/code><\/pre>\n